From 1564fcec038f0a1b4d0504ad0e777d77b79ab487 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Mon, 5 Jul 2021 15:32:59 +0200 Subject: [PATCH] Work on micron parser --- nomadnet/ui/textui/Guide.py | 17 ++++++++-- nomadnet/ui/textui/MarkupParser.py | 50 +++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/nomadnet/ui/textui/Guide.py b/nomadnet/ui/textui/Guide.py index 893a84a..45c79ba 100644 --- a/nomadnet/ui/textui/Guide.py +++ b/nomadnet/ui/textui/Guide.py @@ -185,11 +185,24 @@ Conversations in Nomad Network 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. -Lorem ipsum dolor sit amet. +`cLorem ipsum dolor sit amet. +`a >>Encoding `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 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 -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``! ''' diff --git a/nomadnet/ui/textui/MarkupParser.py b/nomadnet/ui/textui/MarkupParser.py index 9aeec63..d2ff7db 100644 --- a/nomadnet/ui/textui/MarkupParser.py +++ b/nomadnet/ui/textui/MarkupParser.py @@ -20,7 +20,6 @@ INDENT_RIGHT = 1 def markup_to_attrmaps(markup): attrmaps = [] - global_style = "" state = { "depth": 0, @@ -32,7 +31,9 @@ def markup_to_attrmaps(markup): "italic": False, "strikethrough": False, "blink": False, - } + }, + "default_align": "left", + "align": "left", } # Split entire document into lines for @@ -45,11 +46,8 @@ def markup_to_attrmaps(markup): else: display_widget = urwid.Text("") - if global_style == "": - global_style = "plain" - if display_widget != None: - attrmap = urwid.AttrMap(display_widget, global_style) + attrmap = urwid.AttrMap(display_widget, make_style(state)) attrmaps.append(attrmap) return attrmaps @@ -99,10 +97,13 @@ def parse_line(line, state): output = make_output(state, line) - if state["depth"] == 0: - return urwid.Text(output) + if output != None: + 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: - return urwid.Padding(urwid.Text(output), left=left_indent(state), right=right_indent(state)) + return None def left_indent(state): return (state["depth"]-1)*SECTION_INDENT @@ -139,7 +140,7 @@ def make_style(state): if italic: format_string += ",italics" - name = ""+fg+","+bg+","+format_string + name = "micron_"+fg+"_"+bg+"_"+format_string if not name in SYNTH_STYLES: 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)) @@ -172,7 +173,7 @@ def make_output(state, line): elif c == "f": state["fg_color"] = "default" elif c == "B": - if len(line) > i+4: + if len(line) >= i+4: color = line[i+1:i+4] state["bg_color"] = color skip = 3 @@ -184,7 +185,24 @@ def make_output(state, line): state["formatting"]["italic"] = False state["fg_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" if len(part) > 0: output.append(make_part(state, part)) @@ -199,6 +217,10 @@ def make_output(state, line): part += c if i == len(line)-1: - output.append(make_part(state, part)) + if len(part) > 0: + output.append(make_part(state, part)) - return output \ No newline at end of file + if len(output) > 0: + return output + else: + return None \ No newline at end of file