diff --git a/crates/treehouse/src/html/highlight/tokenize.rs b/crates/treehouse/src/html/highlight/tokenize.rs index 3ced033..30865ce 100644 --- a/crates/treehouse/src/html/highlight/tokenize.rs +++ b/crates/treehouse/src/html/highlight/tokenize.rs @@ -24,7 +24,38 @@ impl CompiledSyntax { break; } } - CompiledTokenTypes::Captures(types) => { /* TODO */ } + CompiledTokenTypes::Captures(types) => { + if let Some(captures) = pattern.regex.captures(&text[i..]) { + let whole_match = captures.get(0).unwrap(); + let mut last_match_end = 0; + for (index, capture) in captures + .iter() + .skip(1) + .enumerate() + .filter_map(|(i, m)| m.map(|m| (i, m))) + { + let id = types + .captures + .get(index) + .copied() + .unwrap_or(TOKEN_ID_DEFAULT); + push_token( + &mut tokens, + types.default, + i + last_match_end..i + capture.range().start, + ); + push_token( + &mut tokens, + id, + i + capture.range().start..i + capture.range().end, + ); + last_match_end = capture.range().end; + } + i += whole_match.range().end; + had_match = true; + break; + } + } } } @@ -47,6 +78,10 @@ impl CompiledSyntax { } fn push_token(tokens: &mut Vec, id: TokenId, range: Range) { + if range.is_empty() { + return; + } + if let Some(previous_token) = tokens.last_mut() { if previous_token.id == id { previous_token.range.end = range.end; diff --git a/crates/treehouse/src/html/markdown.rs b/crates/treehouse/src/html/markdown.rs index 4415419..308b66f 100644 --- a/crates/treehouse/src/html/markdown.rs +++ b/crates/treehouse/src/html/markdown.rs @@ -23,7 +23,6 @@ //! HTML renderer that takes an iterator of events as input. -use std::borrow::Borrow; use std::collections::HashMap; use std::io;