This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Bebop/bebop/page.py
dece 8aee7fdfba gemtext: add links/title to parsing result
parse_gemtext used to return only the element list, requiring subsequent
loops to find a title or collect links; now it's all done at the same
time!
2021-03-28 18:55:52 +02:00

30 lines
966 B
Python

from dataclasses import dataclass, field
from bebop.gemtext import parse_gemtext, Title
from bebop.metalines import generate_metalines
from bebop.links import Links
@dataclass
class Page:
"""Page-related data.
Attributes:
- 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.
"""
metalines: list = field(default_factory=list)
links: Links = field(default_factory=Links)
title: str = ""
@staticmethod
def from_gemtext(gemtext: str):
"""Produce a Page from a Gemtext file or string."""
elements, links, title = parse_gemtext(gemtext)
metalines = generate_metalines(elements, 80)
return Page(metalines, links, title)