~nhanb/mcross-devel

Support alt text for preformatted text v1 PROPOSED

Frederick Yin: 1
 Support alt text for preformatted text

 3 files changed, 15 insertions(+), 5 deletions(-)
> > 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? 
I guess not, but I can quote solderpunk's summary on spec updates:

----- BEGIN QUOTATION -----
SUMMARY OF CHANGES:

* A gemini:// URI scheme is formally defined for the first time,
explicitly disallowing the use of the userinfo subcomponent of the
authority component.
* The client certificate system has been substantially simplified, with
transient certificates no long a formally defined concept with
prescribed behaviour.
* A lightweight notion of alt text for pre-formatted content has been
introduced as a tentative step toward fixing the worst search and
accessibility related problems with pre-formatted non-textual content.
No special structures or semantics for alt text are defined - if a
convention for doing this organically arises in the community it may
become a recommended best practice.
* A few minor corrections and tidyups.
----- END OF QUOTATION -----
> Implementing an evolving spec with neither a changelog nor a diff is... not fun.
Do you subscribe to the Gemini list? If so you can catch up with the important updates*.
Next
 > 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
View this thread in the archives

[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):
    pass
    __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:]
            else:
                nodes.append(PreformattedNode(preformatted))
                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
[tool.poetry]
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:
-- 
2.27.0
 > 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.