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.

Rust Syntax Highlighting With Classes And Line Numbers

TODO: Pull subtitle into page object
Code
//! ```cargo
//! [dependencies]
//! syntect = "5.1.0"

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

fn main() {

  let current_code = r#"print("a")
print("b")
for x in y:
    print(x)"#;

  let code_type = "python";
  let syntax_set = SyntaxSet::load_defaults_newlines();
  let syntax = syntax_set.find_syntax_by_token(code_type).unwrap();
  let mut html_generator = ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
  for line in LinesWithEndings::from(current_code) {
      let _ = html_generator.parse_html_for_line_which_includes_newline(line);
  }
  let initial_html = html_generator.finalize();
  let output_html: Vec<_> = initial_html.lines()
    .map(|line| 
        format!(r#"<span class="linenumber">{}</span>"#, line)).collect();

  println!("{}", output_html.join("\n"));
}
Results
<span class="linenumber"><span class="source python"><span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta string python"><span class="string quoted double python"><span class="punctuation definition string begin python">&quot;</span></span></span><span class="meta string python"><span class="string quoted double python">a<span class="punctuation definition string end python">&quot;</span></span></span></span><span class="punctuation section arguments end python">)</span></span></span>
<span class="linenumber"><span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta string python"><span class="string quoted double python"><span class="punctuation definition string begin python">&quot;</span></span></span><span class="meta string python"><span class="string quoted double python">b<span class="punctuation definition string end python">&quot;</span></span></span></span><span class="punctuation section arguments end python">)</span></span></span>
<span class="linenumber"><span class="meta statement for python"><span class="keyword control flow for python">for</span> <span class="meta generic-name python">x</span> <span class="meta statement for python"><span class="keyword control flow for in python">in</span></span></span><span class="meta statement for python"> <span class="meta qualified-name python"><span class="meta generic-name python">y</span></span><span class="punctuation section block for python">:</span></span></span>
<span class="linenumber">    <span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta qualified-name python"><span class="meta generic-name python">x</span></span></span><span class="punctuation section arguments end python">)</span></span></span></span>

Debugging Stuff

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

        -- title

Rust Syntax Highlighting With Classes And Line Numbers


-- code
-- rust

//! ```cargo
//! [dependencies]
//! syntect = "5.1.0"

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

fn main() {

  let current_code = r#"print("a")
print("b")
for x in y:
    print(x)"#;

  let code_type = "python";
  let syntax_set = SyntaxSet::load_defaults_newlines();
  let syntax = syntax_set.find_syntax_by_token(code_type).unwrap();
  let mut html_generator = ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced);
  for line in LinesWithEndings::from(current_code) {
      let _ = html_generator.parse_html_for_line_which_includes_newline(line);
  }
  let initial_html = html_generator.finalize();
  let output_html: Vec<_> = initial_html.lines()
    .map(|line| 
        format!(r#"<span class="linenumber">{}</span>"#, line)).collect();

  println!("{}", output_html.join("\n"));
}

-- results/

<span class="linenumber"><span class="source python"><span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta string python"><span class="string quoted double python"><span class="punctuation definition string begin python">&quot;</span></span></span><span class="meta string python"><span class="string quoted double python">a<span class="punctuation definition string end python">&quot;</span></span></span></span><span class="punctuation section arguments end python">)</span></span></span>
<span class="linenumber"><span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta string python"><span class="string quoted double python"><span class="punctuation definition string begin python">&quot;</span></span></span><span class="meta string python"><span class="string quoted double python">b<span class="punctuation definition string end python">&quot;</span></span></span></span><span class="punctuation section arguments end python">)</span></span></span>
<span class="linenumber"><span class="meta statement for python"><span class="keyword control flow for python">for</span> <span class="meta generic-name python">x</span> <span class="meta statement for python"><span class="keyword control flow for in python">in</span></span></span><span class="meta statement for python"> <span class="meta qualified-name python"><span class="meta generic-name python">y</span></span><span class="punctuation section block for python">:</span></span></span>
<span class="linenumber">    <span class="meta function-call python"><span class="meta qualified-name python"><span class="support function builtin python">print</span></span><span class="punctuation section arguments begin python">(</span><span class="meta function-call arguments python"><span class="meta qualified-name python"><span class="meta generic-name python">x</span></span></span><span class="punctuation section arguments end python">)</span></span></span></span>

-- /results




-- metadata
-- date: 2024-01-16 01:36:25
-- id: 2b1ep9iz
-- type: example
-- status: published