highlight_with_theme

Function 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.

It might be time-consuming to call this method in a hot loop: in that situation you might want to use a PulldownHighlighter object instead.

Examples found in repository?
examples/example.rs (line 44)
20fn main() {
21    let markdown = r#"# Hello syntax highlighting
22Here's some Python code:
23```python
24print("foo", 42)
25```
26
27And here's some Rust code:
28```rust
29enum Hello {
30    World,
31    SyntaxHighlighting,
32}
33```
34"#;
35
36    let events = pulldown_cmark::Parser::new(markdown);
37
38    // The themes available are the same as in syntect:
39    // - base16-ocean.dark,base16-eighties.dark,base16-mocha.dark,base16-ocean.light
40    // - InspiredGitHub
41    // - Solarized (dark) and Solarized (light)
42    // The default theme is the same as in syntect: "base16-ocean.dark".
43    // See also https://docs.rs/syntect/latest/syntect/highlighting/struct.ThemeSet.html#method.load_defaults .
44    let events = highlight_with_theme(events, "base16-ocean.dark").unwrap();
45
46    let mut html = String::new();
47    pulldown_cmark::html::push_html(&mut html, events.into_iter());
48
49    let expected = r#"<pre style="background-color:#2b303b;">
50<span style="color:#c0c5ce;">  ```python
51</span><span style="color:#c0c5ce;">  print(&quot;foo&quot;, 42)
52</span><span style="color:#c0c5ce;">  ```
53</span></pre>
54"#;
55    assert_eq!(html, expected);
56}