#syntax-highlighting #syntax #highlighting #dedicated #capabilities #set #load

hlight

This library is dedicated to delivering exceptional syntax highlighting capabilities

1 unstable release

0.0.1 May 30, 2023
0.0.0 May 29, 2023

#20 in #dedicated


Used in 3 crates (via glossa-codegen)

Apache-2.0

215KB
347 lines

hlight

A library for output syntax highlighting.

Quick Start

add dep

cargo add hlight

print to stdout

use hlight::{gen_syntax_highlight, theme::theme_ayu_dark, HighLightRes};


let s: &str = r#"
[main]
enabled = false
"😎" = "🍥"
float = nan
"#;

let mut res = HighLightRes::default().with_background(false);
// theme_ayu_dark: Cow::from("ayu-dark")
*res.get_name_mut() = theme_ayu_dark();

gen_syntax_highlight("toml", s, Some(&res), None)
    .expect("Failed to get highlighted toml text");

output:

toml.svg

write to file

use std::fs::File;

let mut file = File::create("test.txt").expect("Failed to create test.txt");
gen_syntax_highlight("toml", s, Some(&res), Some(&mut file))
    .expect("Unable to write syntax-highlighted text to file.")

Advanced

Load custom set

The ["preset-syntax-set", "preset-theme-set"] features are enabled by default. If you want to customize the set, you don't need to load these features.

add deps:

cargo add hlight --no-default-features
cargo add once_cell

theme-set

use std::borrow::Cow;
use hlight::{theme::load_theme_set, HighLightRes};

fn main() {
    const THEMES: &[u8] = include_bytes!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/assets/theme-set.packdump"
    ));
    let set = load_theme_set(Some(THEMES));

    let mut res = HighLightRes::default();

    *res.get_theme_set_mut() = &set;
    *res.get_name_mut() = Cow::from("Custom-theme-name");
}

syntax-set

use hlight::{
    syntax::{load_syntax_set, SyntaxSet},
    HighLightRes,
};
// use std::cell::OnceCell;
use once_cell::sync::OnceCell;

const SYNTAXES: &[u8] = include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/assets/theme-syntax-set/syntax-set.packdump"
));

fn static_syntax_set() -> &'static SyntaxSet {
    static S: OnceCell<SyntaxSet> = OnceCell::new();
    S.get_or_init(|| load_syntax_set(Some(SYNTAXES)))
}

fn main() {
    let set = static_syntax_set();

    let mut res = HighLightRes::default();
    *res.get_syntax_set_mut() = set;
}

Dependencies

~5–14MB
~158K SLoC