12 releases

0.5.1 Jul 12, 2024
0.5.0 Jul 12, 2024
0.4.1 Dec 11, 2022
0.3.1 Jun 6, 2022
0.1.2 Dec 14, 2021

#1843 in Parser implementations

Download history 1/week @ 2024-09-02 54/week @ 2024-09-09 22/week @ 2024-09-16 26/week @ 2024-09-23 8/week @ 2024-09-30 7/week @ 2024-10-07 26/week @ 2024-10-14 52/week @ 2024-11-04 3/week @ 2024-11-18

55 downloads per month
Used in 2 crates (via blades)

GPL-3.0-or-later

21KB
413 lines

cmark syntax highlighting

Crates.io status Docs

This crate provides a preprocessor for pulldown_cmark events that implements syntax highlighting. It is based on the work of Maciej Hirsz for the Ramhorns templating engine.

Supported languages

  • Rust
  • JavaScript
  • sh shell
  • TOML

Files defining language syntax are located in src/languages directory. The syntax is defined using regexes, which the Logos procedural macro turns into a lexer at the compile time. PRs implementing new languages are very welcome!

Features

With latex2mathml feature enabled, blocks denoted by math containing LaTeX formulas are rendered into MathML in block mode and analogously for inline code delimited by $ at the start and the end in inline mode.

Use

This preprocessor can be used as a callback for the Ramhorns templating engine.

use ramhorns::encoding::Encoder;

pub fn encode<E: Encoder>(source: &str, encoder: &mut E) -> Result<(), E::Error> {
    let parser = pulldown_cmark::Parser::new(source);
    let processed = cmark_syntax::SyntaxPreprocessor::new(parser);
    encoder.write_html(processed)
}

The emitted HTML consists of span tags with the following class names: glyph, literal, identifier, special-identifier, strong-identifier, keyword, comment.

Dependencies

~4MB
~43K SLoC