Skip to main content

contributor_graphs/
html.rs

1use crate::model::{Contributor, RepoMeta};
2
3const TEMPLATE: &str = include_str!("assets/template.html");
4
5pub struct HtmlOptions {
6    pub accent: String,
7    pub by_affiliation: bool,
8    pub unaffiliated_label: String,
9}
10
11impl Default for HtmlOptions {
12    fn default() -> Self {
13        HtmlOptions {
14            accent: "#2f6feb".into(),
15            by_affiliation: false,
16            unaffiliated_label: "Unaffiliated".into(),
17        }
18    }
19}
20
21pub fn render_html(meta: &RepoMeta, contributors: &[Contributor], opts: &HtmlOptions) -> String {
22    let data = serde_json::json!({
23        "repo": meta,
24        "contributors": contributors,
25        "accent": opts.accent,
26        "byAffiliation": opts.by_affiliation,
27        "unaffiliated": opts.unaffiliated_label,
28    });
29    // `<\/` keeps any `</script>` inside the JSON from terminating the tag.
30    let json = serde_json::to_string(&data)
31        .expect("serialize data")
32        .replace("</", "<\\/");
33    let title = format!("{} ยท contributors", meta.name);
34    TEMPLATE
35        .replace("__PAGE_TITLE__", &html_escape(&title))
36        .replace("__DATA__", &json)
37}
38
39fn html_escape(s: &str) -> String {
40    s.replace('&', "&amp;")
41        .replace('<', "&lt;")
42        .replace('>', "&gt;")
43}