Repos / mcross / 760474a77f
commit 760474a77f29672be3d1c258fdd806bf39123bff
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Wed Jun 17 20:59:03 2020 +0700
correctly render text/* mime type
diff --git a/pyproject.toml b/pyproject.toml
index cf215bc..430e664 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mcross"
-version = "0.5.14"
+version = "0.5.15"
description = "Do you remember www?"
authors = ["nhanb <hi@imnhan.com>"]
license = "MIT"
diff --git a/src/mcross/gui/controller.py b/src/mcross/gui/controller.py
index ca6b684..f65ec4a 100644
--- a/src/mcross/gui/controller.py
+++ b/src/mcross/gui/controller.py
@@ -161,7 +161,9 @@ async def load_page(self, url: GeminiUrl):
)
if resp.status.startswith("2"):
- await self.put_gui_op(self.model.update_content, resp.body.decode())
+ await self.put_gui_op(
+ self.model.update_content, resp.body.decode(), resp.meta
+ )
else:
await self.put_gui_op(
self.model.update_content,
@@ -172,5 +174,6 @@ async def load_page(self, url: GeminiUrl):
resp.body.decode() if resp.body else "",
]
),
+ "text/plain",
)
return resp
diff --git a/src/mcross/gui/model.py b/src/mcross/gui/model.py
index 23667bd..3dedebb 100644
--- a/src/mcross/gui/model.py
+++ b/src/mcross/gui/model.py
@@ -77,15 +77,18 @@ class Model:
plaintext = ""
gemini_nodes = None
history: History
+ mime_type = ""
def __init__(self):
self.history = History()
- self.update_content(DEMO_TEXT)
+ self.update_content(DEMO_TEXT, "text/gemini")
- def update_content(self, plaintext):
+ def update_content(self, plaintext, mime_type):
self.plaintext = plaintext
+ self.mime_type = mime_type
self.gemini_nodes = []
- try:
- self.gemini_nodes = document.parse(plaintext)
- except Exception:
- print("Invalid gemini document!")
+ if mime_type == "text/gemini":
+ try:
+ self.gemini_nodes = document.parse(plaintext)
+ except Exception:
+ print("Invalid gemini document!")
diff --git a/src/mcross/gui/view.py b/src/mcross/gui/view.py
index 7e5ed33..f61f14a 100644
--- a/src/mcross/gui/view.py
+++ b/src/mcross/gui/view.py
@@ -181,6 +181,7 @@ def on_ctrl_l(ev):
text.tag_bind("link", "<Leave>", self._on_link_leave)
text.tag_bind("link", "<Button-1>", self._on_link_click)
text.tag_config("pre", font=(mono_font, 13))
+ text.tag_config("plaintext", font=(mono_font, 13))
text.tag_config("listitem", foreground="#64c664" if dark else "#044604")
base_heading_font = font.Font(font=text["font"])
@@ -245,15 +246,21 @@ def render_page(self):
self.address_bar.delete(0, "end")
self.address_bar.insert(0, current_url.without_protocol())
- # Update viewport
+ self.render_viewport()
+
+ def render_viewport(self):
self.text.delete("1.0", "end")
- if not self.model.gemini_nodes:
- self.text.insert("end", self.model.plaintext)
- else:
+ if self.model.mime_type == "text/gemini":
for node in self.model.gemini_nodes:
render_node(node, self.text)
# delete final trailing newline:
self.text.delete("insert-1c", "insert")
+ elif self.model.mime_type.startswith("text/"):
+ self.text.insert("end", self.model.plaintext, ("plaintext",))
+ else:
+ self.text.insert(
+ "end", f"Unsupported MIME type: {self.model.mime_type}", ("plaintext",)
+ )
def render_node(node: GeminiNode, widget: Text):