Work on micron parser

pull/5/head
Mark Qvist 2021-07-05 15:32:59 +02:00
parent 72f623293e
commit 1564fcec03
2 changed files with 51 additions and 16 deletions

View File

@ -185,11 +185,24 @@ Conversations in Nomad Network
TOPIC_MARKUP = '''>Markup TOPIC_MARKUP = '''>Markup
Nomad Network supports a simple and functional markup language called micron. It has a lean markup structure that adds very little overhead, and is still readable as plain text, but offers basic formatting and text structuring, ideal for displaying in a terminal. Nomad Network supports a simple and functional markup language called micron. It has a lean markup structure that adds very little overhead, and is still readable as plain text, but offers basic formatting and text structuring, ideal for displaying in a terminal.
Lorem ipsum dolor sit amet. `cLorem ipsum dolor sit amet.
`a
>>Encoding >>Encoding
`F222`BdddAll uM source files are encoded as UTF-8, and clients supporting uM display should support UTF-8. `F222`BdddAll uM source files are encoded as UTF-8, and clients supporting uM display should support UTF-8.
`` ``
`B33f
You can change background ...
`B393
`r... and foreground colors
`a
`b
>>>Sections and `F900Headings`f >>>Sections and `F900Headings`f
You can define an arbitrary number of sections and sub-sections, each with their own heading You can define an arbitrary number of sections and sub-sections, each with their own heading
@ -203,7 +216,7 @@ If no heading text is defined, the section will appear as a sub-section without
<- <-
Horizontal dividers can be inserted Horizontal dividers can be inserted
Text `F2cccan`f be `_underlined`_, `!bold`! or `*italic`*. You `F000`B2cccan`b`f also `_`*`!combine formatting``! Text can be `_underlined`_, `!bold`! or `*italic`*. You can also `_`*`!`B5d5`F222combine`f`b`_ `_`Ff00f`Ff80o`Ffd0r`F9f0m`F0f2a`F0fdt`F07ft`F43fi`F70fn`Fe0fg``!
''' '''

View File

@ -20,7 +20,6 @@ INDENT_RIGHT = 1
def markup_to_attrmaps(markup): def markup_to_attrmaps(markup):
attrmaps = [] attrmaps = []
global_style = ""
state = { state = {
"depth": 0, "depth": 0,
@ -32,7 +31,9 @@ def markup_to_attrmaps(markup):
"italic": False, "italic": False,
"strikethrough": False, "strikethrough": False,
"blink": False, "blink": False,
} },
"default_align": "left",
"align": "left",
} }
# Split entire document into lines for # Split entire document into lines for
@ -45,11 +46,8 @@ def markup_to_attrmaps(markup):
else: else:
display_widget = urwid.Text("") display_widget = urwid.Text("")
if global_style == "":
global_style = "plain"
if display_widget != None: if display_widget != None:
attrmap = urwid.AttrMap(display_widget, global_style) attrmap = urwid.AttrMap(display_widget, make_style(state))
attrmaps.append(attrmap) attrmaps.append(attrmap)
return attrmaps return attrmaps
@ -99,10 +97,13 @@ def parse_line(line, state):
output = make_output(state, line) output = make_output(state, line)
if state["depth"] == 0: if output != None:
return urwid.Text(output) if state["depth"] == 0:
return urwid.Text(output, align=state["align"])
else:
return urwid.Padding(urwid.Text(output, align=state["align"]), left=left_indent(state), right=right_indent(state))
else: else:
return urwid.Padding(urwid.Text(output), left=left_indent(state), right=right_indent(state)) return None
def left_indent(state): def left_indent(state):
return (state["depth"]-1)*SECTION_INDENT return (state["depth"]-1)*SECTION_INDENT
@ -139,7 +140,7 @@ def make_style(state):
if italic: if italic:
format_string += ",italics" format_string += ",italics"
name = ""+fg+","+bg+","+format_string name = "micron_"+fg+"_"+bg+"_"+format_string
if not name in SYNTH_STYLES: if not name in SYNTH_STYLES:
screen = nomadnet.NomadNetworkApp.get_shared_instance().ui.screen screen = nomadnet.NomadNetworkApp.get_shared_instance().ui.screen
screen.register_palette_entry(name, low_color(fg)+format_string,low_color(bg),mono_color(fg, bg)+format_string,high_color(fg)+format_string,high_color(bg)) screen.register_palette_entry(name, low_color(fg)+format_string,low_color(bg),mono_color(fg, bg)+format_string,high_color(fg)+format_string,high_color(bg))
@ -172,7 +173,7 @@ def make_output(state, line):
elif c == "f": elif c == "f":
state["fg_color"] = "default" state["fg_color"] = "default"
elif c == "B": elif c == "B":
if len(line) > i+4: if len(line) >= i+4:
color = line[i+1:i+4] color = line[i+1:i+4]
state["bg_color"] = color state["bg_color"] = color
skip = 3 skip = 3
@ -184,7 +185,24 @@ def make_output(state, line):
state["formatting"]["italic"] = False state["formatting"]["italic"] = False
state["fg_color"] = "default" state["fg_color"] = "default"
state["bg_color"] = "default" state["bg_color"] = "default"
elif c == "c":
if state["align"] != "center":
state["align"] = "center"
else:
state["align"] = state["default_align"]
elif c == "l":
if state["align"] != "left":
state["align"] = "left"
else:
state["align"] = state["default_align"]
elif c == "r":
if state["align"] != "right":
state["align"] = "right"
else:
state["align"] = state["default_align"]
elif c == "a":
state["align"] = state["default_align"]
mode = "text" mode = "text"
if len(part) > 0: if len(part) > 0:
output.append(make_part(state, part)) output.append(make_part(state, part))
@ -199,6 +217,10 @@ def make_output(state, line):
part += c part += c
if i == len(line)-1: if i == len(line)-1:
output.append(make_part(state, part)) if len(part) > 0:
output.append(make_part(state, part))
return output if len(output) > 0:
return output
else:
return None