Repos / mcross / 0d54357bf8
commit 0d54357bf82869d195fedb9c1c5920e5a242b057
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Mon Nov 2 11:25:46 2020 +0700
fix preformatted block with alt text
I really need to do proper parser tests one of these days...
diff --git a/src/mcross/document.py b/src/mcross/document.py
index 821f673..d44c698 100644
--- a/src/mcross/document.py
+++ b/src/mcross/document.py
@@ -54,7 +54,13 @@ def __repr__(self):
class PreformattedNode(GeminiNode):
- pass
+ __slots__ = ("alt", "text")
+ alt: str
+ text: str
+
+ def __init__(self, alt, text):
+ self.alt = alt
+ self.text = text
def parse(text):
@@ -66,18 +72,21 @@ def parse(text):
for line in text.strip().split(NEWLINE):
- if line == "```":
+ if line.startswith("```"):
if preformatted is None:
# start preformatted mode
- preformatted = ""
+ preformatted = {"alt": line[3:], "text": ""}
else:
- nodes.append(PreformattedNode(preformatted))
+ # maybe FIXME: in case a closing ``` has trailing text, that text
+ # will be thrown away. Who cares since that would probably
+ # mean malformed gemini anyway.
+ nodes.append(PreformattedNode(**preformatted))
preformatted = None
elif preformatted is not None:
- if len(preformatted) > 0:
- preformatted += "\n"
- preformatted += line
+ if len(preformatted["text"]) > 0:
+ preformatted["text"] += "\n"
+ preformatted["text"] += line
elif line.startswith("=> "):
match = LINK_LINE_PATTERN.match(line)
diff --git a/src/mcross/gui/view.py b/src/mcross/gui/view.py
index bd0fa34..1a8ae95 100644
--- a/src/mcross/gui/view.py
+++ b/src/mcross/gui/view.py
@@ -3,16 +3,8 @@
from tkinter import Text, Tk, font, ttk
from .. import conf
-from ..document import (
- GeminiNode,
- H1Node,
- H2Node,
- H3Node,
- LinkNode,
- ListItemNode,
- PreformattedNode,
- TextNode,
-)
+from ..document import (GeminiNode, H1Node, H2Node, H3Node, LinkNode,
+ ListItemNode, PreformattedNode, TextNode)
from .model import Model
from .widgets import AltButton, McEntry, ReadOnlyText
@@ -271,7 +263,7 @@ def render_node(node: GeminiNode, widget: Text):
if node.name:
widget.insert("end", f" {node.name}")
elif nodetype is PreformattedNode:
- widget.insert("end", f"```\n{node.text}\n```", ("pre",))
+ widget.insert("end", f"```{node.alt}\n{node.text}\n```", ("pre",))
elif nodetype is ListItemNode:
widget.insert("end", node.text, ("listitem",))
elif nodetype is H1Node: