example/example.rs
1// Copyright (C) 2023 Enrico Guiraud
2//
3// This file is part of highlight-pulldown.
4//
5// highlight-pulldown is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// highlight-pulldown is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with highlight-pulldown. If not, see <http://www.gnu.org/licenses/>.
17
18use highlight_pulldown::highlight_with_theme;
19
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("foo", 42)
52</span><span style="color:#c0c5ce;"> ```
53</span></pre>
54"#;
55 assert_eq!(html, expected);
56}