Received: from mail.macaw.me (mail.macaw.me [45.79.76.58]) by mail-b.sr.ht (Postfix) with ESMTPS id AB353FF0A9 for <~nhanb/mcross-devel@lists.sr.ht>; Sun, 14 Jun 2020 14:17:15 +0000 (UTC) Authentication-Results: mail-b.sr.ht; dkim=pass (2048-bit key) header.d=macaw.me header.i=@macaw.me header.b=olW7EsaK Received: from localhost (unknown [153.99.181.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.macaw.me (Postfix) with ESMTPSA id 231323AF34F; Sun, 14 Jun 2020 14:17:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=macaw.me; s=main; t=1592144234; bh=uQKJmcvcT/oVBwFPoxnpVH5gs7AYhd0rtZgvM+bNHo0=; h=From:To:Cc:Subject:Date; b=olW7EsaK4vkZlAknPx0X3LxHck0o7D1XfB8JgLLvFEOL0mlFZNh9uCnVz/+1ktlTK WSA1GN9zoFkcoNwTAp/UJRLvWz+pA7kulpJHcPEw+h9idkd6h6AC8FC8ibQuTGwWJZ 3woitdgCLobxsa7+pq1eggWdRERt2seAM/vaaokY6QwBYPbQw4I8B3loUXkVrsg4RJ 9ETSqLt5L/tBFjsTnjdzMnPlMmVBspktTG9B9NpVuFo38GC9jON8jVpdXm6kuBCpU0 UvcH2pXDQ793ymrG5QHnY8d2fhc1MhC0k88uiGO7HPKJKSGT3caGLiGYTpUiax9KnB NXBp+OqUHticg== From: Frederick Yin To: ~nhanb/mcross-devel@lists.sr.ht Cc: Frederick Yin Subject: [PATCH] Support alt text for preformatted text Date: Sun, 14 Jun 2020 22:16:54 +0800 Message-Id: <20200614141653.12420-1-fkfd@macaw.me> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable In Gemini's new, pending spec: =3D> 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 =3D=3D "```"` 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): =20 =20 class PreformattedNode(GeminiNode): - pass + __slots__ =3D ("alt",) + alt: str + + def __init__(self, text, alt): + self.text =3D text + self.alt =3D alt # optional alt-text for preformatted text + + def __repr__(self): + return f"{self.__class__.__name__}: {self.alt.__repr__()}" =20 =20 def parse(text): @@ -63,15 +71,17 @@ def parse(text): """ nodes =3D [] preformatted =3D None + preformatted_alt =3D "" =20 for line in text.strip().split(NEWLINE): =20 - if line =3D=3D "```": + if line.startswith("```"): if preformatted is None: # start preformatted mode preformatted =3D "" + preformatted_alt =3D line[3:] else: - nodes.append(PreformattedNode(preformatted)) + nodes.append(PreformattedNode(preformatted, preformatted= _alt)) preformatted =3D None =20 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 =3D """\ =20 ## Codes =20 -``` +```a snippet from pyproject.toml [tool.poetry] name =3D "mcross" version =3D "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: --=20 2.27.0