pub mod grammar; pub mod theme; use crate::{markdown::render::escape, syntax_highlight::theme::theme}; use grammar::grammar_for; use synoptic::{Highlighter, Token}; pub fn syntax_highlight(lang: &str, source: &str) -> String { let mut h = Highlighter::new(); for (kind, regex) in grammar_for(lang) { h.join(regex, kind).unwrap(); } let highlighting = h.run(source); let mut out = String::new(); for (_c, row) in highlighting.iter().enumerate() { for tok in row { match tok { Token::Start(kind) => out += &format!("", theme(kind)), Token::Text(text) => out += &escape(text), Token::End(_kind) => out += "", } } out += "\n" } out }