25 lines
876 B
Rust
25 lines
876 B
Rust
use syntect::{highlighting::ThemeSet, parsing::SyntaxSet};
|
|
use tracing::{error, instrument};
|
|
|
|
#[instrument(skip(content))]
|
|
pub fn hilight(content: &str, lang: &str, theme: Option<&str>) -> anyhow::Result<String> {
|
|
let ss = SyntaxSet::load_defaults_newlines();
|
|
let s = ss
|
|
.find_syntax_by_extension(lang)
|
|
.or_else(|| ss.find_syntax_by_name(lang))
|
|
.unwrap_or_else(|| {
|
|
error!("Syntax not found for language: {}", lang);
|
|
ss.find_syntax_plain_text()
|
|
});
|
|
let ts = ThemeSet::load_defaults();
|
|
let theme = if let Some(t) = theme {
|
|
ts.themes
|
|
.get(t)
|
|
.unwrap_or_else(|| ts.themes.first_key_value().unwrap().1)
|
|
} else {
|
|
ts.themes.first_key_value().unwrap().1
|
|
}; // TODO
|
|
|
|
let res = syntect::html::highlighted_html_for_string(content, &ss, s, theme)?;
|
|
Ok(res)
|
|
}
|