use highlighter_core::Token;
fn html_escape(str: String) -> String {
let mut new_str = String::new();
for char in str.chars() {
match char {
'>' => new_str.push_str(">"),
'<' => new_str.push_str("<"),
'&' => new_str.push_str("&"),
'\'' => new_str.push_str("'"),
'"' => new_str.push_str("""),
_ => new_str.push(char),
}
}
new_str
}
#[derive(Clone, Debug, PartialEq)]
pub struct HighlighterTargetHtml {
html_prefix: String,
html_suffix: String,
class_prefix: String,
}
impl HighlighterTargetHtml {
pub fn new() -> Self {
Self { html_prefix: String::new(), html_suffix: String::new(), class_prefix: "scope-".to_owned() }
}
pub fn with_html_prefix<Str: Into<String>>(mut self, html_prefix: Str) -> Self {
self.html_prefix = html_prefix.into();
self
}
pub fn with_html_suffix<Str: Into<String>>(mut self, html_suffix: Str) -> Self {
self.html_suffix = html_suffix.into();
self
}
pub fn with_class_prefix<Str: Into<String>>(mut self, class_prefix: Str) -> Self {
self.class_prefix = class_prefix.into();
self
}
pub fn build(&self, tokens: Vec<Token>) -> String {
let mut str = String::new();
for token in tokens {
str.push_str(&format!("<span class=\"{}{}\">{}</span>", self.class_prefix, token.scope.kebab_case(), html_escape(token.value)));
}
str
}
}