summaryrefslogtreecommitdiff
path: root/support/texlab/src/citation.rs
blob: 96a5a2eccffb952fc2ecbcbb122771672f95fdf5 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
mod bibutils;
mod ris;

use std::sync::Arc;

use citeproc::{prelude::SupportedFormat, ClusterPosition, InitOptions, Processor};
use citeproc_db::PredefinedLocales;
use citeproc_io::{Cite, Reference};
use lsp_types::{MarkupContent, MarkupKind};
use once_cell::sync::Lazy;
use regex::Regex;

use crate::syntax::{bibtex, CstNode};

use self::{
    bibutils::*,
    ris::{RisLibrary, RisReference},
};

static APA_STYLE: &str = include_str!("citation/apa.csl");

static DOI_URL_PATTERN: &str = r#"https://doi.org/\[.*\]\(.*\)"#;

static DOI_URL_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(DOI_URL_PATTERN).unwrap());

pub fn render_citation(root: &bibtex::SyntaxNode, key: &str) -> Option<MarkupContent> {
    let ris_reference = convert_to_ris(root, key)?;
    let doi_url = get_doi_url_markdown(&ris_reference);
    let csl_reference: Reference = ris_reference.into();
    let html = generate_bibliography(csl_reference)?;

    let mut markdown = html2md::parse_html(&html).trim().to_owned();
    if markdown == "" {
        return None;
    }

    if let Some(doi_url) = doi_url {
        markdown = DOI_URL_REGEX
            .replace(&markdown, doi_url.as_str())
            .into_owned();
    }

    markdown = markdown
        .replace("..", ".")
        .replace("\\\'", "'")
        .replace("\\-", "-")
        .replace("\\\\textsubscript", "")
        .replace("\\\\textsuperscript", "");
    let content = MarkupContent {
        kind: MarkupKind::Markdown,
        value: markdown,
    };
    Some(content)
}

fn convert_to_ris(root: &bibtex::SyntaxNode, key: &str) -> Option<RisReference> {
    let mut bib_code = String::new();
    for string in root
        .children()
        .filter_map(bibtex::String::cast)
        .filter(|string| string.name().is_some())
    {
        bib_code.push_str(&string.syntax().to_string());
    }

    let entry = root
        .children()
        .filter_map(bibtex::Entry::cast)
        .find(|entry| entry.key().map(|key| key.to_string()).as_deref() == Some(key))
        .filter(|entry| entry.fields().next().is_some())?;

    bib_code.push_str(&entry.syntax().to_string());

    bib_code = bib_code.replace("\\hypen", "-");

    let ris_code = bibutils::convert(&bib_code, InputFormat::Biblatex, OutputFormat::Ris)?;
    let ris_lib = RisLibrary::parse(ris_code.lines());
    ris_lib
        .references
        .into_iter()
        .find(|reference| reference.id.as_ref().map(AsRef::as_ref) == Some(key))
}

fn get_doi_url_markdown(ris_reference: &RisReference) -> Option<String> {
    ris_reference
        .doi
        .as_ref()
        .map(|doi| format!("[doi:{}](https://doi.org/{})", doi, doi))
}

fn generate_bibliography(reference: Reference) -> Option<String> {
    let mut processor = Processor::new(InitOptions {
        style: APA_STYLE,
        format: SupportedFormat::Html,
        fetcher: Some(Arc::new(PredefinedLocales::bundled_en_us())),
        ..InitOptions::default()
    })
    .ok()?;
    let cite = Cite::basic(&reference.id);
    let cluster_id = processor.new_cluster("texlab");
    processor.insert_reference(reference);
    processor.insert_cites(cluster_id, &[cite]);
    processor
        .set_cluster_order(&[ClusterPosition {
            id: cluster_id,
            note: Some(1),
        }])
        .unwrap();
    Some(processor.get_bibliography().pop()?.value.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple() {
        let document = bibtex::parse(
            r#"
                @article{foo,
                    author = {Foo Bar},
                    title = {Baz Qux},
                    year = {2020}
                }
            "#,
        );

        let actual_md = render_citation(&document.root, "foo").unwrap();

        let expected_md = MarkupContent {
            kind: MarkupKind::Markdown,
            value: "Bar, Foo. (2020). *Baz Qux*.".into(),
        };

        assert_eq!(actual_md, expected_md);
    }

    #[test]
    fn test_string() {
        let document = bibtex::parse(
            r#"
                @string{author = "Foo Bar"}
                @article{foo,
                    author = author,
                    title = {Baz Qux},
                    year = {2020}
                }
            "#,
        );
        let actual_md = render_citation(&document.root, "foo").unwrap();

        let expected_md = MarkupContent {
            kind: MarkupKind::Markdown,
            value: "Bar, Foo. (2020). *Baz Qux*.".into(),
        };

        assert_eq!(actual_md, expected_md);
    }

    #[test]
    fn test_unknown_key() {
        let document = bibtex::parse("");

        let actual_md = render_citation(&document.root, "foo");

        assert_eq!(actual_md, None);
    }
}