The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Syntax Highlighting In Rust Apps With The syntect Crate

TODO: Pull subtitle into page object

Introduction

I looked at using `syntect`` for syntax highlighting on my site. Initiail tests were very slow so I'm not going with it. Might take a look at some caching stuff later, but that's more than I want to get into. For now I'll just use one of the javascript front ends like prism.

The notes below are for reference for usage.

(I'm also not sure if I'm doing something that is causing the slowness. I'll re-examine at some point to see if there's something to be done about it)

Inline Styles Included

This is the first approach I'm using. It adds inline styles but doesn't wrap things in a `

Code
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
use syntect::parsing::SyntaxSet;

fn main() {
    let ps = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let syntax = ps.find_syntax_by_name("HTML").unwrap();
    let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
    let regions = h.highlight_line("<h1>Alfa</h1>", &ps).unwrap();
    let html = styled_line_to_highlighted_html(&regions[..], IncludeBackground::No).unwrap();
    dbg!(html);
}

Core Usage

Code
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;

fn main() {
    let current_code = r#"<h1>Alfa</h1>
<p>Bravo</p>"#;
    let syntax_set = SyntaxSet::load_defaults_newlines();
    let syntax = syntax_set.find_syntax_by_name("HTML").unwrap();
    let mut html_generator =
        ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
    for line in LinesWithEndings::from(current_code) {
        html_generator.parse_html_for_line_which_includes_newline(line);
    }
    let output_html = html_generator.finalize();
    dbg!(output_html);
}

Alternate Approach

The crate offers a way to style things directly inline as well. That code looks like this:

Code
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;

fn main() {
    let ss = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let syntax = ss.find_syntax_by_extension("rs").unwrap();
    let source_text = String::from("<h1>Alfa</h1>");
    let highlighted =
        highlighted_html_for_string(&source_text, &ss, &syntax, &ts.themes["base16-ocean.dark"])
            .unwrap();
    dbg!(highlighted);
}
Installation

I installed the crate that got all these working with:

cargo add syntect

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Syntax Highlighting In Rust Apps With The syntect Crate

-- todo

[] Look at and combine these posts: 2ujekgik

-- h2

Introduction 

I looked at using `syntect`` for syntax highlighting
on my site. Initiail tests were very slow so I'm not
going with it. Might take a look at some caching stuff
later, but that's more than I want to get into. For
now I'll just use one of the javascript front ends
like prism.

The notes below are for reference for usage. 

(I'm also not sure if I'm doing something that 
is causing the slowness. I'll re-examine at 
some point to see if there's something to 
be done about it)

-- h2

Inline Styles Included

This is the first approach I'm using. It adds inline
styles but doesn't wrap things in a `<pre>`html` tag. 
I'm taking care of that already which lets me add
additional styles when I want. 

-- code
-- rust

use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
use syntect::parsing::SyntaxSet;

fn main() {
    let ps = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let syntax = ps.find_syntax_by_name("HTML").unwrap();
    let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
    let regions = h.highlight_line("<h1>Alfa</h1>", &ps).unwrap();
    let html = styled_line_to_highlighted_html(&regions[..], IncludeBackground::No).unwrap();
    dbg!(html);
}


-- h2

Core Usage


-- todo

[] Write up details on this approach


-- code
-- rust 

use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;

fn main() {
    let current_code = r#"<h1>Alfa</h1>
<p>Bravo</p>"#;
    let syntax_set = SyntaxSet::load_defaults_newlines();
    let syntax = syntax_set.find_syntax_by_name("HTML").unwrap();
    let mut html_generator =
        ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
    for line in LinesWithEndings::from(current_code) {
        html_generator.parse_html_for_line_which_includes_newline(line);
    }
    let output_html = html_generator.finalize();
    dbg!(output_html);
}


-- todo

[] show how to generate the css file


-- h2

Alternate Approach 

The crate offers a way to style things directly inline as
well. That code looks like this:

-- code
-- rust

use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;

fn main() {
    let ss = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let syntax = ss.find_syntax_by_extension("rs").unwrap();
    let source_text = String::from("<h1>Alfa</h1>");
    let highlighted =
        highlighted_html_for_string(&source_text, &ss, &syntax, &ts.themes["base16-ocean.dark"])
            .unwrap();
    dbg!(highlighted);
}


-- note
-- title: Installation

I installed the crate that got all these working with:

cargo add syntect




-- ref
-- id: syntect
-- title: syntect
-- url: https://github.com/trishume/syntect

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


-- ref
-- title: syntect::html::styled_line_to_highlighted_html
-- url: https://docs.rs/syntect/latest/syntect/html/fn.styled_line_to_highlighted_html.html

I copied the code I'm using directly from this example. 


-- ref
-- title: Sublime Text Syntax Definitions
-- url: https://www.sublimetext.com/docs/syntax.html#include-syntax

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.


-- ref
-- title: syntect::html::ClassedHTMLGenerator
-- url: https://docs.rs/syntect/latest/syntect/html/struct.ClassedHTMLGenerator.html


-- categories
-- Rust 

-- metadata
-- date: 2023-04-24 20:11:40
-- id: 2otym2mt
-- site: aws
-- type: post
-- status: draft