Syntax Highlighting In Rust Apps With The syntect Crate
April 2023
TODO
☐
Merge this into: id: 2b1ep9iz
Inline Styles Included
This is the first approach I'm using. It adds inline styles but doesn't wrap things in a `
html tag. I'm taking care of that already which lets me add additional styles when I want.```cargo
syntect = "5.2.0"
```
use HighlightLines;
use ThemeSet;
use ;
use SyntaxSet;
Output:
<span style="color:#c0c5ce;"><</span><span style="color:#bf616a;">h1</span><span style="color:#c0c5ce;">>
Alfa
</</span><span style="color:#bf616a;">h1</span><span style="color:#c0c5ce;">></span>
Core Usage
use ;
use SyntaxSet;
use LinesWithEndings;
TODO
☐
show how to generate the css file
Alternate Approach
The crate offers a way to style things directly inline as well. That code looks like this:
use ThemeSet;
use highlighted_html_for_string;
use SyntaxSet;
I installed the crate that got all these working with:
cargo add syntect
end of line
References
A syntax highlighting library for Rust that uses Sublime Text syntax definitions. It aims to be a good solution for any Rust project that needs syntax highlighting
I copied the code I'm using directly from this example.
Sublime Syntax files are YAML files with a small header, followed by a list of contexts. Each context has a list of patterns that describe how to highlight text in that context, and how to change the current text.