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-04-16 19:56:56 +02:00
|
|
|
from bebop.metalines import 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-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-05-29 01:26:15 +02:00
|
|
|
- render: optional render mode 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-05-29 01:26:15 +02:00
|
|
|
render: Optional[str] = None
|
2021-03-13 20:37:13 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2021-05-29 01:13:43 +02:00
|
|
|
def from_gemtext(gemtext: str, wrap_at: int, render: str ="fancy"):
|
2021-03-13 20:37:13 +01:00
|
|
|
"""Produce a Page from a Gemtext file or string."""
|
2021-05-29 01:13:43 +02:00
|
|
|
dumb_mode = render == "dumb"
|
|
|
|
elements, links, title = parse_gemtext(gemtext, dumb=dumb_mode)
|
|
|
|
metalines = generate_metalines(elements, wrap_at, dumb=dumb_mode)
|
2021-05-29 01:26:15 +02:00
|
|
|
return Page(gemtext, metalines, links, title, render=render)
|
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)
|