Builditluc / wiki-tui

A simple and easy to use Wikipedia Text User Interface
https://wiki-tui.net/
MIT License
416 stars 14 forks source link

Rewriting wiki::parser #219

Closed Builditluc closed 11 months ago

Builditluc commented 1 year ago

Linked to #216

This issue describes the changes to the wiki::parser module already outlined in #216. The main goal is to remove any outside dependencies (cursive and the use of the global CONFIG) from the parser. The few dependencies we may need should be injected (dependency injection) to simplify the testing of the parser.

To remove the dependency from the cursive crate, we would need to either remove the Effect and Style values from the Element or create our own.

I believe we should remove the styling from the parser altogether because the parser really should only create a tree of elements from the HTML page and nothing more. The colors and formatting should be done by the renderer (LinesWrapper).

That means the Element should only include the id,kind, content, width and attributes. The style attribute is no longer needed. We also need a way of specifying the effects of the element meaning we need a custom Effect struct.

Because we have to sometimes declare the start and end of different objects such as lists or disambiguations, the element struct should be an Enum with two different options: Content and Meta. Element::Content would contain the standard element attributes like width and id and Element::Meta would contain a value of another enum containing the different Meta values.

Below is a first structure

enum Element {
  Content(ElementContent),
  Meta(ElementMeta),
}

struct ElementContent {
  id,
  kind: ElementKind,
  content,
  ...
}

enum ElementKind {
  Text,
  Header,
  ...
}

enum ElementMeta {
  ListItemStart,
  ListItemEnd,
  ...
}

For some elements, we need extra information (the anchor for headers, the url for links, etc.). We could embed them in the ElementKind enum. For a link it could look like this

enum ElementKind {
  Link { link: link::Link },
  ...

When we want to render the returned tree of elements, we can store the current context and apply padding, styling, etc. as needed.

That means we need to do the following things: