|
| 1 | +//! Our Syntax Highlighting code, |
| 2 | +//! |
| 3 | +//! A comrak plugin for adding syntax highlighting classes to code blocks. |
| 4 | +
|
| 5 | +pub struct SyntaxHighlighter(); |
| 6 | + |
| 7 | +impl SyntaxHighlighter { |
| 8 | + pub fn new() -> SyntaxHighlighter { |
| 9 | + SyntaxHighlighter() |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +impl comrak::adapters::SyntaxHighlighterAdapter for SyntaxHighlighter { |
| 14 | + fn write_pre_tag( |
| 15 | + &self, |
| 16 | + output: &mut dyn std::io::Write, |
| 17 | + attributes: std::collections::HashMap<String, String>, |
| 18 | + ) -> std::io::Result<()> { |
| 19 | + println!("Pre Attr: {:?}", attributes); |
| 20 | + writeln!(output, "<pre>")?; |
| 21 | + Ok(()) |
| 22 | + } |
| 23 | + |
| 24 | + fn write_code_tag( |
| 25 | + &self, |
| 26 | + output: &mut dyn std::io::Write, |
| 27 | + attributes: std::collections::HashMap<String, String>, |
| 28 | + ) -> std::io::Result<()> { |
| 29 | + println!("Code Attr: {:?}", attributes); |
| 30 | + let mut lang_class = ""; |
| 31 | + if let Some(class) = attributes.get("class") { |
| 32 | + if let Some(lang) = class.strip_prefix("language-") { |
| 33 | + lang_class = lang; |
| 34 | + } |
| 35 | + } |
| 36 | + writeln!( |
| 37 | + output, |
| 38 | + r#"<code data-trim data-noescape class="{lang_class}">"# |
| 39 | + )?; |
| 40 | + Ok(()) |
| 41 | + } |
| 42 | + |
| 43 | + fn write_highlighted( |
| 44 | + &self, |
| 45 | + output: &mut dyn std::io::Write, |
| 46 | + lang: Option<&str>, |
| 47 | + code: &str, |
| 48 | + ) -> std::io::Result<()> { |
| 49 | + println!("Got lang {:?}", lang); |
| 50 | + writeln!(output, "{}", code)?; |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | +} |
0 commit comments