~earboxer/browset

74c1b89b728d7d1013b714adf39737cd27e045f3 — Zach DeCook 1 year, 1 month ago d236dd9
mime: Support plaintext format as well
4 files changed, 26 insertions(+), 3 deletions(-)

A .gitignore
M browset.py
R gemtext.py => mime/gemtext.py
A mime/plaintext.py
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
__pycache__

M browset.py => browset.py +7 -2
@@ 4,7 4,8 @@ from textual.widgets import Input, Button, Static, Footer
from textual.containers import Container
from rich.markdown import Markdown
from textual.binding import Binding
from gemtext import Gemtext
from mime.gemtext import Gemtext
from mime.plaintext import Plaintext
from protocol.gemini import GeminiProtocol

class Browset(App):


@@ 69,7 70,11 @@ class Browset(App):
        self.url = url
        (mime, fp) = GeminiProtocol.get(url)
        self.query_one("#content").remove()
        self.mount(Gemtext(fp=fp, id="content"))
        if "text/gemini" in mime:
          content = Gemtext(fp=fp, id="content")
        else:
          content = Plaintext(fp=fp, id="content")
        self.mount(content)

if __name__ == "__main__":
    app = Browset()

R gemtext.py => mime/gemtext.py +2 -1
@@ 1,6 1,7 @@
from textual.widgets import Button, Static
from textual.app import ComposeResult
from textual.containers import Container
from rich.text import Text

class Gemtext(Static):
    """Gemtext widget."""


@@ 26,7 27,7 @@ class Gemtext(Static):
            elif line.startswith("=>"):
                path = line[2:].lstrip().split(' ')[0]
                text = ' '.join(line[2:].lstrip().split(' ')[1:])
                self.mount(Button(text or path, name=path))
                self.mount(Button(Text(text or path,overflow="fold"), name=path))
            elif line.startswith("#"):
             count = line.split(' ')[0][0:3].count("#")
             w = Static(line.lstrip("#"), markup=False, classes="header h"+str(count))

A mime/plaintext.py => mime/plaintext.py +16 -0
@@ 0,0 1,16 @@
from textual.widgets import Static

class Plaintext(Static):
    """Plaintext widget."""

    def __init__(self, fp, id):
        super().__init__(id=id)
        self.addlines(fp)

    def addlines(self, fp):
        for lin in fp:
            line = lin
            if type(line) is bytes:
                line = line.decode("UTF-8")
            line = line.rstrip("\r\n")
            self.mount(Static(line, markup=False))