Diffstat (limited to 'src/syntax_highlight.rs')
1 file changed, 877 insertions(+), 248 deletions(-)
@@ -1,284 +1,913 @@
use std::{
cell::RefCell,
collections::HashMap,
fmt::Write as FmtWrite,
io::{ErrorKind, Write as IoWrite},
path::Path,
sync::LazyLock,
};
use comrak::adapters::SyntaxHighlighterAdapter;
use tree_sitter_grammar_repository::{Grammar, Language};
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter};
thread_local! {
static HIGHLIGHTER: RefCell<Highlighter> = RefCell::new(Highlighter::new());
}
macro_rules! count {
() => (0);
($e:expr) => (1);
($e:expr, $($rest:expr),*) => (1 + count!($($rest),*));
}
macro_rules! define_classes {
($($name:literal => $class:literal),*,) => {
static HIGHLIGHT_NAMES: [&str; count!($($name),*)] = [
$($name),*
];
static HIGHLIGHT_CLASSES: [&str; count!($($name),*)] = [
$($class),*
];
};
}
define_classes! {
"attribute" => "attribute",
"boolean" => "boolean",
"carriage-return" => "carriage-return",
"comment" => "comment",
"comment.documentation" => "comment documentation",
"constant" => "constant",
"constant.builtin" => "constant builtin",
"constructor" => "constructor",
"constructor.builtin" => "constructor builtin",
"embedded" => "embedded",
"error" => "error",
"escape" => "escape",
"function" => "function",
"function.builtin" => "function builtin",
"keyword" => "keyword",
"markup" => "markup",
"markup.bold" => "markup bold",
"markup.heading" => "markup heading",
"markup.italic" => "markup italic",
"markup.link" => "markup link",
"markup.link.url" => "markup link url",
"markup.list" => "markup list",
"markup.list.checked" => "markup list checked",
"markup.list.numbered" => "markup list numbered",
"markup.list.unchecked" => "markup list unchecked",
"markup.list.unnumbered" => "markup list unnumbered",
"markup.quote" => "markup quote",
"markup.raw" => "markup raw",
"markup.raw.block" => "markup raw block",
"markup.raw.inline" => "markup raw inline",
"markup.strikethrough" => "markup strikethrough",
"module" => "module",
"number" => "number",
"operator" => "operator",
"property" => "property",
"property.builtin" => "property builtin",
"punctuation" => "punctuation",
"punctuation.bracket" => "punctuation bracket",
"punctuation.delimiter" => "punctuation delimiter",
"punctuation.special" => "punctuation special",
"string" => "string",
"string.escape" => "string escape",
"string.regexp" => "string regexp",
"string.special" => "string special",
"string.special.symbol" => "string special symbol",
"tag" => "tag",
"type" => "type",
"type.builtin" => "type builtin",
"variable" => "variable",
"variable.builtin" => "variable builtin",
"variable.member" => "variable member",
"variable.parameter" => "variable parameter",}
pub fn prime_highlighters() {
let _res = HIGHLIGHTER_CONFIGS.len();
}
static HIGHLIGHTER_CONFIGS: LazyLock<Vec<HighlightConfiguration>> = LazyLock::new(|| {
Grammar::VARIANTS
.iter()
.copied()
.map(Grammar::highlight_configuration_params)
.map(|v| {
let mut configuration = HighlightConfiguration::new(
v.language.into(),
v.name,
v.highlights_query,
v.injection_query,
v.locals_query,
)
.unwrap_or_else(|e| panic!("bad query for {}: {e}", v.name));
configuration.configure(&HIGHLIGHT_NAMES);
configuration
})
.collect()
});
use std::path::Path;
const IMPORTS: &[&str] = &[
"use", "mod", "extern", "import", "from", "export", "package", "require", "include",
];
const KEYWORDS: &[&str] = &[
"fn",
"let",
"mut",
"pub",
"priv",
"struct",
"enum",
"impl",
"trait",
"type",
"where",
"for",
"in",
"while",
"loop",
"if",
"else",
"match",
"return",
"break",
"continue",
"async",
"await",
"move",
"ref",
"static",
"const",
"unsafe",
"dyn",
"as",
"local",
"function",
"then",
"elseif",
"end",
"do",
"repeat",
"until",
"and",
"or",
"not",
"def",
"class",
"try",
"except",
"finally",
"with",
"yield",
"lambda",
"pass",
"raise",
"switch",
"case",
"default",
"var",
"interface",
"go",
"defer",
"select",
"chan",
"fallthrough",
"elif",
"fi",
"done",
"esac",
];
const BOOLEANS: &[&str] = &["true", "false", "nil", "null", "undefined"];
const BUILTIN_TYPES: &[&str] = &[
"u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize", "f32",
"f64", "bool", "str", "char", "int", "float", "double", "void", "auto", "string", "number",
"boolean", "any",
];
const BUILTIN_CONSTS: &[&str] = &["Some", "None", "Ok", "Err"];
const BUILTIN_FUNCS: &[&str] = &[
"drop", "print", "ipairs", "pairs", "pcall", "xpcall", "tonumber", "tostring", "assert",
"error", "len", "range", "echo", "set",
];
const BUILTIN_VARS: &[&str] = &["self", "Self", "super", "crate", "this"];
#[derive(Clone, Copy, PartialEq, Eq)]
enum Mode {
Code {
hash_comments: bool,
dash_comments: bool,
rust_like: bool,
},
Config,
Style,
Markup,
Plain,
}
pub fn fetch_highlighter_config(file: &Path) -> Option<&'static HighlightConfiguration> {
Language::from_file_name(file)
.map(Language::grammar)
.map(Grammar::idx)
.map(|idx| &HIGHLIGHTER_CONFIGS[idx])
#[derive(Clone, Copy, PartialEq, Eq)]
enum State {
Normal,
BlockComment,
LuaBlockComment,
DoubleString,
RawString(usize),
}
pub fn highlight_lines(source: &str, path_or_lang: &str) -> Vec<String> {
let mode = detect_mode(path_or_lang, source);
let mut state = State::Normal;
pub fn fetch_highlighter_config_by_token(token: &str) -> Option<&'static HighlightConfiguration> {
Language::from_injection(token)
.map(Language::grammar)
.map(Grammar::idx)
.map(|idx| &HIGHLIGHTER_CONFIGS[idx])
source
.lines()
.map(|line| highlight_line(line, mode, &mut state))
.collect()
}
pub struct ComrakHighlightAdapter;
pub fn highlight_block(source: &str, lang: &str) -> String {
let lines = highlight_lines(source, lang);
format!("<pre><code>{}</code></pre>", lines.join("\n"))
}
impl SyntaxHighlighterAdapter for ComrakHighlightAdapter {
fn write_highlighted(
&self,
output: &mut dyn IoWrite,
lang: Option<&str>,
code: &str,
) -> std::io::Result<()> {
let out = format_file(code, FileIdentifier::Token(lang.unwrap_or_default()))
.map_err(|e| std::io::Error::new(ErrorKind::Other, e))?;
output.write_all(out.as_bytes())
fn detect_mode(path_or_lang: &str, source: &str) -> Mode {
let lower = path_or_lang.trim().to_ascii_lowercase();
let file_name = Path::new(&lower)
.file_name()
.and_then(|s| s.to_str())
.unwrap_or(&lower);
let ext = Path::new(file_name)
.extension()
.and_then(|s| s.to_str())
.unwrap_or(file_name);
if matches!(
file_name,
"justfile"
| "makefile"
| "gnumakefile"
| "dockerfile"
| "containerfile"
| "pkgbuild"
| ".bashrc"
| ".zshrc"
| ".profile"
) {
return Mode::Code {
hash_comments: true,
dash_comments: false,
rust_like: false,
};
}
fn write_pre_tag(
&self,
output: &mut dyn IoWrite,
_attributes: HashMap<String, String>,
) -> std::io::Result<()> {
write!(output, "<pre>")
if matches!(
file_name,
".gitignore" | ".gitmodules" | ".gitattributes" | ".editorconfig" | "cargo.lock"
) {
return Mode::Config;
}
fn write_code_tag(
&self,
_output: &mut dyn IoWrite,
_attributes: HashMap<String, String>,
) -> std::io::Result<()> {
Ok(())
match ext {
"rs" | "rust" | "ron" => Mode::Code {
hash_comments: false,
dash_comments: false,
rust_like: true,
},
"lua" | "sql" => Mode::Code {
hash_comments: false,
dash_comments: true,
rust_like: false,
},
"sh" | "bash" | "zsh" | "fish" | "py" | "python" | "rb" | "ruby" | "nix" | "pl" | "r"
| "just" | "mk" | "cmake" => Mode::Code {
hash_comments: true,
dash_comments: false,
rust_like: false,
},
"c" | "h" | "cpp" | "hpp" | "cc" | "cxx" | "go" | "golang" | "js" | "jsx" | "ts"
| "tsx" | "javascript" | "typescript" | "java" | "cs" | "csharp" | "zig" | "kt"
| "kotlin" | "swift" | "dart" | "php" | "glsl" | "wgsl" | "hlsl" => Mode::Code {
hash_comments: false,
dash_comments: false,
rust_like: false,
},
"toml" | "ini" | "cfg" | "conf" | "yaml" | "yml" | "json" | "jsonc" | "kdl" | "service"
| "timer" | "socket" | "target" | "mount" | "desktop" | "env" => Mode::Config,
"css" | "scss" | "sass" | "less" => Mode::Style,
"html" | "htm" | "xml" | "svg" | "xhtml" => Mode::Markup,
_ if source.starts_with("#!") => Mode::Code {
hash_comments: true,
dash_comments: false,
rust_like: false,
},
_ => Mode::Plain,
}
}
#[derive(Copy, Clone, Debug)]
pub enum FileIdentifier<'a> {
Path(&'a Path),
Token(&'a str),
fn highlight_line(line: &str, mode: Mode, state: &mut State) -> String {
match mode {
Mode::Plain => escape_html(line),
Mode::Config => highlight_config_line(line),
Mode::Style => highlight_style_line(line, state),
Mode::Markup => highlight_markup_line(line, state),
Mode::Code {
hash_comments,
dash_comments,
rust_like,
} => highlight_code_line(line, hash_comments, dash_comments, rust_like, state),
}
}
fn render_line_prefix(tag: bool, line_number: usize) -> String {
if !tag {
return "".to_string();
fn highlight_code_line(
line: &str,
hash_comments: bool,
dash_comments: bool,
rust_like: bool,
state: &mut State,
) -> String {
let bytes = line.as_bytes();
let len = bytes.len();
let mut i = 0;
let mut out = String::with_capacity(len * 2);
let mut prev_keyword = "";
let mut prev_was_dot = false;
let mut in_fn_params = false;
let mut paren_depth: usize = 0;
while i < len {
match *state {
State::BlockComment => {
if let Some(pos) = line[i..].find("*/") {
let end = i + pos + 2;
push_span(&mut out, "hl-comment", &line[i..end]);
i = end;
*state = State::Normal;
} else {
push_span(&mut out, "hl-comment", &line[i..]);
return out;
}
continue;
}
State::LuaBlockComment => {
if let Some(pos) = line[i..].find("]]") {
let end = i + pos + 2;
push_span(&mut out, "hl-comment", &line[i..end]);
i = end;
*state = State::Normal;
} else {
push_span(&mut out, "hl-comment", &line[i..]);
return out;
}
continue;
}
State::DoubleString => {
i = scan_double_string(line, i, &mut out, state);
continue;
}
State::RawString(hashes) => {
i = scan_raw_string(line, i, hashes, &mut out, state);
continue;
}
State::Normal => {}
}
let b = bytes[i];
if b.is_ascii_whitespace() {
let start = i;
while i < len && bytes[i].is_ascii_whitespace() {
i += 1;
}
out.push_str(&line[start..i]);
continue;
}
// `//` and `/*` comments
if b == b'/' && i + 1 < len {
if bytes[i + 1] == b'/' {
push_span(&mut out, "hl-comment", &line[i..]);
break;
}
if bytes[i + 1] == b'*' {
*state = State::BlockComment;
continue;
}
}
// Lua `--` and `--[[` comments
if dash_comments && b == b'-' && i + 1 < len && bytes[i + 1] == b'-' {
if line[i + 2..].starts_with("[[") {
*state = State::LuaBlockComment;
continue;
}
push_span(&mut out, "hl-comment", &line[i..]);
break;
}
// `#` attributes, comments, or preprocessor directives
if b == b'#' {
if rust_like
&& i + 1 < len
&& (bytes[i + 1] == b'[' || bytes[i + 1] == b'!')
&& let Some(close) = line[i..].find(']')
{
let end = i + close + 1;
push_span(&mut out, "hl-attribute", &line[i..end]);
i = end;
continue;
}
if hash_comments || (i == 0 && line.starts_with("#!")) {
push_span(&mut out, "hl-comment", &line[i..]);
break;
}
if !rust_like {
let start = i;
i += 1;
while i < len && bytes[i].is_ascii_whitespace() {
i += 1;
}
while i < len && bytes[i].is_ascii_alphabetic() {
i += 1;
}
push_span(&mut out, "hl-macro", &line[start..i]);
continue;
}
}
// Rust raw strings `r"..."`, `r#"..."#`
if rust_like && b == b'r' && i + 1 < len && (bytes[i + 1] == b'"' || bytes[i + 1] == b'#') {
let mut hashes = 0;
let mut j = i + 1;
while j < len && bytes[j] == b'#' {
hashes += 1;
j += 1;
}
if j < len && bytes[j] == b'"' {
push_span(&mut out, "hl-string", &line[i..=j]);
*state = State::RawString(hashes);
i = j + 1;
continue;
}
}
if b == b'"' {
push_span(&mut out, "hl-string", "\"");
*state = State::DoubleString;
i = scan_double_string(line, i + 1, &mut out, state);
prev_was_dot = false;
continue;
}
if b == b'\'' {
if rust_like {
if let Some((end, is_lifetime)) = scan_rust_quote(line, i) {
let class = if is_lifetime { "hl-label" } else { "hl-escape" };
push_span(&mut out, class, &line[i..end]);
i = end;
prev_was_dot = false;
continue;
}
} else {
let end = scan_single_string(line, i);
push_span(&mut out, "hl-string", &line[i..end]);
i = end;
prev_was_dot = false;
continue;
}
}
if b.is_ascii_digit() {
let start = i;
i += 1;
while i < len
&& (bytes[i].is_ascii_alphanumeric()
|| bytes[i] == b'_'
|| (bytes[i] == b'.' && i + 1 < len && bytes[i + 1].is_ascii_digit()))
{
i += 1;
}
push_span(&mut out, "hl-number", &line[start..i]);
prev_was_dot = false;
continue;
}
if is_ident_start(b) {
let start = i;
i += 1;
while i < len && is_ident_continue(bytes[i]) {
i += 1;
}
let ident = &line[start..i];
let rest = &line[i..];
if rest.starts_with('!') && !rest.starts_with("!=") {
i += 1;
push_span(&mut out, "hl-macro", &line[start..i]);
prev_was_dot = false;
continue;
}
let class = classify_ident(
ident,
rest.trim_start(),
prev_keyword,
prev_was_dot,
in_fn_params && paren_depth == 1,
);
push_span(&mut out, class, ident);
if matches!(ident, "fn" | "function" | "def") {
in_fn_params = true;
}
prev_keyword = if matches!(class, "hl-keyword" | "hl-include") {
ident
} else {
""
};
prev_was_dot = false;
continue;
}
if matches!(b, b'(' | b')' | b'[' | b']' | b'{' | b'}' | b',' | b';') {
match b {
b'(' => paren_depth += 1,
b')' => {
paren_depth = paren_depth.saturating_sub(1);
if paren_depth == 0 {
in_fn_params = false;
}
}
b'{' | b';' => in_fn_params = false,
_ => {}
}
push_span(&mut out, "hl-punctuation", &line[i..=i]);
prev_was_dot = false;
i += 1;
continue;
}
if b == b':' {
let len_colon = if i + 1 < len && bytes[i + 1] == b':' {
2
} else {
1
};
push_span(&mut out, "hl-punctuation", &line[i..i + len_colon]);
prev_was_dot = false;
i += len_colon;
continue;
}
if b == b'.' {
if i + 1 < len && bytes[i + 1] == b'.' {
let end = if i + 2 < len && bytes[i + 2] == b'=' {
i + 3
} else {
i + 2
};
push_span(&mut out, "hl-operator", &line[i..end]);
prev_was_dot = false;
i = end;
} else {
push_span(&mut out, "hl-punctuation", ".");
prev_was_dot = true;
i += 1;
}
continue;
}
if is_operator_byte(b) {
let start = i;
i += 1;
while i < len && is_operator_byte(bytes[i]) {
i += 1;
}
push_span(&mut out, "hl-operator", &line[start..i]);
prev_was_dot = false;
continue;
}
let ch = line[i..].chars().next().unwrap();
let end = i + ch.len_utf8();
escape_into(&mut out, &line[i..end]);
prev_was_dot = false;
i = end;
}
format!("<span class='lnb'><a id='n{0}' href='#n{0}'>{0}</a></span><code>", line_number)
out
}
fn classify_ident(
ident: &str,
trimmed_rest: &str,
prev_keyword: &str,
prev_was_dot: bool,
in_fn_params: bool,
) -> &'static str {
if !prev_was_dot {
if IMPORTS.contains(&ident) {
return "hl-include";
}
if KEYWORDS.contains(&ident) {
return "hl-keyword";
}
if BOOLEANS.contains(&ident) {
return "hl-boolean";
}
if BUILTIN_TYPES.contains(&ident) {
return "hl-type-builtin";
}
if BUILTIN_CONSTS.contains(&ident) {
return "hl-constant";
}
if BUILTIN_FUNCS.contains(&ident) {
return "hl-function-builtin";
}
if BUILTIN_VARS.contains(&ident) {
return "hl-variable";
}
}
if matches!(prev_keyword, "fn" | "function" | "def") {
return "hl-function";
}
if matches!(
prev_keyword,
"struct" | "enum" | "trait" | "type" | "class" | "interface"
) {
return "hl-type";
}
if prev_keyword == "mod" {
return "hl-module";
}
if ident.as_bytes()[0].is_ascii_uppercase() {
let is_const = ident.len() > 1
&& ident
.bytes()
.all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_');
return if is_const { "hl-constant" } else { "hl-type" };
}
fn render_line_suffix(tag: bool, line_number: usize) -> String {
if !tag {
return "\n".to_string();
if trimmed_rest.starts_with("::") && !trimmed_rest.starts_with("::<") {
return "hl-module";
}
"</code>\n".to_string()
if trimmed_rest.starts_with('(') || trimmed_rest.starts_with("::<") {
return "hl-function";
}
if prev_was_dot {
return "hl-property";
}
if trimmed_rest.starts_with(':') && !trimmed_rest.starts_with("::") {
return if in_fn_params {
"hl-parameter"
} else {
"hl-property"
};
}
"hl-variable"
}
fn highlight_config_line(line: &str) -> String {
let trimmed = line.trim_start();
if trimmed.starts_with('#') || trimmed.starts_with("//") || trimmed.starts_with(';') {
let mut out = String::new();
push_span(&mut out, "hl-comment", line);
return out;
}
if trimmed.starts_with('[') && trimmed.ends_with(']') {
let indent = &line[..line.len() - trimmed.len()];
let mut out = String::from(indent);
let open_len = if trimmed.starts_with("[[") { 2 } else { 1 };
let close_len = if trimmed.ends_with("]]") { 2 } else { 1 };
push_span(&mut out, "hl-punctuation", &trimmed[..open_len]);
push_span(
&mut out,
"hl-property",
&trimmed[open_len..trimmed.len() - close_len],
);
push_span(
&mut out,
"hl-punctuation",
&trimmed[trimmed.len() - close_len..],
);
return out;
}
if let Some(sep_idx) = find_config_sep(line) {
let key_part = &line[..sep_idx];
let sep = &line[sep_idx..=sep_idx];
let val_part = &line[sep_idx + 1..];
let mut out = String::new();
let key_trim = key_part.trim();
if !key_trim.is_empty() {
let lead = key_part.len() - key_part.trim_start().len();
let trail = key_part.len() - key_part.trim_end().len();
out.push_str(&key_part[..lead]);
push_span(&mut out, "hl-property", key_trim);
out.push_str(&key_part[key_part.len() - trail..]);
} else {
escape_into(&mut out, key_part);
}
let sep_class = if sep == "=" {
"hl-operator"
} else {
"hl-punctuation"
};
push_span(&mut out, sep_class, sep);
let mut state = State::Normal;
out.push_str(&highlight_code_line(
val_part, true, false, false, &mut state,
));
return out;
}
let mut state = State::Normal;
highlight_code_line(line, true, false, false, &mut state)
}
pub fn format_file(content: &str, identifier: FileIdentifier<'_>) -> Result<String. Error> {
let mut out = String::new();
format_file_inner(&mut out, content, identifier, true)?;
Ok(out)
fn find_config_sep(line: &str) -> Option<usize> {
let mut in_quotes = false;
for (i, &b) in line.as_bytes().iter().enumerate() {
if b == b'"' {
in_quotes = !in_quotes;
} else if !in_quotes && (b == b'=' || b == b':') {
return Some(i);
}
}
None
}
pub fn format_file_inner(
out: &mut String,
content: &str,
identifier: FileIdentifier<'_>,
code_tag: bool,
) -> anyhow::Result<()> {
let config = match identifier {
FileIdentifier::Path(v) => fetch_highlighter_config(v),
FileIdentifier::Token(v) => fetch_highlighter_config_by_token(v),
};
let Some(config) = config else {
for (i, line) in content.lines().enumerate() {
out.push_str(&render_line_prefix(code_tag, i));
v_htmlescape::b_escape(line.as_bytes(), out);
out.push_str(&render_line_suffix(code_tag, i));
}
return Ok(());
};
let mut line_number = 0;
HIGHLIGHTER.with_borrow_mut(|highlighter| {
highlighter.parser().reset();
let spans = highlighter.highlight(config, content.as_bytes(), None, |injection| {
debug!(injection, "Highlighter switch requested");
fetch_highlighter_config_by_token(injection)
});
let mut spans = match spans {
Ok(v) => v,
Err(error) => {
error!(
?error,
"Failed to run highlighter, falling back to plaintext"
);
for line in content.lines() {
line_number = line_number + 1;
out.push_str(&render_line_prefix(code_tag, line_number));
v_htmlescape::b_escape(line.as_bytes(), out);
out.push_str(&render_line_suffix(code_tag, line_number));
}
fn highlight_style_line(line: &str, state: &mut State) -> String {
let trimmed = line.trim_start();
if *state == State::BlockComment || trimmed.starts_with("/*") {
return highlight_code_line(line, false, false, false, state);
}
if let Some(colon) = line.find(':')
&& !trimmed.starts_with(['.', '#', '@'])
&& !line[..colon].contains('{')
{
let prop = &line[..colon];
let rest = &line[colon + 1..];
let mut out = String::new();
let lead = prop.len() - prop.trim_start().len();
out.push_str(&prop[..lead]);
push_span(&mut out, "hl-property", prop.trim_start());
push_span(&mut out, "hl-punctuation", ":");
out.push_str(&highlight_code_line(rest, false, false, false, state));
return out;
}
highlight_code_line(line, false, false, false, state)
}
return Ok(());
fn highlight_markup_line(line: &str, state: &mut State) -> String {
let bytes = line.as_bytes();
let len = bytes.len();
let mut i = 0;
let mut out = String::with_capacity(len * 2);
while i < len {
if *state == State::BlockComment {
if let Some(pos) = line[i..].find("-->") {
let end = i + pos + 3;
push_span(&mut out, "hl-comment", &line[i..end]);
i = end;
*state = State::Normal;
} else {
push_span(&mut out, "hl-comment", &line[i..]);
return out;
}
};
continue;
}
let mut tag_open = true;
line_number = line_number + 1;
out.push_str(&render_line_prefix(code_tag, line_number));
while let Some(span) = spans.next().transpose()? {
if !tag_open {
line_number = line_number + 1;
out.push_str(&render_line_prefix(code_tag, line_number));
tag_open = true;
}
match span {
HighlightEvent::Source { start, end } => {
let content = &content[start..end];
for (i, line) in content.lines().enumerate() {
if i != 0 {
out.push_str(&render_line_suffix(code_tag, line_number));
line_number = line_number + 1;
out.push_str(&render_line_prefix(code_tag, line_number));
}
v_htmlescape::b_escape(line.as_bytes(), out);
}
if line[i..].starts_with("<!--") {
*state = State::BlockComment;
continue;
}
if bytes[i] == b'<' {
let start = i;
i += 1;
if i < len && matches!(bytes[i], b'/' | b'!' | b'?') {
i += 1;
}
push_span(&mut out, "hl-tag-delimiter", &line[start..i]);
let tag_start = i;
while i < len && (bytes[i].is_ascii_alphanumeric() || matches!(bytes[i], b'-' | b':')) {
i += 1;
}
if i > tag_start {
push_span(&mut out, "hl-tag", &line[tag_start..i]);
}
if content.ends_with('\n') {
out.push_str(&render_line_suffix(code_tag, line_number));
tag_open = false;
while i < len && bytes[i] != b'>' {
if bytes[i].is_ascii_whitespace() {
out.push(bytes[i] as char);
i += 1;
} else if matches!(bytes[i], b'"' | b'\'') {
let quote = bytes[i];
let s_start = i;
i += 1;
while i < len && bytes[i] != quote {
i += 1;
}
}
HighlightEvent::HighlightStart(highlight) => {
write!(
out,
r#"<span class="highlight {}">"#,
HIGHLIGHT_CLASSES[highlight.0]
)?;
}
HighlightEvent::HighlightEnd => {
out.push_str("</span>");
if i < len {
i += 1;
}
push_span(&mut out, "hl-string", &line[s_start..i]);
} else if bytes[i] == b'=' {
push_span(&mut out, "hl-operator", "=");
i += 1;
} else if bytes[i] == b'/' && i + 1 < len && bytes[i + 1] == b'>' {
push_span(&mut out, "hl-tag-delimiter", "/>");
i += 2;
break;
} else {
let attr_start = i;
while i < len
&& !bytes[i].is_ascii_whitespace()
&& !matches!(bytes[i], b'=' | b'>' | b'/' | b'"' | b'\'')
{
i += 1;
}
if i > attr_start {
push_span(&mut out, "hl-label", &line[attr_start..i]);
} else {
escape_into(&mut out, &line[i..=i]);
i += 1;
}
}
}
if i < len && bytes[i] == b'>' {
push_span(&mut out, "hl-tag-delimiter", ">");
i += 1;
}
continue;
}
if tag_open {
out.push_str(&render_line_suffix(code_tag, line_number));
let next_lt = line[i..].find('<').map(|p| i + p).unwrap_or(len);
escape_into(&mut out, &line[i..next_lt]);
i = next_lt;
}
out
}
fn scan_double_string(line: &str, mut i: usize, out: &mut String, state: &mut State) -> usize {
let bytes = line.as_bytes();
let len = bytes.len();
let mut seg_start = i;
while i < len {
if bytes[i] == b'\\' && i + 1 < len {
if i > seg_start {
push_span(out, "hl-string", &line[seg_start..i]);
}
let esc_end = (i + 2).min(len);
push_span(out, "hl-escape", &line[i..esc_end]);
i = esc_end;
seg_start = i;
} else if bytes[i] == b'"' {
push_span(out, "hl-string", &line[seg_start..=i]);
*state = State::Normal;
return i + 1;
} else {
i += line[i..].chars().next().unwrap().len_utf8();
}
}
if seg_start < len {
push_span(out, "hl-string", &line[seg_start..len]);
}
len
}
fn scan_raw_string(
line: &str,
mut i: usize,
hashes: usize,
out: &mut String,
state: &mut State,
) -> usize {
let bytes = line.as_bytes();
let len = bytes.len();
let start = i;
while i < len {
if bytes[i] == b'"' {
let mut h = 0;
while i + 1 + h < len && bytes[i + 1 + h] == b'#' && h < hashes {
h += 1;
}
if h == hashes {
let end = i + 1 + hashes;
push_span(out, "hl-string", &line[start..end]);
*state = State::Normal;
return end;
}
}
i += 1;
}
if start < len {
push_span(out, "hl-string", &line[start..len]);
}
len
}
fn scan_single_string(line: &str, start: usize) -> usize {
let bytes = line.as_bytes();
let len = bytes.len();
let mut i = start + 1;
while i < len {
if bytes[i] == b'\\' && i + 1 < len {
i += 2;
} else if bytes[i] == b'\'' {
return i + 1;
} else {
i += 1;
}
}
len
}
fn scan_rust_quote(line: &str, start: usize) -> Option<(usize, bool)> {
let rest = &line[start + 1..];
if let Some(escaped) = rest.strip_prefix('\\')
&& let Some(close) = escaped.find('\'')
{
return Some((start + 2 + close + 1, false));
}
let mut chars = rest.char_indices();
if let Some((_, first)) = chars.next() {
if let Some((next_idx, '\'')) = chars.next() {
return Some((start + 1 + next_idx + 1, false));
}
if first.is_ascii_alphabetic() || first == '_' {
let mut end = first.len_utf8();
for (idx, ch) in rest[end..].char_indices() {
if ch.is_ascii_alphanumeric() || ch == '_' {
end = first.len_utf8() + idx + ch.len_utf8();
} else {
break;
}
}
return Some((start + 1 + end, true));
}
}
None
}
Ok::<_, anyhow::Error>(())
})?;
fn is_ident_start(b: u8) -> bool {
b.is_ascii_alphabetic() || b == b'_'
}
fn is_ident_continue(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_'
}
fn is_operator_byte(b: u8) -> bool {
matches!(
b,
b'=' | b'+'
| b'-'
| b'*'
| b'/'
| b'%'
| b'&'
| b'|'
| b'^'
| b'!'
| b'<'
| b'>'
| b'?'
| b'~'
| b'@'
)
}
fn push_span(out: &mut String, class: &str, text: &str) {
if text.is_empty() {
return;
}
out.push_str("<span class=\"");
out.push_str(class);
out.push_str("\">");
escape_into(out, text);
out.push_str("</span>");
}
fn escape_html(text: &str) -> String {
let mut out = String::with_capacity(text.len());
escape_into(&mut out, text);
out
}
Ok(())
fn escape_into(out: &mut String, text: &str) {
v_htmlescape::b_escape(text.as_bytes(), out);
}