2 个版本
0.1.1-alpha | 2021 年 12 月 14 日 |
---|---|
0.1.0-alpha | 2021 年 12 月 13 日 |
#2702 在 解析器实现
9KB
129 行
tack
tack 是一个输出 HTML 的最小化语法高亮器。
注意:此库为 alpha 版本,API 可能会进行破坏性更改。 更多语言即将到来!
特性
- 基于类的
- 最小化 HTML
支持的语言
- CSS
要使用功能更全面的语法高亮器,请使用 syntect
。
示例
body {
display: flex;
flex-direction: column;
min-height: 100%;
}
tack 使用方法
fn main() {
let s = "
body {
display: flex;
min-height: 100%;
}".to_string();
let html = tack::coloured_html(s);
println!("{}", html);
}
tack 输出
<span class="tag">body</span> {
<span class="property">display</span>: <span class="string">flex</span>;
<span class="property">min-height</span>: <span class="numeric">100%</span>;
}
syntect
使用方法
use syntect::{
html::{ClassStyle, ClassedHTMLGenerator},
parsing::SyntaxSet,
util::LinesWithEndings,
};
fn main() {
let ss = SyntaxSet::load_defaults_newlines();
let s = "
body {
display: flex;
min-height: 100%;
}";
let syntax_ref = ss.find_syntax_by_extension("css").unwrap();
let mut html_generator =
ClassedHTMLGenerator::new_with_class_style(syntax_ref, &ss, ClassStyle::Spaced);
for line in LinesWithEndings::from(s) {
html_generator.parse_html_for_line_which_includes_newline(line);
}
let html = html_generator.finalize();
println!("{}", html);
}
syntect
输出
<span class="source css">
<span class="meta selector css"><span class="entity name tag css">body</span> </span><span
class="meta property-list css"><span class="punctuation section property-list css">{</span>
<span class="meta property-name css"><span class="support type property-name css">display</span></span><span
class="punctuation separator key-value css">:</span><span class="meta property-value css"> </span><span
class="meta property-value css"><span class="support constant property-value css">flex</span></span><span
class="punctuation terminator rule css">;</span>
<span class="meta property-name css"><span class="support type property-name css">min-height</span></span><span
class="punctuation separator key-value css">:</span><span class="meta property-value css"> </span><span
class="meta property-value css"><span class="constant numeric css">100<span
class="keyword other unit css">%</span></span></span><span class="punctuation terminator rule css">;</span>
</span><span class="punctuation section property-list css">}</span></span>