Crates.io | cmark-syntax |
lib.rs | cmark-syntax |
version | 0.5.1 |
source | src |
created_at | 2021-12-13 23:34:29.298823 |
updated_at | 2024-07-12 19:47:04.167686 |
description | Syntax highlighting for pulldown-cmark |
homepage | https://github.com/grego/cmark-syntax |
repository | https://github.com/grego/cmark-syntax |
max_upload_size | |
id | 497467 |
size | 69,452 |
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.
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!
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.
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
.