summary refs log tree commit diff
path: root/mime
diff options
context:
space:
mode:
authorZach DeCook <zachdecook@librem.one>2023-01-14 13:05:07 -0500
committerZach DeCook <zachdecook@librem.one>2023-01-14 13:05:07 -0500
commit67bccc9e1a39ac2749806a37cb53b23322645083 (patch)
tree18b5b5fac406c01f8b1a6bf4bd4f94217ac08ba8 /mime
parent74c1b89b728d7d1013b714adf39737cd27e045f3 (diff)
downloadbrowset-67bccc9e1a39ac2749806a37cb53b23322645083.tar.gz
Mimetypes: Handle source code syntax highlighting
Diffstat (limited to 'mime')
-rw-r--r--mime/highlightedcode.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/mime/highlightedcode.py b/mime/highlightedcode.py
new file mode 100644
index 0000000..63defef
--- /dev/null
+++ b/mime/highlightedcode.py
@@ -0,0 +1,45 @@
+from textual.widgets import Static
+from textual.containers import Container
+from rich.syntax import Syntax
+
+# mime list from gemini://geminispace.info/statistics
+mimetolexer = {
+    "text/x-diff": "diff",
+    "text/x-csrc": "c",
+    "text/x-python": "python",
+    "text/markdown": "markdown",
+    "text/x-patch": "diff",
+    "text/x-c++src": "cpp",
+    "text/x-pascal": "pascal",
+    "text/x-c++hdr": "cpp",
+    "text/x-go": "go",
+    "text/x-rust": "rust",
+    "text/css": "css",
+    "text/x-php": "php",
+    # Hopefully, future handlers will take care of these mimes first.
+    "application/json": "json",
+    "text/html": "html",
+    "application/xml": "xml",
+    "application/atom+xml": "xml",
+    "text/xml": "xml",
+    "image/svg+xml": "xml",
+}
+
+class HighlightedCode(Static):
+    """Plaintext widget."""
+
+    def __init__(self, fp, id, mime):
+        super().__init__(id=id)
+        self.addblock(fp, mime)
+
+    def addblock(self, fp, mime):
+        code = ""
+        # Read the whole file first, then render it all.
+        # Hopefully not a problem, because source code should have terminal length.
+        for lin in fp:
+            line = lin
+            if type(line) is bytes:
+                line = line.decode("UTF-8")
+            code += line
+        lexer = mimetolexer[mime]
+        self.mount(Static(Syntax(code,lexer=lexer)))