2021-03-13 20:37:13 +01:00
|
|
|
from dataclasses import dataclass, field
|
2021-05-24 20:09:34 +02:00
|
|
|
from typing import Optional
|
2021-02-12 23:29:51 +01:00
|
|
|
|
2021-04-17 22:02:29 +02:00
|
|
|
from bebop.gemtext import parse_gemtext
|
2021-06-28 00:49:44 +02:00
|
|
|
from bebop.metalines import (
|
|
|
|
RenderOptions, generate_dumb_metalines, generate_metalines)
|
2021-05-24 20:09:34 +02:00
|
|
|
from bebop.mime import MimeType
|
2021-03-13 16:31:11 +01:00
|
|
|
from bebop.links import Links
|
2021-02-12 23:29:51 +01:00
|
|
|
|
|
|
|
|
2021-06-28 00:49:44 +02:00
|
|
|
def get_render_options(config: dict):
|
|
|
|
"""Prepare RenderOptions from the user config."""
|
|
|
|
return RenderOptions(
|
|
|
|
width=config["text_width"],
|
|
|
|
mode=config["render_mode"],
|
|
|
|
bullet=config["list_item_bullet"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-13 20:37:13 +01:00
|
|
|
@dataclass
|
2021-02-12 23:29:51 +01:00
|
|
|
class Page:
|
2021-03-28 18:55:52 +02:00
|
|
|
"""Page-related data.
|
|
|
|
|
|
|
|
Attributes:
|
2021-04-16 19:56:56 +02:00
|
|
|
- source: str used to create the page.
|
2021-03-28 18:55:52 +02:00
|
|
|
- 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
|
|
|
|
corresponding metalines, it is meant to be used as a quick map for link ID
|
|
|
|
lookup and disambiguation.
|
|
|
|
- title: optional page title.
|
2021-05-29 01:15:04 +02:00
|
|
|
- mime: optional MIME type received from the server.
|
|
|
|
- encoding: optional encoding received from the server.
|
2021-06-28 00:49:44 +02:00
|
|
|
- render_opts: optional render options used to create the page from Gemtext.
|
2021-03-28 18:55:52 +02:00
|
|
|
"""
|
2021-04-16 19:30:33 +02:00
|
|
|
source: str
|
2021-03-13 20:37:13 +01:00
|
|
|
metalines: list = field(default_factory=list)
|
|
|
|
links: Links = field(default_factory=Links)
|
2021-03-18 01:55:31 +01:00
|
|
|
title: str = ""
|
2021-05-24 20:09:34 +02:00
|
|
|
mime: Optional[MimeType] = None
|
|
|
|
encoding: str = ""
|
2021-06-28 00:49:44 +02:00
|
|
|
render_opts: Optional[RenderOptions] = None
|
2021-03-13 20:37:13 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2021-06-28 00:49:44 +02:00
|
|
|
def from_gemtext(gemtext: str, options: RenderOptions):
|
2021-03-13 20:37:13 +01:00
|
|
|
"""Produce a Page from a Gemtext file or string."""
|
2021-06-28 00:49:44 +02:00
|
|
|
dumb = options.mode == "dumb"
|
|
|
|
elements, links, title = parse_gemtext(gemtext, dumb=dumb)
|
|
|
|
metalines = generate_metalines(elements, options)
|
|
|
|
return Page(
|
|
|
|
gemtext,
|
|
|
|
metalines,
|
|
|
|
links,
|
|
|
|
title,
|
|
|
|
render_opts=options
|
|
|
|
)
|
2021-04-16 19:56:56 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_text(text: str):
|
|
|
|
"""Produce a Page for a text string."""
|
|
|
|
metalines = generate_dumb_metalines(text.splitlines())
|
|
|
|
return Page(text, metalines)
|