Function highlight_pulldown::highlight_with_theme
source · pub fn highlight_with_theme<'a, It>(
events: It,
theme: &str
) -> Result<Vec<Event<'a>>, Error>where
It: Iterator<Item = Event<'a>>,Expand description
Apply syntax highlighting to pulldown-cmark events using the specified theme.
Take an iterator over pulldown-cmark’s events, and (on success) return a new iterator where code blocks have been turned into HTML text blocks with syntax highlighting.
Based on https://github.com/raphlinus/pulldown-cmark/issues/167#issuecomment-448491422.
Examples found in repository?
examples/example.rs (line 44)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
fn main() {
let markdown = r#"# Hello syntax highlighting
Here's some Python code:
```python
print("foo", 42)
```
And here's some Rust code:
```rust
enum Hello {
World,
SyntaxHighlighting,
}
```
"#;
let events = pulldown_cmark::Parser::new(markdown);
// The themes available are the same as in syntect:
// - base16-ocean.dark,base16-eighties.dark,base16-mocha.dark,base16-ocean.light
// - InspiredGitHub
// - Solarized (dark) and Solarized (light)
// The default theme is the same as in syntect: "base16-ocean.dark".
// See also https://docs.rs/syntect/latest/syntect/highlighting/struct.ThemeSet.html#method.load_defaults .
let events = highlight_with_theme(events, "base16-ocean.dark").unwrap();
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, events.into_iter());
let expected = r#"<pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;"> ```python
</span><span style="color:#c0c5ce;"> print("foo", 42)
</span><span style="color:#c0c5ce;"> ```
</span></pre>
"#;
assert_eq!(html, expected);
}