Skip to content
This repository was archived by the owner on Jul 12, 2026. It is now read-only.
This repository was archived by the owner on Jul 12, 2026. It is now read-only.

Pluggable Renderer design #105

Open
Open

Description

@bdarcus

It should be trivial to configure different output renderers (HTML, RTF, ODT, LaTeX, etc.), and to create new ones (like Typst). I'm a bit uncertain on the ideal design ATM, however.

  1. pluggable output renderers, starting with basic string, and maybe Djot integration #86
  2. borrow ideas from https://github.com/obsidiandynamics/stanza; it has a markdown renderer, for example

Here's how one uses jotdown, which has a Render trait to offer configurable output.

use jotdown::Render;
let djot_input = "hello *world*!";
let events = jotdown::Parser::new(djot_input);
let mut html = String::new();
jotdown::html::Renderer::default().push(events, &mut html);
assert_eq!(html, "<p>hello <strong>world</strong>!</p>\n");

While that's an event-based processor (hence the push method), and this isn't, here's how they define that trait:

pub trait Render {
    /// Push owned [`Event`]s to a unicode-accepting buffer or stream.
    fn push<'s, I, W>(&self, events: I, out: W) -> fmt::Result
    where
        I: Iterator<Item = Event<'s>>,
        W: fmt::Write;

    /// Write owned [`Event`]s to a byte sink, encoded as UTF-8.
    ///
    /// NOTE: This performs many small writes, so IO writes should be buffered with e.g.
    /// [`std::io::BufWriter`].
    fn write<'s, I, W>(&self, events: I, out: W) -> io::Result<()>
    where
        I: Iterator<Item = Event<'s>>,
        W: io::Write,
    {
        let mut out = WriteAdapter {
            inner: out,
            error: Ok(()),
        };

        self.push(events, &mut out).map_err(|_| match out.error {
            Err(e) => e,
            _ => io::Error::new(io::ErrorKind::Other, "formatter error"),
        })
    }

Here's the stanza Renderer trait, and it's markdown implementation:

pub trait Renderer {
    type Output: Display;

    #[inline]
    fn render(&self, table: &Table) -> Self::Output {
        self.render_with_hints(table, &[])
    }

    fn render_with_hints(&self, table: &Table, hints: &[RenderHint]) -> Self::Output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions