typst / hayagriva

Rusty bibliography management.
Apache License 2.0
289 stars 44 forks source link

HTML output for rendered citations and bibliography #156

Open kamoshi opened 2 months ago

kamoshi commented 2 months ago

Hey, I'm trying to use hayagriva as a library to parse and render citations and bibliography on a HTML website. Generally it works quite well, but when rendering the data the library includes ANSI escape codes in the output by default. This isn't a major problem, because I can for example do something like .replace("\u{1b}[0m", "") to strip them, but this is less than ideal.

For example given this bibliography

@book{dojg-advanced,
  title     = {A dictionary of advanced Japanese grammar},
  author    = {Makino, Seiichi and Tsutsui, Michio},
  isbn      = {9784789012959},
  lccn      = {2017414531},
  year      = {2008},
  publisher = {Japan Times}
}

When rendering this entry as a String I get:

Ideally (HTML) these would be something along the lines of:

If this currently can't be accomplished, it could potentially be added as an alternative API to the default ToString, if it can then it would be useful to add the way to accomplish this to the documentation.

This is how I am currently processing the bibliography:

    let bib = match res.bibliography {
        Some(ref bib) => {
            let test = bib.items.iter()
                .map(|x| x.content.to_string().replace("\u{1b}[0m", ""))
                .collect::<Vec<_>>();
            Some(test)
        },
        None => None,
    };
laurmaedje commented 2 months ago

Note that you can also iterate over the ElemChildren yourself, the ANSI escapes are just a default. A direct-to-HTML feature might still be nice.

kamoshi commented 2 months ago

I took a look at the code, and I found that you can format HTML like this (via ElemChildren):

let mut buffer = String::new();
x.content.write_buf(&mut buffer, BufWriteFormat::Html).unwrap();
buffer

Result string looks like this:

 Makino S and Tsutsui M 2008 <span style="font-style: italic;">A dictionary of advanced Japanese grammar</span> (Japan Times)

One different issue I found along the way is that the BibliographyItem is marked as pub, but it isn't exported from the package, so it can't be accessed in the documentation, even though you can use it in code as library consumer. It's also missing documentation in code.

laurmaedje commented 2 months ago

Oh, I completely missed that HTML is already implemented. I also noticed the problem with the re-export and had opened https://github.com/typst/hayagriva/issues/157 for it.