Support alt text for preformatted text v1 PROPOSED

Frederick Yin: 1
 Support alt text for preformatted text

 3 files changed, 15 insertions(+), 5 deletions(-)
> Do you subscribe to the Gemini list? If so you can catch up with the 
important updates*.

Right, I forgot about that, thanks. Better than nothing I guess.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~nhanb/mcross-devel/patches/11187/mbox | git am -3
Learn more about email & git

[PATCH] Support alt text for preformatted text Export this patch

In Gemini's new, pending spec:

=> gemini://gemini.circumlunar.space/docs/specification-modified.gmi

...section 5.4.3:

> Any text following the leading "```" of a preformat toggle line which
> toggles preformatted mode on MAY be interpreted by the client as "alt
> text" pertaining to the preformatted text lines which follow the
> toggle line.

Since McRoss didn't adhere to the spec in the first place:

> Any line whose first three characters are "```"

"first three characters" means simply `if line == "```"` won't do. I
resolved this as a side effect.

This patch may be postponed until the modified spec takes effect.
 src/mcross/document.py  | 16 +++++++++++++---
 src/mcross/gui/model.py |  2 +-
 src/mcross/gui/view.py  |  2 +-
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/mcross/document.py b/src/mcross/document.py
index 821f673..f285642 100644
--- a/src/mcross/document.py
+++ b/src/mcross/document.py
@@ -54,7 +54,15 @@ class LinkNode(GeminiNode):

class PreformattedNode(GeminiNode):
    __slots__ = ("alt",)
    alt: str

    def __init__(self, text, alt):
        self.text = text
        self.alt = alt  # optional alt-text for preformatted text

    def __repr__(self):
        return f"{self.__class__.__name__}: {self.alt.__repr__()}"

def parse(text):
@@ -63,15 +71,17 @@ def parse(text):
    nodes = []
    preformatted = None
    preformatted_alt = ""

    for line in text.strip().split(NEWLINE):

        if line == "```":
        if line.startswith("```"):
            if preformatted is None:
                # start preformatted mode
                preformatted = ""
                preformatted_alt = line[3:]
                nodes.append(PreformattedNode(preformatted, preformatted_alt))
                preformatted = None

        elif preformatted is not None:
diff --git a/src/mcross/gui/model.py b/src/mcross/gui/model.py
index 23667bd..98a5823 100644
--- a/src/mcross/gui/model.py
+++ b/src/mcross/gui/model.py
@@ -26,7 +26,7 @@ DEMO_TEXT = """\

## Codes

```a snippet from pyproject.toml
name = "mcross"
version = "0.1.0"
diff --git a/src/mcross/gui/view.py b/src/mcross/gui/view.py
index 7e5ed33..0bc8f23 100644
--- a/src/mcross/gui/view.py
+++ b/src/mcross/gui/view.py
@@ -266,7 +266,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:
> In Gemini's new, pending spec

Aw crap, looks like it's promoted to official spec now. Do you know if 
they maintain some version controlled repo of the spec? Implementing an 
evolving spec with neither a changelog nor a diff is... not fun.

There's no excuse for missing the "Any line whose first three characters 
are ```" though. My bad.

Guess I'll comb through the new spec.