2 releases

Uses new Rust 2021

0.1.1-alpha Aug 28, 2022
0.1.0-alpha Aug 28, 2022

#660 in Development tools

Download history 44/week @ 2022-08-28 27/week @ 2022-09-04 10/week @ 2022-09-11 10/week @ 2022-09-18 13/week @ 2022-09-25 26/week @ 2022-10-02 8/week @ 2022-10-09 6/week @ 2022-10-16 17/week @ 2022-10-23 15/week @ 2022-10-30 18/week @ 2022-11-06 14/week @ 2022-11-13

64 downloads per month
Used in 3 crates

MIT license

15KB
206 lines

Highlighter

An extendable syntax highlighter written in Rust.

Installation

Simply add highlighter to your dependencies:

[dependencies]
highlighter = "0.1.1-alpha"

Basic Usage

use highlighter::{highlight_language, HighlightTargetHtml};

fn main() {
    let result = highlight_language("brainheck", "++++++++ set current cell to 8").unwrap().unwrap();
    let html = HighlightTargetHtml::new()
        .build(result);
    println!(html);
}

Implementing Languages

To extend Highlighter, make a structure which implements the Language trait.

extern crate highlighter;

use highlighter::{core::language::{Language, Scope}, highlight, HighlighterTargetHtml};

/// My example programming language
pub struct MyLanguage;

impl Language for MyLanguage {
    fn name(&self) -> String {
        "my-language".to_string()
    }
    
    fn init(&self, cx: &mut highlighter::core::LexerContext) -> Result<(), highlighter::core::Error> {
        // Initialize the tokens in your language.
        cx.token(Scope::KeywordControl, "\\b(if|else|while|continue|break|return)\\b")?;
        cx.token(Scope::StorageType, "\\b(var|function)\\b")?;
        cx.token(Scope::ConstantNumber, "\\b([0-9][0-9_]*)\\b")?;
        cx.token(Scope::ConstantLanguage, "\\b(true|false)\\b")?;
        Ok(())
    }
}

fn main() {
    let tokens = highlight(MyLanguage, "var i = 0;").unwrap();
    let html = HighlighterTargetHtml::new()
        .build(tokens);
    
    println!("{}", html);
}

Dependencies

~1–1.3MB
~37K SLoC