1个不稳定版本
0.1.1-alpha | 2022年8月28日 |
---|
#13 in #高亮器
用于 highlighter
20KB
274 代码行(不含注释)
Highlighter
用Rust编写的可扩展语法高亮器。
安装
只需将highlighter添加到依赖项中
[dependencies]
highlighter = "0.1.1-alpha"
基本用法
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);
}
实现语言
要扩展Highlighter,创建一个实现了Language
特征的架构。
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);
}
依赖项
~2–3MB
~53K 额外代码行数