~earboxer/browset

03c758af3eaebd6b7c18e1e595d49b0552eb5278 — Zach DeCook 1 year, 1 month ago 67bccc9
browset: Support data protocol
2 files changed, 19 insertions(+), 1 deletions(-)

M browset.py
A protocol/data.py
M browset.py => browset.py +10 -1
@@ 8,6 8,7 @@ from mime.gemtext import Gemtext
from mime.plaintext import Plaintext
from mime.highlightedcode import HighlightedCode,mimetolexer
from protocol.gemini import GeminiProtocol
from protocol.data import DataProtocol

class Browset(App):
    url = ""


@@ 69,8 70,16 @@ class Browset(App):
        if histore:
            self.history.append(self.url)
        self.url = url
        (mime, fp) = GeminiProtocol.get(url)
        protocol = url.split(":")[0]
        if protocol == "gemini":
          (mime, fp) = GeminiProtocol.get(url)
        elif protocol == "data":
          (mime, fp) = DataProtocol.get(url)
        else:
          (mime, fp) = ("error", ["Unsupported protocol: " + protocol])

        self.query_one("#content").remove()

        if "text/gemini" in mime:
          content = Gemtext(fp=fp, id="content")
        elif mime in mimetolexer:

A protocol/data.py => protocol/data.py +9 -0
@@ 0,0 1,9 @@
from base64 import b64decode
class DataProtocol():
    def get(url):
        mime = url.split(':')[1].split(',')[0].split(';')[0]
        base64 = 'base64' in url.split(',')[0].split(';')
        content = ','.join(url.split(',')[1:])
        if base64:
            content = b64decode(content)
        return (mime or 'text/plain', [content])