browser: render all "text/*" Gemini responses

This commit is contained in:
dece 2021-04-16 19:56:56 +02:00
parent 347b5a81cc
commit f3a3a36039
5 changed files with 22 additions and 5 deletions

View file

@ -4,7 +4,7 @@ TODO DONE
links
redirections
web links
history
history (back/forward)
simple caching
simple text files
encodings
@ -14,6 +14,7 @@ non shit command-line
home page
downloads
media files
view history
identity management
configuration
--------------------------------------------------------------------------------

View file

@ -18,7 +18,7 @@ def open_file(browser: Browser, filepath: str, encoding="utf-8", history=True):
except (OSError, ValueError) as exc:
browser.set_status_error(f"Failed to open file: {exc}")
return
browser.load_page(Page.from_gemtext(text))
browser.load_page(Page.from_text(text))
file_url = "file://" + filepath
if history:
browser.history.push(file_url)

View file

@ -84,7 +84,9 @@ def handle_response_content(browser: Browser, response: Response) -> int:
According to the MIME type received or inferred, render or download the
response's content.
Currently only text/gemini content is rendered.
Currently only text content is rendered. For Gemini, the encoding specified
in the response is used, if available on the Python distribution. For other
text formats, only UTF-8 is attempted.
Arguments:
- response: a successful Response.
@ -108,7 +110,9 @@ def handle_response_content(browser: Browser, response: Response) -> int:
browser.load_page(Page.from_gemtext(text))
return 0
else:
pass # TODO
text = response.content.decode("utf-8", errors="replace")
browser.load_page(Page.from_text(text))
return 0
else:
pass # TODO
return 1

View file

@ -94,6 +94,11 @@ def generate_metalines(elements, width):
return metalines
def generate_dumb_metalines(lines):
"""Generate dumb metalines: all lines are given the PARAGRAPH line type."""
return [({"type": LineType.PARAGRAPH}, line) for line in lines]
def format_title(title: Title, context: dict):
"""Return metalines for this title."""
width = context["width"]

View file

@ -1,7 +1,7 @@
from dataclasses import dataclass, field
from bebop.gemtext import parse_gemtext, Title
from bebop.metalines import generate_metalines
from bebop.metalines import generate_dumb_metalines, generate_metalines
from bebop.links import Links
@ -10,6 +10,7 @@ class Page:
"""Page-related data.
Attributes:
- source: str used to create the page.
- metalines: lines ready to be rendered.
- links: Links instance, mapping IDs to links on the page; this data is
redundant as the links' URLs/IDs are already available in the
@ -28,3 +29,9 @@ class Page:
elements, links, title = parse_gemtext(gemtext)
metalines = generate_metalines(elements, 80)
return Page(gemtext, metalines, links, title)
@staticmethod
def from_text(text: str):
"""Produce a Page for a text string."""
metalines = generate_dumb_metalines(text.splitlines())
return Page(text, metalines)