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
use crate::{CarbonError, CarbonResult};
use carbon_dump::{SYNTAX_SET, THEME_SET};
use syntect::{highlighting::Theme, parsing::SyntaxReference};
pub struct Render {
pub(crate) theme: Theme,
pub(crate) syntax: SyntaxReference,
pub html_style: String,
pub html_type: CarbonHTML,
pub file_title: Option<String>,
pub line_number: Option<usize>,
}
pub enum CarbonHTML {
Inline,
Embedded,
Independent,
}
impl Default for Render {
fn default() -> Self {
let theme = THEME_SET.themes.get("one-dark").unwrap();
let syntax = SYNTAX_SET.find_syntax_by_extension("rs").unwrap();
Self {
syntax: syntax.clone(),
theme: theme.clone(),
html_style: String::from(include_str!("render.css")),
html_type: CarbonHTML::Independent,
file_title: None,
line_number: None,
}
}
}
impl Render {
pub fn set_theme(&mut self, s: &str) -> CarbonResult<()> {
let theme = THEME_SET.themes.get(s).ok_or(CarbonError::ThemeNotFound(s.to_string()))?;
self.theme = theme.clone();
Ok(())
}
pub fn set_syntax(&mut self, s: &str) -> CarbonResult<()> {
let syntax = SYNTAX_SET.find_syntax_by_extension(s).ok_or(CarbonError::SyntaxNotFound(s.to_string()))?;
self.syntax = syntax.clone();
Ok(())
}
}