| 1 |
use std::path::Path; |
| 2 |
|
| 3 |
const IMPORTS: &[&str] = &[ |
| 4 |
"use", "mod", "extern", "import", "from", "export", "package", "require", "include", |
| 5 |
]; |
| 6 |
|
| 7 |
const KEYWORDS: &[&str] = &[ |
| 8 |
"fn", |
| 9 |
"let", |
| 10 |
"mut", |
| 11 |
"pub", |
| 12 |
"priv", |
| 13 |
"struct", |
| 14 |
"enum", |
| 15 |
"impl", |
| 16 |
"trait", |
| 17 |
"type", |
| 18 |
"where", |
| 19 |
"for", |
| 20 |
"in", |
| 21 |
"while", |
| 22 |
"loop", |
| 23 |
"if", |
| 24 |
"else", |
| 25 |
"match", |
| 26 |
"return", |
| 27 |
"break", |
| 28 |
"continue", |
| 29 |
"async", |
| 30 |
"await", |
| 31 |
"move", |
| 32 |
"ref", |
| 33 |
"static", |
| 34 |
"const", |
| 35 |
"unsafe", |
| 36 |
"dyn", |
| 37 |
"as", |
| 38 |
"local", |
| 39 |
"function", |
| 40 |
"then", |
| 41 |
"elseif", |
| 42 |
"end", |
| 43 |
"do", |
| 44 |
"repeat", |
| 45 |
"until", |
| 46 |
"and", |
| 47 |
"or", |
| 48 |
"not", |
| 49 |
"def", |
| 50 |
"class", |
| 51 |
"try", |
| 52 |
"except", |
| 53 |
"finally", |
| 54 |
"with", |
| 55 |
"yield", |
| 56 |
"lambda", |
| 57 |
"pass", |
| 58 |
"raise", |
| 59 |
"switch", |
| 60 |
"case", |
| 61 |
"default", |
| 62 |
"var", |
| 63 |
"interface", |
| 64 |
"go", |
| 65 |
"defer", |
| 66 |
"select", |
| 67 |
"chan", |
| 68 |
"fallthrough", |
| 69 |
"elif", |
| 70 |
"fi", |
| 71 |
"done", |
| 72 |
"esac", |
| 73 |
]; |
| 74 |
|
| 75 |
const BOOLEANS: &[&str] = &["true", "false", "nil", "null", "undefined"]; |
| 76 |
|
| 77 |
const BUILTIN_TYPES: &[&str] = &[ |
| 78 |
"u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize", "f32", |
| 79 |
"f64", "bool", "str", "char", "int", "float", "double", "void", "auto", "string", "number", |
| 80 |
"boolean", "any", |
| 81 |
]; |
| 82 |
|
| 83 |
const BUILTIN_CONSTS: &[&str] = &["Some", "None", "Ok", "Err"]; |
| 84 |
|
| 85 |
const BUILTIN_FUNCS: &[&str] = &[ |
| 86 |
"drop", "print", "ipairs", "pairs", "pcall", "xpcall", "tonumber", "tostring", "assert", |
| 87 |
"error", "len", "range", "echo", "set", |
| 88 |
]; |
| 89 |
|
| 90 |
const BUILTIN_VARS: &[&str] = &["self", "Self", "super", "crate", "this"]; |
| 91 |
|
| 92 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 93 |
enum Mode { |
| 94 |
Code { |
| 95 |
hash_comments: bool, |
| 96 |
dash_comments: bool, |
| 97 |
rust_like: bool, |
| 98 |
}, |
| 99 |
Config, |
| 100 |
Style, |
| 101 |
Markup, |
| 102 |
Plain, |
| 103 |
} |
| 104 |
|
| 105 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 106 |
enum State { |
| 107 |
Normal, |
| 108 |
BlockComment, |
| 109 |
LuaBlockComment, |
| 110 |
DoubleString, |
| 111 |
RawString(usize), |
| 112 |
} |
| 113 |
|
| 114 |
pub fn highlight_lines(source: &str, path_or_lang: &str) -> Vec<String> { |
| 115 |
let mode = detect_mode(path_or_lang, source); |
| 116 |
let mut state = State::Normal; |
| 117 |
|
| 118 |
source |
| 119 |
.lines() |
| 120 |
.map(|line| highlight_line(line, mode, &mut state)) |
| 121 |
.collect() |
| 122 |
} |
| 123 |
|
| 124 |
pub fn highlight_block(source: &str, lang: &str) -> String { |
| 125 |
let lines = highlight_lines(source, lang); |
| 126 |
format!("<pre><code>{}</code></pre>", lines.join("\n")) |
| 127 |
} |
| 128 |
|
| 129 |
fn detect_mode(path_or_lang: &str, source: &str) -> Mode { |
| 130 |
let lower = path_or_lang.trim().to_ascii_lowercase(); |
| 131 |
let file_name = Path::new(&lower) |
| 132 |
.file_name() |
| 133 |
.and_then(|s| s.to_str()) |
| 134 |
.unwrap_or(&lower); |
| 135 |
let ext = Path::new(file_name) |
| 136 |
.extension() |
| 137 |
.and_then(|s| s.to_str()) |
| 138 |
.unwrap_or(file_name); |
| 139 |
|
| 140 |
if matches!( |
| 141 |
file_name, |
| 142 |
"justfile" |
| 143 |
| "makefile" |
| 144 |
| "gnumakefile" |
| 145 |
| "dockerfile" |
| 146 |
| "containerfile" |
| 147 |
| "pkgbuild" |
| 148 |
| ".bashrc" |
| 149 |
| ".zshrc" |
| 150 |
| ".profile" |
| 151 |
) { |
| 152 |
return Mode::Code { |
| 153 |
hash_comments: true, |
| 154 |
dash_comments: false, |
| 155 |
rust_like: false, |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
if matches!( |
| 160 |
file_name, |
| 161 |
".gitignore" | ".gitmodules" | ".gitattributes" | ".editorconfig" | "cargo.lock" |
| 162 |
) { |
| 163 |
return Mode::Config; |
| 164 |
} |
| 165 |
|
| 166 |
match ext { |
| 167 |
"rs" | "rust" | "ron" => Mode::Code { |
| 168 |
hash_comments: false, |
| 169 |
dash_comments: false, |
| 170 |
rust_like: true, |
| 171 |
}, |
| 172 |
"lua" | "sql" => Mode::Code { |
| 173 |
hash_comments: false, |
| 174 |
dash_comments: true, |
| 175 |
rust_like: false, |
| 176 |
}, |
| 177 |
"sh" | "bash" | "zsh" | "fish" | "py" | "python" | "rb" | "ruby" | "nix" | "pl" | "r" |
| 178 |
| "just" | "mk" | "cmake" => Mode::Code { |
| 179 |
hash_comments: true, |
| 180 |
dash_comments: false, |
| 181 |
rust_like: false, |
| 182 |
}, |
| 183 |
"c" | "h" | "cpp" | "hpp" | "cc" | "cxx" | "go" | "golang" | "js" | "jsx" | "ts" |
| 184 |
| "tsx" | "javascript" | "typescript" | "java" | "cs" | "csharp" | "zig" | "kt" |
| 185 |
| "kotlin" | "swift" | "dart" | "php" | "glsl" | "wgsl" | "hlsl" => Mode::Code { |
| 186 |
hash_comments: false, |
| 187 |
dash_comments: false, |
| 188 |
rust_like: false, |
| 189 |
}, |
| 190 |
"toml" | "ini" | "cfg" | "conf" | "yaml" | "yml" | "json" | "jsonc" | "kdl" | "service" |
| 191 |
| "timer" | "socket" | "target" | "mount" | "desktop" | "env" => Mode::Config, |
| 192 |
"css" | "scss" | "sass" | "less" => Mode::Style, |
| 193 |
"html" | "htm" | "xml" | "svg" | "xhtml" => Mode::Markup, |
| 194 |
_ if source.starts_with("#!") => Mode::Code { |
| 195 |
hash_comments: true, |
| 196 |
dash_comments: false, |
| 197 |
rust_like: false, |
| 198 |
}, |
| 199 |
_ => Mode::Plain, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
fn highlight_line(line: &str, mode: Mode, state: &mut State) -> String { |
| 204 |
match mode { |
| 205 |
Mode::Plain => escape_html(line), |
| 206 |
Mode::Config => highlight_config_line(line), |
| 207 |
Mode::Style => highlight_style_line(line, state), |
| 208 |
Mode::Markup => highlight_markup_line(line, state), |
| 209 |
Mode::Code { |
| 210 |
hash_comments, |
| 211 |
dash_comments, |
| 212 |
rust_like, |
| 213 |
} => highlight_code_line(line, hash_comments, dash_comments, rust_like, state), |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
fn highlight_code_line( |
| 218 |
line: &str, |
| 219 |
hash_comments: bool, |
| 220 |
dash_comments: bool, |
| 221 |
rust_like: bool, |
| 222 |
state: &mut State, |
| 223 |
) -> String { |
| 224 |
let bytes = line.as_bytes(); |
| 225 |
let len = bytes.len(); |
| 226 |
let mut i = 0; |
| 227 |
let mut out = String::with_capacity(len * 2); |
| 228 |
let mut prev_keyword = ""; |
| 229 |
let mut prev_was_dot = false; |
| 230 |
let mut in_fn_params = false; |
| 231 |
let mut paren_depth: usize = 0; |
| 232 |
|
| 233 |
while i < len { |
| 234 |
match *state { |
| 235 |
State::BlockComment => { |
| 236 |
if let Some(pos) = line[i..].find("*/") { |
| 237 |
let end = i + pos + 2; |
| 238 |
push_span(&mut out, "hl-comment", &line[i..end]); |
| 239 |
i = end; |
| 240 |
*state = State::Normal; |
| 241 |
} else { |
| 242 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 243 |
return out; |
| 244 |
} |
| 245 |
continue; |
| 246 |
} |
| 247 |
State::LuaBlockComment => { |
| 248 |
if let Some(pos) = line[i..].find("]]") { |
| 249 |
let end = i + pos + 2; |
| 250 |
push_span(&mut out, "hl-comment", &line[i..end]); |
| 251 |
i = end; |
| 252 |
*state = State::Normal; |
| 253 |
} else { |
| 254 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 255 |
return out; |
| 256 |
} |
| 257 |
continue; |
| 258 |
} |
| 259 |
State::DoubleString => { |
| 260 |
i = scan_double_string(line, i, &mut out, state); |
| 261 |
continue; |
| 262 |
} |
| 263 |
State::RawString(hashes) => { |
| 264 |
i = scan_raw_string(line, i, hashes, &mut out, state); |
| 265 |
continue; |
| 266 |
} |
| 267 |
State::Normal => {} |
| 268 |
} |
| 269 |
|
| 270 |
let b = bytes[i]; |
| 271 |
|
| 272 |
if b.is_ascii_whitespace() { |
| 273 |
let start = i; |
| 274 |
while i < len && bytes[i].is_ascii_whitespace() { |
| 275 |
i += 1; |
| 276 |
} |
| 277 |
out.push_str(&line[start..i]); |
| 278 |
continue; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
if b == b'/' && i + 1 < len { |
| 283 |
if bytes[i + 1] == b'/' { |
| 284 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 285 |
break; |
| 286 |
} |
| 287 |
if bytes[i + 1] == b'*' { |
| 288 |
*state = State::BlockComment; |
| 289 |
continue; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
if dash_comments && b == b'-' && i + 1 < len && bytes[i + 1] == b'-' { |
| 295 |
if line[i + 2..].starts_with("[[") { |
| 296 |
*state = State::LuaBlockComment; |
| 297 |
continue; |
| 298 |
} |
| 299 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
if b == b'#' { |
| 305 |
if rust_like |
| 306 |
&& i + 1 < len |
| 307 |
&& (bytes[i + 1] == b'[' || bytes[i + 1] == b'!') |
| 308 |
&& let Some(close) = line[i..].find(']') |
| 309 |
{ |
| 310 |
let end = i + close + 1; |
| 311 |
push_span(&mut out, "hl-attribute", &line[i..end]); |
| 312 |
i = end; |
| 313 |
continue; |
| 314 |
} |
| 315 |
if hash_comments || (i == 0 && line.starts_with("#!")) { |
| 316 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 317 |
break; |
| 318 |
} |
| 319 |
if !rust_like { |
| 320 |
let start = i; |
| 321 |
i += 1; |
| 322 |
while i < len && bytes[i].is_ascii_whitespace() { |
| 323 |
i += 1; |
| 324 |
} |
| 325 |
while i < len && bytes[i].is_ascii_alphabetic() { |
| 326 |
i += 1; |
| 327 |
} |
| 328 |
push_span(&mut out, "hl-macro", &line[start..i]); |
| 329 |
continue; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
if rust_like && b == b'r' && i + 1 < len && (bytes[i + 1] == b'"' || bytes[i + 1] == b'#') { |
| 335 |
let mut hashes = 0; |
| 336 |
let mut j = i + 1; |
| 337 |
while j < len && bytes[j] == b'#' { |
| 338 |
hashes += 1; |
| 339 |
j += 1; |
| 340 |
} |
| 341 |
if j < len && bytes[j] == b'"' { |
| 342 |
push_span(&mut out, "hl-string", &line[i..=j]); |
| 343 |
*state = State::RawString(hashes); |
| 344 |
i = j + 1; |
| 345 |
continue; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if b == b'"' { |
| 350 |
push_span(&mut out, "hl-string", "\""); |
| 351 |
*state = State::DoubleString; |
| 352 |
i = scan_double_string(line, i + 1, &mut out, state); |
| 353 |
prev_was_dot = false; |
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
if b == b'\'' { |
| 358 |
if rust_like { |
| 359 |
if let Some((end, is_lifetime)) = scan_rust_quote(line, i) { |
| 360 |
let class = if is_lifetime { "hl-label" } else { "hl-escape" }; |
| 361 |
push_span(&mut out, class, &line[i..end]); |
| 362 |
i = end; |
| 363 |
prev_was_dot = false; |
| 364 |
continue; |
| 365 |
} |
| 366 |
} else { |
| 367 |
let end = scan_single_string(line, i); |
| 368 |
push_span(&mut out, "hl-string", &line[i..end]); |
| 369 |
i = end; |
| 370 |
prev_was_dot = false; |
| 371 |
continue; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if b.is_ascii_digit() { |
| 376 |
let start = i; |
| 377 |
i += 1; |
| 378 |
while i < len |
| 379 |
&& (bytes[i].is_ascii_alphanumeric() |
| 380 |
|| bytes[i] == b'_' |
| 381 |
|| (bytes[i] == b'.' && i + 1 < len && bytes[i + 1].is_ascii_digit())) |
| 382 |
{ |
| 383 |
i += 1; |
| 384 |
} |
| 385 |
push_span(&mut out, "hl-number", &line[start..i]); |
| 386 |
prev_was_dot = false; |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
if is_ident_start(b) { |
| 391 |
let start = i; |
| 392 |
i += 1; |
| 393 |
while i < len && is_ident_continue(bytes[i]) { |
| 394 |
i += 1; |
| 395 |
} |
| 396 |
let ident = &line[start..i]; |
| 397 |
let rest = &line[i..]; |
| 398 |
|
| 399 |
if rest.starts_with('!') && !rest.starts_with("!=") { |
| 400 |
i += 1; |
| 401 |
push_span(&mut out, "hl-macro", &line[start..i]); |
| 402 |
prev_was_dot = false; |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
let class = classify_ident( |
| 407 |
ident, |
| 408 |
rest.trim_start(), |
| 409 |
prev_keyword, |
| 410 |
prev_was_dot, |
| 411 |
in_fn_params && paren_depth == 1, |
| 412 |
); |
| 413 |
push_span(&mut out, class, ident); |
| 414 |
|
| 415 |
if matches!(ident, "fn" | "function" | "def") { |
| 416 |
in_fn_params = true; |
| 417 |
} |
| 418 |
prev_keyword = if matches!(class, "hl-keyword" | "hl-include") { |
| 419 |
ident |
| 420 |
} else { |
| 421 |
"" |
| 422 |
}; |
| 423 |
prev_was_dot = false; |
| 424 |
continue; |
| 425 |
} |
| 426 |
|
| 427 |
if matches!(b, b'(' | b')' | b'[' | b']' | b'{' | b'}' | b',' | b';') { |
| 428 |
match b { |
| 429 |
b'(' => paren_depth += 1, |
| 430 |
b')' => { |
| 431 |
paren_depth = paren_depth.saturating_sub(1); |
| 432 |
if paren_depth == 0 { |
| 433 |
in_fn_params = false; |
| 434 |
} |
| 435 |
} |
| 436 |
b'{' | b';' => in_fn_params = false, |
| 437 |
_ => {} |
| 438 |
} |
| 439 |
push_span(&mut out, "hl-punctuation", &line[i..=i]); |
| 440 |
prev_was_dot = false; |
| 441 |
i += 1; |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
if b == b':' { |
| 446 |
let len_colon = if i + 1 < len && bytes[i + 1] == b':' { |
| 447 |
2 |
| 448 |
} else { |
| 449 |
1 |
| 450 |
}; |
| 451 |
push_span(&mut out, "hl-punctuation", &line[i..i + len_colon]); |
| 452 |
prev_was_dot = false; |
| 453 |
i += len_colon; |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
if b == b'.' { |
| 458 |
if i + 1 < len && bytes[i + 1] == b'.' { |
| 459 |
let end = if i + 2 < len && bytes[i + 2] == b'=' { |
| 460 |
i + 3 |
| 461 |
} else { |
| 462 |
i + 2 |
| 463 |
}; |
| 464 |
push_span(&mut out, "hl-operator", &line[i..end]); |
| 465 |
prev_was_dot = false; |
| 466 |
i = end; |
| 467 |
} else { |
| 468 |
push_span(&mut out, "hl-punctuation", "."); |
| 469 |
prev_was_dot = true; |
| 470 |
i += 1; |
| 471 |
} |
| 472 |
continue; |
| 473 |
} |
| 474 |
|
| 475 |
if is_operator_byte(b) { |
| 476 |
let start = i; |
| 477 |
i += 1; |
| 478 |
while i < len && is_operator_byte(bytes[i]) { |
| 479 |
i += 1; |
| 480 |
} |
| 481 |
push_span(&mut out, "hl-operator", &line[start..i]); |
| 482 |
prev_was_dot = false; |
| 483 |
continue; |
| 484 |
} |
| 485 |
|
| 486 |
let ch = line[i..].chars().next().unwrap(); |
| 487 |
let end = i + ch.len_utf8(); |
| 488 |
escape_into(&mut out, &line[i..end]); |
| 489 |
prev_was_dot = false; |
| 490 |
i = end; |
| 491 |
} |
| 492 |
|
| 493 |
out |
| 494 |
} |
| 495 |
|
| 496 |
fn classify_ident( |
| 497 |
ident: &str, |
| 498 |
trimmed_rest: &str, |
| 499 |
prev_keyword: &str, |
| 500 |
prev_was_dot: bool, |
| 501 |
in_fn_params: bool, |
| 502 |
) -> &'static str { |
| 503 |
if !prev_was_dot { |
| 504 |
if IMPORTS.contains(&ident) { |
| 505 |
return "hl-include"; |
| 506 |
} |
| 507 |
if KEYWORDS.contains(&ident) { |
| 508 |
return "hl-keyword"; |
| 509 |
} |
| 510 |
if BOOLEANS.contains(&ident) { |
| 511 |
return "hl-boolean"; |
| 512 |
} |
| 513 |
if BUILTIN_TYPES.contains(&ident) { |
| 514 |
return "hl-type-builtin"; |
| 515 |
} |
| 516 |
if BUILTIN_CONSTS.contains(&ident) { |
| 517 |
return "hl-constant"; |
| 518 |
} |
| 519 |
if BUILTIN_FUNCS.contains(&ident) { |
| 520 |
return "hl-function-builtin"; |
| 521 |
} |
| 522 |
if BUILTIN_VARS.contains(&ident) { |
| 523 |
return "hl-variable"; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
if matches!(prev_keyword, "fn" | "function" | "def") { |
| 528 |
return "hl-function"; |
| 529 |
} |
| 530 |
if matches!( |
| 531 |
prev_keyword, |
| 532 |
"struct" | "enum" | "trait" | "type" | "class" | "interface" |
| 533 |
) { |
| 534 |
return "hl-type"; |
| 535 |
} |
| 536 |
if prev_keyword == "mod" { |
| 537 |
return "hl-module"; |
| 538 |
} |
| 539 |
|
| 540 |
if ident.as_bytes()[0].is_ascii_uppercase() { |
| 541 |
let is_const = ident.len() > 1 |
| 542 |
&& ident |
| 543 |
.bytes() |
| 544 |
.all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_'); |
| 545 |
return if is_const { "hl-constant" } else { "hl-type" }; |
| 546 |
} |
| 547 |
|
| 548 |
if trimmed_rest.starts_with("::") && !trimmed_rest.starts_with("::<") { |
| 549 |
return "hl-module"; |
| 550 |
} |
| 551 |
if trimmed_rest.starts_with('(') || trimmed_rest.starts_with("::<") { |
| 552 |
return "hl-function"; |
| 553 |
} |
| 554 |
if prev_was_dot { |
| 555 |
return "hl-property"; |
| 556 |
} |
| 557 |
if trimmed_rest.starts_with(':') && !trimmed_rest.starts_with("::") { |
| 558 |
return if in_fn_params { |
| 559 |
"hl-parameter" |
| 560 |
} else { |
| 561 |
"hl-property" |
| 562 |
}; |
| 563 |
} |
| 564 |
|
| 565 |
"hl-variable" |
| 566 |
} |
| 567 |
|
| 568 |
fn highlight_config_line(line: &str) -> String { |
| 569 |
let trimmed = line.trim_start(); |
| 570 |
if trimmed.starts_with('#') || trimmed.starts_with("//") || trimmed.starts_with(';') { |
| 571 |
let mut out = String::new(); |
| 572 |
push_span(&mut out, "hl-comment", line); |
| 573 |
return out; |
| 574 |
} |
| 575 |
|
| 576 |
if trimmed.starts_with('[') && trimmed.ends_with(']') { |
| 577 |
let indent = &line[..line.len() - trimmed.len()]; |
| 578 |
let mut out = String::from(indent); |
| 579 |
let open_len = if trimmed.starts_with("[[") { 2 } else { 1 }; |
| 580 |
let close_len = if trimmed.ends_with("]]") { 2 } else { 1 }; |
| 581 |
push_span(&mut out, "hl-punctuation", &trimmed[..open_len]); |
| 582 |
push_span( |
| 583 |
&mut out, |
| 584 |
"hl-property", |
| 585 |
&trimmed[open_len..trimmed.len() - close_len], |
| 586 |
); |
| 587 |
push_span( |
| 588 |
&mut out, |
| 589 |
"hl-punctuation", |
| 590 |
&trimmed[trimmed.len() - close_len..], |
| 591 |
); |
| 592 |
return out; |
| 593 |
} |
| 594 |
|
| 595 |
if let Some(sep_idx) = find_config_sep(line) { |
| 596 |
let key_part = &line[..sep_idx]; |
| 597 |
let sep = &line[sep_idx..=sep_idx]; |
| 598 |
let val_part = &line[sep_idx + 1..]; |
| 599 |
|
| 600 |
let mut out = String::new(); |
| 601 |
let key_trim = key_part.trim(); |
| 602 |
if !key_trim.is_empty() { |
| 603 |
let lead = key_part.len() - key_part.trim_start().len(); |
| 604 |
let trail = key_part.len() - key_part.trim_end().len(); |
| 605 |
out.push_str(&key_part[..lead]); |
| 606 |
push_span(&mut out, "hl-property", key_trim); |
| 607 |
out.push_str(&key_part[key_part.len() - trail..]); |
| 608 |
} else { |
| 609 |
escape_into(&mut out, key_part); |
| 610 |
} |
| 611 |
|
| 612 |
let sep_class = if sep == "=" { |
| 613 |
"hl-operator" |
| 614 |
} else { |
| 615 |
"hl-punctuation" |
| 616 |
}; |
| 617 |
push_span(&mut out, sep_class, sep); |
| 618 |
|
| 619 |
let mut state = State::Normal; |
| 620 |
out.push_str(&highlight_code_line( |
| 621 |
val_part, true, false, false, &mut state, |
| 622 |
)); |
| 623 |
return out; |
| 624 |
} |
| 625 |
|
| 626 |
let mut state = State::Normal; |
| 627 |
highlight_code_line(line, true, false, false, &mut state) |
| 628 |
} |
| 629 |
|
| 630 |
fn find_config_sep(line: &str) -> Option<usize> { |
| 631 |
let mut in_quotes = false; |
| 632 |
for (i, &b) in line.as_bytes().iter().enumerate() { |
| 633 |
if b == b'"' { |
| 634 |
in_quotes = !in_quotes; |
| 635 |
} else if !in_quotes && (b == b'=' || b == b':') { |
| 636 |
return Some(i); |
| 637 |
} |
| 638 |
} |
| 639 |
None |
| 640 |
} |
| 641 |
|
| 642 |
fn highlight_style_line(line: &str, state: &mut State) -> String { |
| 643 |
let trimmed = line.trim_start(); |
| 644 |
if *state == State::BlockComment || trimmed.starts_with("/*") { |
| 645 |
return highlight_code_line(line, false, false, false, state); |
| 646 |
} |
| 647 |
|
| 648 |
if let Some(colon) = line.find(':') |
| 649 |
&& !trimmed.starts_with(['.', '#', '@']) |
| 650 |
&& !line[..colon].contains('{') |
| 651 |
{ |
| 652 |
let prop = &line[..colon]; |
| 653 |
let rest = &line[colon + 1..]; |
| 654 |
let mut out = String::new(); |
| 655 |
let lead = prop.len() - prop.trim_start().len(); |
| 656 |
out.push_str(&prop[..lead]); |
| 657 |
push_span(&mut out, "hl-property", prop.trim_start()); |
| 658 |
push_span(&mut out, "hl-punctuation", ":"); |
| 659 |
out.push_str(&highlight_code_line(rest, false, false, false, state)); |
| 660 |
return out; |
| 661 |
} |
| 662 |
|
| 663 |
highlight_code_line(line, false, false, false, state) |
| 664 |
} |
| 665 |
|
| 666 |
fn highlight_markup_line(line: &str, state: &mut State) -> String { |
| 667 |
let bytes = line.as_bytes(); |
| 668 |
let len = bytes.len(); |
| 669 |
let mut i = 0; |
| 670 |
let mut out = String::with_capacity(len * 2); |
| 671 |
|
| 672 |
while i < len { |
| 673 |
if *state == State::BlockComment { |
| 674 |
if let Some(pos) = line[i..].find("-->") { |
| 675 |
let end = i + pos + 3; |
| 676 |
push_span(&mut out, "hl-comment", &line[i..end]); |
| 677 |
i = end; |
| 678 |
*state = State::Normal; |
| 679 |
} else { |
| 680 |
push_span(&mut out, "hl-comment", &line[i..]); |
| 681 |
return out; |
| 682 |
} |
| 683 |
continue; |
| 684 |
} |
| 685 |
|
| 686 |
if line[i..].starts_with("<!--") { |
| 687 |
*state = State::BlockComment; |
| 688 |
continue; |
| 689 |
} |
| 690 |
|
| 691 |
if bytes[i] == b'<' { |
| 692 |
let start = i; |
| 693 |
i += 1; |
| 694 |
if i < len && matches!(bytes[i], b'/' | b'!' | b'?') { |
| 695 |
i += 1; |
| 696 |
} |
| 697 |
push_span(&mut out, "hl-tag-delimiter", &line[start..i]); |
| 698 |
|
| 699 |
let tag_start = i; |
| 700 |
while i < len && (bytes[i].is_ascii_alphanumeric() || matches!(bytes[i], b'-' | b':')) { |
| 701 |
i += 1; |
| 702 |
} |
| 703 |
if i > tag_start { |
| 704 |
push_span(&mut out, "hl-tag", &line[tag_start..i]); |
| 705 |
} |
| 706 |
|
| 707 |
while i < len && bytes[i] != b'>' { |
| 708 |
if bytes[i].is_ascii_whitespace() { |
| 709 |
out.push(bytes[i] as char); |
| 710 |
i += 1; |
| 711 |
} else if matches!(bytes[i], b'"' | b'\'') { |
| 712 |
let quote = bytes[i]; |
| 713 |
let s_start = i; |
| 714 |
i += 1; |
| 715 |
while i < len && bytes[i] != quote { |
| 716 |
i += 1; |
| 717 |
} |
| 718 |
if i < len { |
| 719 |
i += 1; |
| 720 |
} |
| 721 |
push_span(&mut out, "hl-string", &line[s_start..i]); |
| 722 |
} else if bytes[i] == b'=' { |
| 723 |
push_span(&mut out, "hl-operator", "="); |
| 724 |
i += 1; |
| 725 |
} else if bytes[i] == b'/' && i + 1 < len && bytes[i + 1] == b'>' { |
| 726 |
push_span(&mut out, "hl-tag-delimiter", "/>"); |
| 727 |
i += 2; |
| 728 |
break; |
| 729 |
} else { |
| 730 |
let attr_start = i; |
| 731 |
while i < len |
| 732 |
&& !bytes[i].is_ascii_whitespace() |
| 733 |
&& !matches!(bytes[i], b'=' | b'>' | b'/' | b'"' | b'\'') |
| 734 |
{ |
| 735 |
i += 1; |
| 736 |
} |
| 737 |
if i > attr_start { |
| 738 |
push_span(&mut out, "hl-label", &line[attr_start..i]); |
| 739 |
} else { |
| 740 |
escape_into(&mut out, &line[i..=i]); |
| 741 |
i += 1; |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
if i < len && bytes[i] == b'>' { |
| 747 |
push_span(&mut out, "hl-tag-delimiter", ">"); |
| 748 |
i += 1; |
| 749 |
} |
| 750 |
continue; |
| 751 |
} |
| 752 |
|
| 753 |
let next_lt = line[i..].find('<').map(|p| i + p).unwrap_or(len); |
| 754 |
escape_into(&mut out, &line[i..next_lt]); |
| 755 |
i = next_lt; |
| 756 |
} |
| 757 |
|
| 758 |
out |
| 759 |
} |
| 760 |
|
| 761 |
fn scan_double_string(line: &str, mut i: usize, out: &mut String, state: &mut State) -> usize { |
| 762 |
let bytes = line.as_bytes(); |
| 763 |
let len = bytes.len(); |
| 764 |
let mut seg_start = i; |
| 765 |
|
| 766 |
while i < len { |
| 767 |
if bytes[i] == b'\\' && i + 1 < len { |
| 768 |
if i > seg_start { |
| 769 |
push_span(out, "hl-string", &line[seg_start..i]); |
| 770 |
} |
| 771 |
let esc_end = (i + 2).min(len); |
| 772 |
push_span(out, "hl-escape", &line[i..esc_end]); |
| 773 |
i = esc_end; |
| 774 |
seg_start = i; |
| 775 |
} else if bytes[i] == b'"' { |
| 776 |
push_span(out, "hl-string", &line[seg_start..=i]); |
| 777 |
*state = State::Normal; |
| 778 |
return i + 1; |
| 779 |
} else { |
| 780 |
i += line[i..].chars().next().unwrap().len_utf8(); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
if seg_start < len { |
| 785 |
push_span(out, "hl-string", &line[seg_start..len]); |
| 786 |
} |
| 787 |
len |
| 788 |
} |
| 789 |
|
| 790 |
fn scan_raw_string( |
| 791 |
line: &str, |
| 792 |
mut i: usize, |
| 793 |
hashes: usize, |
| 794 |
out: &mut String, |
| 795 |
state: &mut State, |
| 796 |
) -> usize { |
| 797 |
let bytes = line.as_bytes(); |
| 798 |
let len = bytes.len(); |
| 799 |
let start = i; |
| 800 |
|
| 801 |
while i < len { |
| 802 |
if bytes[i] == b'"' { |
| 803 |
let mut h = 0; |
| 804 |
while i + 1 + h < len && bytes[i + 1 + h] == b'#' && h < hashes { |
| 805 |
h += 1; |
| 806 |
} |
| 807 |
if h == hashes { |
| 808 |
let end = i + 1 + hashes; |
| 809 |
push_span(out, "hl-string", &line[start..end]); |
| 810 |
*state = State::Normal; |
| 811 |
return end; |
| 812 |
} |
| 813 |
} |
| 814 |
i += 1; |
| 815 |
} |
| 816 |
|
| 817 |
if start < len { |
| 818 |
push_span(out, "hl-string", &line[start..len]); |
| 819 |
} |
| 820 |
len |
| 821 |
} |
| 822 |
|
| 823 |
fn scan_single_string(line: &str, start: usize) -> usize { |
| 824 |
let bytes = line.as_bytes(); |
| 825 |
let len = bytes.len(); |
| 826 |
let mut i = start + 1; |
| 827 |
while i < len { |
| 828 |
if bytes[i] == b'\\' && i + 1 < len { |
| 829 |
i += 2; |
| 830 |
} else if bytes[i] == b'\'' { |
| 831 |
return i + 1; |
| 832 |
} else { |
| 833 |
i += 1; |
| 834 |
} |
| 835 |
} |
| 836 |
len |
| 837 |
} |
| 838 |
|
| 839 |
fn scan_rust_quote(line: &str, start: usize) -> Option<(usize, bool)> { |
| 840 |
let rest = &line[start + 1..]; |
| 841 |
if let Some(escaped) = rest.strip_prefix('\\') |
| 842 |
&& let Some(close) = escaped.find('\'') |
| 843 |
{ |
| 844 |
return Some((start + 2 + close + 1, false)); |
| 845 |
} |
| 846 |
let mut chars = rest.char_indices(); |
| 847 |
if let Some((_, first)) = chars.next() { |
| 848 |
if let Some((next_idx, '\'')) = chars.next() { |
| 849 |
return Some((start + 1 + next_idx + 1, false)); |
| 850 |
} |
| 851 |
if first.is_ascii_alphabetic() || first == '_' { |
| 852 |
let mut end = first.len_utf8(); |
| 853 |
for (idx, ch) in rest[end..].char_indices() { |
| 854 |
if ch.is_ascii_alphanumeric() || ch == '_' { |
| 855 |
end = first.len_utf8() + idx + ch.len_utf8(); |
| 856 |
} else { |
| 857 |
break; |
| 858 |
} |
| 859 |
} |
| 860 |
return Some((start + 1 + end, true)); |
| 861 |
} |
| 862 |
} |
| 863 |
None |
| 864 |
} |
| 865 |
|
| 866 |
fn is_ident_start(b: u8) -> bool { |
| 867 |
b.is_ascii_alphabetic() || b == b'_' |
| 868 |
} |
| 869 |
|
| 870 |
fn is_ident_continue(b: u8) -> bool { |
| 871 |
b.is_ascii_alphanumeric() || b == b'_' |
| 872 |
} |
| 873 |
|
| 874 |
fn is_operator_byte(b: u8) -> bool { |
| 875 |
matches!( |
| 876 |
b, |
| 877 |
b'=' | b'+' |
| 878 |
| b'-' |
| 879 |
| b'*' |
| 880 |
| b'/' |
| 881 |
| b'%' |
| 882 |
| b'&' |
| 883 |
| b'|' |
| 884 |
| b'^' |
| 885 |
| b'!' |
| 886 |
| b'<' |
| 887 |
| b'>' |
| 888 |
| b'?' |
| 889 |
| b'~' |
| 890 |
| b'@' |
| 891 |
) |
| 892 |
} |
| 893 |
|
| 894 |
fn push_span(out: &mut String, class: &str, text: &str) { |
| 895 |
if text.is_empty() { |
| 896 |
return; |
| 897 |
} |
| 898 |
out.push_str("<span class=\""); |
| 899 |
out.push_str(class); |
| 900 |
out.push_str("\">"); |
| 901 |
escape_into(out, text); |
| 902 |
out.push_str("</span>"); |
| 903 |
} |
| 904 |
|
| 905 |
fn escape_html(text: &str) -> String { |
| 906 |
let mut out = String::with_capacity(text.len()); |
| 907 |
escape_into(&mut out, text); |
| 908 |
out |
| 909 |
} |
| 910 |
|
| 911 |
fn escape_into(out: &mut String, text: &str) { |
| 912 |
v_htmlescape::b_escape(text.as_bytes(), out); |
| 913 |
} |