From 802efbca25cb92d8567761361b6513fd57e05578 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 30 Aug 2022 16:54:57 +0200 Subject: basic syntax highlighting --- code/src/syntax_highlight/mod.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 code/src/syntax_highlight/mod.rs (limited to 'code/src/syntax_highlight/mod.rs') diff --git a/code/src/syntax_highlight/mod.rs b/code/src/syntax_highlight/mod.rs new file mode 100644 index 0000000..688b010 --- /dev/null +++ b/code/src/syntax_highlight/mod.rs @@ -0,0 +1,29 @@ +pub mod grammar; +pub mod theme; + +use crate::{markdown::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 (regex, kind) 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() { + eprintln!("{row:?}"); + 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" + } + eprintln!("{out:?}"); + out +} -- cgit v1.2.3-70-g09d2