tinymist_query/
bib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use hayagriva::{
    BibliographyDriver, BibliographyRequest, BufWriteFormat, CitationItem, CitationRequest,
    ElemChildren,
};

use crate::analysis::BibInfo;

pub(crate) struct RenderedBibCitation {
    pub citation: String,
    pub bib_item: String,
}

/// Render the citation string in the bib with given CSL style.
pub(crate) fn render_citation_string(
    bib_info: &BibInfo,
    key: &str,
    support_html: bool,
) -> Option<RenderedBibCitation> {
    let entry = bib_info.entries.get(key)?;
    let raw_entry = entry.raw_entry.as_ref()?;

    let mut driver = BibliographyDriver::new();

    let locales = &[];
    driver.citation(CitationRequest::from_items(
        vec![CitationItem::with_entry(raw_entry)],
        bib_info.csl_style.get(),
        locales,
    ));

    let result = driver.finish(BibliographyRequest {
        style: bib_info.csl_style.get(),
        locale: None, // todo: get locale from CiteElem
        locale_files: locales,
    });
    let rendered_bib = result.bibliography?;

    let format_elem = |elem: &ElemChildren| {
        let mut buf = String::new();
        elem.write_buf(
            &mut buf,
            if support_html {
                BufWriteFormat::Html
            } else {
                BufWriteFormat::Plain
            },
        )
        .ok()?;
        Some(buf)
    };

    Some(RenderedBibCitation {
        citation: format_elem(&result.citations.first()?.citation)?,
        bib_item: format_elem(&rendered_bib.items.first()?.content)?,
    })
}