979134bfeat: code reads as code, in classes rather than colours19h | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | use std::sync::OnceLock; |
| 22 | |
| 23 | use syntect::{ |
| 24 | html::{ClassStyle, line_tokens_to_classed_spans}, |
| 25 | parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet}, |
| 26 | util::LinesWithEndings, |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | const PREFIX: &str = "hl-"; |
| 31 | |
| 32 | const CLASS_STYLE: ClassStyle = ClassStyle::SpacedPrefixed { prefix: PREFIX }; |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | pub const MAX_BYTES: usize = 512 * 1024; |
| 41 | |
| 42 | |
| 43 | pub const MAX_LINES: usize = 10_000; |
| 44 | |
| 45 | |
| 46 | |
| 47 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 48 | pub struct Source { |
| 49 | pub lines: Vec<SourceLine>, |
| 50 | |
| 51 | |
| 52 | |
| 53 | pub too_large: bool, |
| 54 | } |
| 55 | |
| 56 | |
| 57 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 58 | pub enum SourceLine { |
| 59 | |
| 60 | Plain(String), |
| 61 | |
| 62 | Classed(String), |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | pub fn source_lines(file_name: &str, text: &str) -> Source { |
| 71 | let too_large = text.len() > MAX_BYTES || text.lines().count() > MAX_LINES; |
| 72 | |
| 73 | let classed = (!too_large) |
| 74 | .then(|| { |
| 75 | let syntaxes = syntaxes(); |
| 76 | let syntax = syntax_for(syntaxes, file_name, text)?; |
| 77 | classed_lines(syntaxes, syntax, text) |
| 78 | }) |
| 79 | .flatten() |
| 80 | |
| 81 | |
| 82 | .filter(|lines| lines.len() == text.lines().count()); |
| 83 | |
| 84 | let lines = match classed { |
| 85 | Some(classed) => text |
| 86 | .lines() |
| 87 | .zip(classed) |
| 88 | .map(|(line, html)| match line.is_empty() { |
| 89 | |
| 90 | |
| 91 | true => SourceLine::Plain(" ".to_owned()), |
| 92 | false => SourceLine::Classed(html), |
| 93 | }) |
| 94 | .collect(), |
| 95 | None => text |
| 96 | .lines() |
| 97 | .map(|line| match line.is_empty() { |
| 98 | true => SourceLine::Plain(" ".to_owned()), |
| 99 | false => SourceLine::Plain(line.to_owned()), |
| 100 | }) |
| 101 | .collect(), |
| 102 | }; |
| 103 | |
| 104 | Source { lines, too_large } |
| 105 | } |
| 106 | |
8331d0cfeat: the languages this repository actually uses18h | 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
979134bfeat: code reads as code, in classes rather than colours19h | 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | fn syntaxes() -> &'static SyntaxSet { |
| 120 | static SYNTAXES: OnceLock<SyntaxSet> = OnceLock::new(); |
8331d0cfeat: the languages this repository actually uses18h | 121 | SYNTAXES.get_or_init(two_face::syntax::extra_newlines) |
979134bfeat: code reads as code, in classes rather than colours19h | 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | fn syntax_for<'a>( |
| 132 | syntaxes: &'a SyntaxSet, |
| 133 | file_name: &str, |
| 134 | text: &str, |
| 135 | ) -> Option<&'a SyntaxReference> { |
| 136 | let extension = file_name.rsplit_once('.').map(|(_, ext)| ext); |
| 137 | |
| 138 | let syntax = syntaxes |
| 139 | .find_syntax_by_extension(file_name) |
| 140 | .or_else(|| extension.and_then(|ext| syntaxes.find_syntax_by_extension(ext))) |
| 141 | .or_else(|| syntaxes.find_syntax_by_first_line(text.lines().next().unwrap_or("")))?; |
| 142 | |
| 143 | (syntax.name != syntaxes.find_syntax_plain_text().name).then_some(syntax) |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | fn classed_lines( |
| 153 | syntaxes: &SyntaxSet, |
| 154 | syntax: &SyntaxReference, |
| 155 | text: &str, |
| 156 | ) -> Option<Vec<String>> { |
| 157 | let mut state = ParseState::new(syntax); |
| 158 | let mut stack = ScopeStack::new(); |
| 159 | let mut lines = Vec::new(); |
| 160 | |
| 161 | for line in LinesWithEndings::from(text) { |
| 162 | let ops = state.parse_line(line, syntaxes).ok()?; |
| 163 | |
| 164 | let mut html = String::with_capacity(line.len()); |
| 165 | for scope in &stack.scopes { |
| 166 | open(&mut html, &scope.build_string()); |
| 167 | } |
| 168 | |
| 169 | let (body, _) = line_tokens_to_classed_spans(line, &ops, CLASS_STYLE, &mut stack).ok()?; |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | html.extend(body.chars().filter(|c| *c != '\n' && *c != '\r')); |
| 176 | |
| 177 | |
| 178 | |
| 179 | for _ in 0..stack.scopes.len() { |
| 180 | html.push_str("</span>"); |
| 181 | } |
| 182 | |
| 183 | lines.push(html); |
| 184 | } |
| 185 | |
| 186 | Some(lines) |
| 187 | } |
| 188 | |
| 189 | |
| 190 | fn open(html: &mut String, scope: &str) { |
| 191 | html.push_str("<span class=\""); |
| 192 | for (index, atom) in scope.split('.').filter(|atom| !atom.is_empty()).enumerate() { |
| 193 | if index != 0 { |
| 194 | html.push(' '); |
| 195 | } |
| 196 | html.push_str(PREFIX); |
| 197 | html.push_str(atom); |
| 198 | } |
| 199 | html.push_str("\">"); |
| 200 | } |
| 201 | |
| 202 | #[cfg(test)] |
| 203 | mod tests { |
| 204 | use super::*; |
| 205 | |
| 206 | fn classes(line: &SourceLine) -> String { |
| 207 | match line { |
| 208 | SourceLine::Classed(html) => html.clone(), |
| 209 | SourceLine::Plain(text) => text.clone(), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn rust_source_is_wrapped_in_prefixed_classes() { |
| 215 | let source = source_lines("main.rs", "fn main() {\n // hi\n}\n"); |
| 216 | |
| 217 | assert!(!source.too_large); |
| 218 | assert_eq!(source.lines.len(), 3); |
| 219 | assert!(matches!(source.lines[0], SourceLine::Classed(_))); |
| 220 | |
| 221 | let first = classes(&source.lines[0]); |
| 222 | assert!(first.contains("class=\"hl-"), "{first}"); |
| 223 | |
| 224 | assert!(first.contains("hl-storage"), "{first}"); |
| 225 | assert!(first.contains("hl-entity hl-name hl-function"), "{first}"); |
| 226 | assert!(classes(&source.lines[1]).contains("hl-comment")); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn a_line_carries_no_line_ending_and_closes_every_span_it_opens() { |
| 231 | let source = source_lines("main.rs", "fn main() {\n let x = \"a\";\n}\n"); |
| 232 | |
| 233 | for line in &source.lines { |
| 234 | let html = classes(line); |
| 235 | assert!(!html.contains('\n'), "{html}"); |
| 236 | assert_eq!( |
| 237 | html.matches("<span").count(), |
| 238 | html.matches("</span>").count(), |
| 239 | "{html}" |
| 240 | ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn the_source_is_escaped_rather_than_passed_through() { |
| 246 | let source = source_lines("main.rs", "// <script>alert(1)</script>\n"); |
| 247 | |
| 248 | let html = classes(&source.lines[0]); |
| 249 | assert!(!html.contains("<script"), "{html}"); |
| 250 | assert!(html.contains("<script"), "{html}"); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn an_unknown_extension_stays_plain() { |
| 255 | let source = source_lines("notes.wibble", "hello\nworld\n"); |
| 256 | |
| 257 | assert_eq!( |
| 258 | source.lines, |
| 259 | vec![ |
| 260 | SourceLine::Plain("hello".to_owned()), |
| 261 | SourceLine::Plain("world".to_owned()), |
| 262 | ] |
| 263 | ); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn a_shebang_names_the_language_when_the_name_does_not() { |
| 268 | let source = source_lines("run", "#!/bin/sh\necho hi\n"); |
| 269 | |
| 270 | assert!(matches!(source.lines[0], SourceLine::Classed(_))); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn a_file_past_the_byte_cap_is_plain_and_says_so() { |
| 275 | let text = format!("fn main() {{}}\n{}\n", "x".repeat(MAX_BYTES)); |
| 276 | let source = source_lines("main.rs", &text); |
| 277 | |
| 278 | assert!(source.too_large); |
| 279 | assert!( |
| 280 | source |
| 281 | .lines |
| 282 | .iter() |
| 283 | .all(|line| matches!(line, SourceLine::Plain(_))) |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn a_file_past_the_line_cap_is_plain_and_says_so() { |
| 289 | let text = "let x = 1;\n".repeat(MAX_LINES + 1); |
| 290 | let source = source_lines("main.rs", &text); |
| 291 | |
| 292 | assert!(source.too_large); |
| 293 | assert_eq!(source.lines.len(), MAX_LINES + 1); |
| 294 | assert!(matches!(source.lines[0], SourceLine::Plain(_))); |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn a_blank_line_keeps_its_height() { |
| 299 | let source = source_lines("main.rs", "fn a() {}\n\nfn b() {}\n"); |
| 300 | |
| 301 | assert_eq!(source.lines[1], SourceLine::Plain(" ".to_owned())); |
| 302 | } |
| 303 | |
| 304 | #[test] |
| 305 | fn plain_text_is_not_dressed_in_spans_that_colour_nothing() { |
| 306 | let source = source_lines("notes.txt", "hello\n"); |
| 307 | |
| 308 | assert_eq!(source.lines, vec![SourceLine::Plain("hello".to_owned())]); |
| 309 | } |
| 310 | |
8331d0cfeat: the languages this repository actually uses18h | 311 | |
| 312 | |
| 313 | |
979134bfeat: code reads as code, in classes rather than colours19h | 314 | #[test] |
8331d0cfeat: the languages this repository actually uses18h | 315 | fn the_languages_this_repository_uses_are_all_highlighted() { |
979134bfeat: code reads as code, in classes rather than colours19h | 316 | for name in [ |
| 317 | "main.rs", |
8331d0cfeat: the languages this repository actually uses18h | 318 | "install.sh", |
979134bfeat: code reads as code, in classes rather than colours19h | 319 | "Cargo.toml", |
| 320 | "Dockerfile", |
8331d0cfeat: the languages this repository actually uses18h | 321 | "app.ts", |
| 322 | "App.tsx", |
979134bfeat: code reads as code, in classes rather than colours19h | 323 | "README.md", |
8331d0cfeat: the languages this repository actually uses18h | 324 | "styles.css", |
| 325 | "config.yaml", |
| 326 | "data.json", |
| 327 | "Makefile", |
979134bfeat: code reads as code, in classes rather than colours19h | 328 | "app.py", |
| 329 | "index.js", |
| 330 | "main.go", |
| 331 | "query.sql", |
| 332 | "main.c", |
| 333 | ] { |
8331d0cfeat: the languages this repository actually uses18h | 334 | assert!( |
| 335 | syntax_for(syntaxes(), name, "").is_some(), |
| 336 | "{name} has no syntax" |
| 337 | ); |
979134bfeat: code reads as code, in classes rather than colours19h | 338 | } |
| 339 | } |
8331d0cfeat: the languages this repository actually uses18h | 340 | |
| 341 | #[test] |
| 342 | fn toml_is_highlighted_and_not_merely_recognised() { |
| 343 | let source = source_lines("Cargo.toml", "[package]\nname = \"steid\"\n"); |
| 344 | |
| 345 | assert!(matches!(source.lines[1], SourceLine::Classed(_))); |
| 346 | let html = classes(&source.lines[1]); |
| 347 | assert!(html.contains("hl-string"), "{html}"); |
| 348 | } |
979134bfeat: code reads as code, in classes rather than colours19h | 349 | } |