From d82ffce504788c85d03504b18183d410c2c51cac Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 6 May 2026 21:11:49 +0200 Subject: [PATCH] Fixed markdown-to-micron formatting and syntax highlighting being weird in some cases --- RNS/Utilities/rngit/highlight.py | 45 +++++++++++++++++++++++++++----- RNS/Utilities/rngit/util.py | 4 +++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/RNS/Utilities/rngit/highlight.py b/RNS/Utilities/rngit/highlight.py index 0b8b0847..928f2fe6 100644 --- a/RNS/Utilities/rngit/highlight.py +++ b/RNS/Utilities/rngit/highlight.py @@ -218,8 +218,12 @@ class MicronFormatter: output_parts = [] prev_was_dot = False + last_ended_with_break = True for ttype, value in tokensource: is_dot = (str(ttype) == "Token.Operator" and value == ".") + ends_with_break = value.endswith("\n") + + # RNS.log(f" <{value}!") # If previous token was a dot and this is a Name, treat as attribute/function call # TODO: Improve this if we can check next token as parantheses or something. @@ -241,19 +245,46 @@ class MicronFormatter: else: ilb = "" if escaped.endswith("\n"): tlb = "\n"; escaped = escaped[:-1] else: tlb = "" - output_parts.append(f"{ilb}`FT{color}{escaped}`f{tlb}") + + if len(escaped): output = f"{ilb}`FT{color}{escaped}`f{tlb}" + else: output = f"{ilb}{tlb}" + # RNS.log(f"c>{ilb}{escaped}{tlb}!") + + output_parts.append(output) - else: output_parts.append(self._escape_value(value)) + else: + escaped = self._escape_value(value) + if "\n" in escaped: + parts = [] + splitl = escaped.splitlines() + if len(splitl) > 1: + for line in splitl: + if line.startswith("-"): l = f"\\{line}" + elif line.startswith(">"): l = f"\\{line}" + elif line.startswith("<"): l = f"\\{line}" + else: l = line + parts.append(l) + trmpart = "\n" if escaped.endswith("\n") else "" + escaped = "\n".join(parts)+trmpart + + elif last_ended_with_break: + if escaped.startswith("-"): escaped = f"\\{escaped}" + elif escaped.startswith(">"): escaped = f"\\{escaped}" + elif escaped.startswith("<"): escaped = f"\\{escaped}" + + # RNS.log(f"p>{escaped}!") + output_parts.append(escaped) prev_was_dot = is_dot + last_ended_with_break = ends_with_break output = "".join(output_parts) - final_output = "" - for line in output.splitlines(): - if line.startswith(">"): line = f"`>{line}" - final_output += f"{line}\n" + # final_output = "" + # for line in output.splitlines(): + # if line.startswith(">"): line = f"`>{line}" + # final_output += f"{line}\n" - outfile.write(final_output) + outfile.write(output) def _get_color_key_for_token(self, ttype): token_parts = [] diff --git a/RNS/Utilities/rngit/util.py b/RNS/Utilities/rngit/util.py index 51c11bb0..a4cd385f 100644 --- a/RNS/Utilities/rngit/util.py +++ b/RNS/Utilities/rngit/util.py @@ -177,6 +177,10 @@ class MarkdownToMicron: for line in lines: is_fence, lang_hint = self._detect_code_fence(line) + + if line.startswith("-") and not line.startswith("---") and not line.startswith("- "): line = f"\\{line}" + if line.startswith(">"): line = f"\\{line}" + if line.startswith("<"): line = f"\\{line}" if is_fence: # Flush any pending structures before code fence