summaryrefslogtreecommitdiff
path: root/src/generate_html.rs
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-05-21 14:25:18 +1200
committerBen Bridle <ben@derelict.engineering>2025-05-21 19:50:09 +1200
commit060c1f23a5585a5fdabc56eda808b4cfd90f9081 (patch)
treea8bd3d2aa0e5c87528a5b7c417cf4a63edb2dbef /src/generate_html.rs
parentc749c1db30f2b757e63093ac39127b3e338cb704 (diff)
downloadtoaster-060c1f23a5585a5fdabc56eda808b4cfd90f9081.zip
Implement syntax highlighting for syntax fragments
A new key 'highlighters' has been added to the toaster.conf file. The value should be a line defining the languages to use that syntax for, like [py/python]. The lines following are the template definitions, as per the highlighter library.
Diffstat (limited to 'src/generate_html.rs')
-rw-r--r--src/generate_html.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/generate_html.rs b/src/generate_html.rs
index 0de42a5..5abddf2 100644
--- a/src/generate_html.rs
+++ b/src/generate_html.rs
@@ -236,7 +236,24 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs
warn!("Gallery-nav on page {from:?} has line without a '::' separator");
}
}),
- _ => wrap!("pre", format!("class='{language}'"), html!("{}", sanitize_text(content, false))),
+ _ => wrap!("pre", format!("class='{language}'"), {
+ if let Some(i) = website.highlighters.languages.get(language) {
+ let mut source = String::new();
+ let highlighter = &website.highlighters.highlighters[*i];
+ for span in highlighter.highlight(content) {
+ if span.tag.is_empty() {
+ source.push_str(&sanitize_text(&span.text, false));
+ } else {
+ source.push_str(&format!("<span class='{}'>", span.tag.to_lowercase()));
+ source.push_str(&sanitize_text(&span.text, false));
+ source.push_str("</span>");
+ }
+ }
+ html!("{source}");
+ } else {
+ html!("{}", sanitize_text(content, false))
+ }
+ })
}
}
Block::Break => html!("<hr>"),