highlighter-languages 0.1.1-alpha

Officially maintained language extensions for Highlighter.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 5.67 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.29 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 23s Average build duration of successful builds.
  • all releases: 23s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trimorphdev/highlighter
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trimorphdev

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);
}