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
use crate::{
    utils::{html_render_line, html_render_line_number, CarbonHTML},
    CarbonError, Render,
};
use carbon_dump::SYNTAX_SET;
use std::ops::Deref;
use syntect::{
    easy::HighlightLines,
    highlighting::{Color, Style},
    util::{as_24_bit_terminal_escaped, as_latex_escaped, LinesWithEndings},
};

impl Render {
    pub fn render_terminal(&self, input: &str) -> Result<String, CarbonError> {
        let set = SYNTAX_SET.deref();
        // The main process of the program
        let mut h = HighlightLines::new(&self.syntax, &self.theme);
        let mut out = String::with_capacity(2 * input.len());
        for line in LinesWithEndings::from(input) {
            let ranges: Vec<(Style, &str)> = h.highlight(line, set);
            let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
            out.push_str(&escaped)
        }
        return Ok(out);
    }
    pub fn render_latex(&self, input: &str) -> Result<String, CarbonError> {
        let set = SYNTAX_SET.deref();
        // The main process of the program
        let mut h = HighlightLines::new(&self.syntax, &self.theme);
        let mut out = String::with_capacity(10 * input.len());
        for line in LinesWithEndings::from(input) {
            // LinesWithEndings enables use of newlines mode
            let ranges: Vec<(Style, &str)> = h.highlight(line, set);
            let escaped = as_latex_escaped(&ranges[..]);
            out.push_str(&escaped);
            out.push_str("\n\n")
        }
        return Ok(out);
    }
    pub fn render_html(&self, input: &str) -> Result<String, CarbonError> {
        // The main process of the program
        let mut out = String::with_capacity(25 * input.len());
        let c = self.theme.settings.background.unwrap_or(Color::WHITE);
        match self.html_type {
            CarbonHTML::Inline => {
                out.push_str("<pre class=\"carbon\">");
                out.push_str(&self.render_check_line_number(input));
                out.push_str(&format!("</pre>"));
            }
            CarbonHTML::Embedded => match &self.file_title {
                None => {
                    out.push_str(&self.render_check_line_number(input));
                }
                Some(s) => {
                    out.push_str(&format!(
                        "<div class=\"carbon\" style=\"background-color:#{:02x}{:02x}{:02x};\">\n",
                        c.r, c.g, c.b
                    ));
                    out.push_str(&self.render_check_line_number(input));
                    out.push_str(&format!("</div>"));
                }
            },
            CarbonHTML::Independent => {
                out.push_str(&format!("<head><style>pre{{{}}}</style></head>", self.html_style));
                out.push_str(&format!("<body style=\"background-color:#{:02x}{:02x}{:02x};\">\n", c.r, c.g, c.b));
                out.push_str(&format!("{}", self.render_check_line_number(input)));
                out.push_str(&format!("</body>"));
            }
        };
        return Ok(out);
    }
}

impl Render {
    fn render_check_line_number(&self, input: &str) -> String {
        match self.line_number {
            None => html_render_line(input, &self.syntax, &self.theme),
            Some(_) => html_render_line_number(input, &self.syntax, &self.theme),
        }
    }
}