hlight

Crates.iohlight
lib.rshlight
version0.0.1
sourcesrc
created_at2023-05-29 23:40:29.260519
updated_at2023-05-30 20:58:57.80728
descriptionThis library is dedicated to delivering exceptional syntax highlighting capabilities.
homepage
repositoryhttps://github.com/2moe/hlight
max_upload_size
id877390
size223,873
Moe (2moe)

documentation

README

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;
}
Commit count: 15

cargo fmt