added formatting;

pull/4/head
Blake DeMarcy 2017-03-01 15:54:34 -06:00
parent d6b6190664
commit 4bb3340d43
1 changed files with 35 additions and 0 deletions

35
src/formatting.py 100644
View File

@ -0,0 +1,35 @@
import re
COLORS = ["red", "green", "yellow", "blue", "magenta", "cyan"]
KEYWORDS = COLORS + [
"bold", "italic", "underline"
]
TOKENS = re.compile(r"\[\[({}): (.+?)]]".format("|".join(KEYWORDS)), flags=re.DOTALL)
QUOTES = re.compile(">>([0-9]+)")
LINEQUOTES = re.compile("^>(.+)$", flags=re.MULTILINE)
def parse(text, doquotes=True):
output = TOKENS.sub("\\2", text)
objects = list()
offset = 0
for token in TOKENS.finditer(text):
directive = token.group(1).lower()
start = token.start() - offset
end = start + len(token.group(2))
offset += len(directive) + 6
if directive in COLORS:
objects.append(["color", start, end, directive])
else:
objects.append([directive, start, end])
objects += [["linequote", m.start(), m.end()]
for m in LINEQUOTES.finditer(output)]
if doquotes:
objects += [["quote", m.start(), m.end(), int(m.group(1))]
for m in QUOTES.finditer(output)]
return output, objects