Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pluggable Renderer design #105

Open
bdarcus opened this issue Jul 3, 2023 · 0 comments
Open

Pluggable Renderer design #105

bdarcus opened this issue Jul 3, 2023 · 0 comments

Comments

@bdarcus
Copy link
Owner

bdarcus commented Jul 3, 2023

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;
}
@bdarcus bdarcus changed the title Renderer design Pluggable Renderer design Jul 3, 2023
bdarcus added a commit that referenced this issue Jul 19, 2023
I can't figure out how I want to do #105, so just added a basic
refs_to_string function, so I have some final rendering.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
bdarcus added a commit that referenced this issue Jul 19, 2023
I can't figure out how I want to do #105, so just added a basic
refs_to_string function, so I have some final rendering.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
bdarcus added a commit that referenced this issue Jul 19, 2023
I can't figure out how I want to do #105, so just added a basic
refs_to_string function, so I have some final rendering.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
bdarcus added a commit that referenced this issue Jul 23, 2023
I can't figure out how I want to do #105, so just added a basic
refs_to_string function, so I have some final rendering.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant