Repos / mcross / 8cf80965be
commit 8cf80965be709a9c6fc2e5cd5e6a5afdc617624e
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Thu May 14 20:24:35 2020 +0700

    basic preformatted & link cosmetics

diff --git a/README.md b/README.md
index 4e04916..026f941 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,16 @@
-![do you remember wwww?](https://p.caophim.net/81.webp)
+McCross is a WIP [gemini://](https://gemini.circumlunar.space/) browser
+written in python and tkinter.
 
-McCross is a WIP [gemini://](https://gemini.circumlunar.space/) browser,
-built in loving memory of a simpler, more innocent time.
-
-Developed against gemini://gemini.circumlunar.space/ because apparently
-it's the only one with a valid, CA-approved TLS cert.
+It's developed against gemini://gemini.circumlunar.space/ because apparently
+that's the only server with a valid, CA-approved TLS cert.
 
 It currently looks like this:
 
-![](https://p.caophim.net/82.png)
+![](https://p.caophim.net/84.png)
+
+Happy-path fetching and rendering already works.
+Clicking on links isn't implemented yet though.
+I'm still figuring out stuff as I go.
 
 # Installation
 
diff --git a/pyproject.toml b/pyproject.toml
index d2e98ba..97c39b0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "mccross"
-version = "0.1.0"
+version = "0.1.1"
 description = "Do you remember www?"
 authors = ["nhanb <hi@imnhan.com>"]
 license = "MIT"
diff --git a/src/mccross/document.py b/src/mccross/document.py
index e27d4ad..37af176 100644
--- a/src/mccross/document.py
+++ b/src/mccross/document.py
@@ -73,9 +73,3 @@ def parse(text):
             nodes.append(TextNode(line))
 
     return nodes
-
-
-def test():
-    return parse(
-        """# Project Gemini\n\n## Overview\n\nGemini is a new internet protocol which:\n\n* Is heavier than gopher\n* Is lighter than the web\n* Will not replace either\n* Strives for maximum power to weight ratio\n* Takes user privacy very seriously\n\n## Resources\n\n=> docs/\tGemini documentation\n=> software/\tGemini software\n=> servers/\tKnown Gemini servers\n=> gemini://gus.guru/\tGemini Universal Search engine\n=> https://lists.orbitalfox.eu/listinfo/gemini\tGemini mailing list\n=> https://portal.mozz.us/?url=gemini%3A%2F%2Fgemini.circumlunar.space%2F&fmt=fixed\tGemini-to-web proxy service\n=> https://proxy.vulpes.one/gemini/gemini.circumlunar.space\tAnother Gemini-to-web proxy service\n=> gemini://gemini.conman.org/test/torture/\tGemini client torture test\n\n## Geminispace aggregator (experimental!)\n\n=> capcom/\tCAPCOM\n\n## Free Gemini hosting\n\n=> users/\tUsers with Gemini content on this server\n```\nfooo\nbar\n```\nBye.\n"""
-    )
diff --git a/src/mccross/gui/model.py b/src/mccross/gui/model.py
index d351890..80b4678 100644
--- a/src/mccross/gui/model.py
+++ b/src/mccross/gui/model.py
@@ -1,10 +1,46 @@
 from .. import document
 
+DEMO_TEXT = """\
+# Welcome to McCross Browser
+
+## List
+
+* おぼえていますか 目と目が会った時を
+* Do you remember? The time when our eyes first met?
+* おぼえていますか 手と手触れ会った時
+* Do you remember? The time when our hands first touched?
+
+## Codes
+
+```
+[tool.poetry]
+name = "mccross"
+version = "0.1.0"
+description = "Do you remember www?"
+authors = ["nhanb <hi@imnhan.com>"]
+license = "MIT"
+```
+
+## Links
+
+=> gemini.circumlunar.space/docs/	Gemini documentation
+=> gemini://gemini.circumlunar.space/software/ Gemini software
+=> gemini.circumlunar.space/servers/     Known Gemini servers
+=> gemini://gus.guru/	Gemini Universal Search engine
+=> https://lists.orbitalfox.eu/listinfo/gemini	Gemini mailing list
+=> https://portal.mozz.us/?url=gemini%3A%2F%2Fgemini.circumlunar.space%2F&fmt=fixed	Gemini-to-web proxy service
+=> https://proxy.vulpes.one/gemini/gemini.circumlunar.space	Another Gemini-to-web proxy service
+=> gemini://gemini.conman.org/test/torture/	Gemini client torture test
+"""
+
 
 class Model:
-    plaintext = "Nothing to see here... yet."
+    plaintext = ""
     gemini_nodes = None
 
+    def __init__(self):
+        self.update_content(DEMO_TEXT)
+
     def update_content(self, plaintext):
         self.plaintext = plaintext
         self.gemini_nodes = []
diff --git a/src/mccross/gui/view.py b/src/mccross/gui/view.py
index 8625556..6565cba 100644
--- a/src/mccross/gui/view.py
+++ b/src/mccross/gui/view.py
@@ -1,6 +1,7 @@
 import sys
 from tkinter import Text, Tk, font, ttk
 
+from ..document import GeminiNode, LinkNode, PreformattedNode, TextNode
 from .model import Model
 from .widgets import ReadOnlyText
 
@@ -78,9 +79,17 @@ def __init__(self, root: Tk, model: Model):
                 "Times",
             ]
         )
+        mono_font = pick_font(["Ubuntu Mono", "Consolas", "Courier"])
         text.config(
             font=(text_font, 13), bg="#fff8dc", fg="black", padx=5, pady=5,
         )
+        text.tag_config("link", foreground="blue", underline=1)
+        text.tag_config(
+            "pre",
+            font=(mono_font, 13),
+            # background="#ffe4c4",
+            # selectbackground=text.cget("selectbackground"),
+        )
         text.pack(side="left", fill="both", expand=True)
 
         text_scrollbar = ttk.Scrollbar(viewport, command=text.yview)
@@ -105,6 +114,21 @@ def render_page(self):
         if not self.model.gemini_nodes:
             self.text.insert("end", self.model.plaintext)
         else:
-            self.text.insert(
-                "end", "\n".join(str(node) for node in self.model.gemini_nodes)
-            )
+            for node in self.model.gemini_nodes:
+                render_node(node, self.text)
+
+
+def render_node(node: GeminiNode, widget: Text):
+    nodetype = type(node)
+    if nodetype is TextNode:
+        widget.insert("end", node.text + "\n")
+    elif nodetype is LinkNode:
+        widget.insert("end", "=> ")
+        widget.insert("end", f"{node.url}", ("link",))
+        if node.name:
+            widget.insert("end", f" {node.name}")
+        widget.insert("end", "\n")
+    elif nodetype is PreformattedNode:
+        widget.insert("end", f"```\n{node.text}\n```\n", ("pre",))
+    else:
+        widget.insert("end", node.text + "\n")