kakemono 0.15.3

Convert scientific-paper PDFs (arXiv) and Word documents into clean, readable HTML in four fidelity modes, Word documents, Markdown, LaTeX or EPUB books
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Syntax colour for code listings, done at conversion time (the page needs
//! no highlighter script).
//!
//! The language of a listing extracted from a PDF is unknown, so it is
//! guessed from the text ([`detect`]). A confident guess gives full
//! colouring; otherwise a generic tokenizer marks only strings, comments
//! and numbers. Tokens become `<span class="tok-*">` around the escaped
//! text: the characters are never changed, so copying the code gives the
//! same text with or without colour.

use crate::render::esc;
use regex::Regex;
use std::sync::LazyLock;

/// A language family with its own keywords and comment syntax.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Lang {
    /// Python (and Python-like pseudocode).
    Python,
    /// C, C++, C#, Java and similar brace languages.
    CFamily,
    /// JavaScript and TypeScript.
    JavaScript,
    /// Go.
    Go,
    /// Rust.
    Rust,
    /// Unknown: only strings, comments and numbers are marked.
    Generic,
}

/// Tokenizer state carried from one line to the next.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) enum Carry {
    /// Nothing open.
    #[default]
    None,
    /// Inside a `/* ... */` comment.
    BlockComment,
    /// Inside a Python string opened with `"""` or `'''` (the quote char).
    TripleString(char),
}

/// One clue for [`detect`]: a pattern, the language it points to and its
/// weight.
struct Clue {
    /// What to look for (multi-line mode).
    pattern: Regex,
    /// The language it suggests.
    lang: Lang,
    /// How strongly.
    weight: u32,
}

/// A guess is confident when its score reaches this...
const CONFIDENT_MIN_SCORE: u32 = 3;
/// ...and is at least this many times the score of the runner-up.
const CONFIDENT_MIN_LEAD: u32 = 2;

/// Keywords of Python.
const PYTHON_KEYWORDS: &[&str] = &[
    "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except",
    "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise",
    "return", "try", "while", "with", "yield",
];
/// Keywords of CFamily.
const CFAMILY_KEYWORDS: &[&str] = &[
    "abstract",
    "auto",
    "break",
    "case",
    "catch",
    "class",
    "const",
    "continue",
    "default",
    "delete",
    "do",
    "else",
    "enum",
    "extends",
    "final",
    "for",
    "goto",
    "if",
    "implements",
    "import",
    "include",
    "inline",
    "interface",
    "namespace",
    "new",
    "operator",
    "package",
    "private",
    "protected",
    "public",
    "return",
    "sizeof",
    "static",
    "struct",
    "switch",
    "template",
    "throw",
    "throws",
    "try",
    "typedef",
    "typename",
    "union",
    "using",
    "virtual",
    "volatile",
    "while",
];
/// Keywords of JavaScript.
const JAVASCRIPT_KEYWORDS: &[&str] = &[
    "async",
    "await",
    "break",
    "case",
    "catch",
    "class",
    "const",
    "continue",
    "default",
    "delete",
    "do",
    "else",
    "export",
    "extends",
    "finally",
    "for",
    "from",
    "function",
    "if",
    "import",
    "in",
    "instanceof",
    "interface",
    "let",
    "new",
    "of",
    "return",
    "switch",
    "throw",
    "try",
    "type",
    "typeof",
    "var",
    "while",
    "yield",
];
/// Keywords of Go.
const GO_KEYWORDS: &[&str] = &[
    "break",
    "case",
    "chan",
    "const",
    "continue",
    "default",
    "defer",
    "else",
    "fallthrough",
    "for",
    "func",
    "go",
    "goto",
    "if",
    "import",
    "interface",
    "map",
    "package",
    "range",
    "return",
    "select",
    "struct",
    "switch",
    "type",
    "var",
];
/// Keywords of Rust.
const RUST_KEYWORDS: &[&str] = &[
    "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", "fn", "for", "if",
    "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "static", "struct", "trait",
    "type", "unsafe", "use", "where", "while",
];
/// Builtin names and constants of Python.
const PYTHON_BUILTINS: &[&str] = &[
    "True",
    "False",
    "None",
    "self",
    "cls",
    "print",
    "len",
    "range",
    "int",
    "float",
    "str",
    "list",
    "dict",
    "set",
    "tuple",
    "zip",
    "enumerate",
    "max",
    "min",
    "sum",
    "abs",
    "np",
    "torch",
];
/// Builtin names and constants of CFamily.
const CFAMILY_BUILTINS: &[&str] = &[
    "true", "false", "null", "nullptr", "NULL", "this", "int", "void", "char", "double", "float", "bool", "long",
    "short", "unsigned", "signed", "size_t",
];
/// Builtin names and constants of JavaScript.
const JAVASCRIPT_BUILTINS: &[&str] = &[
    "true",
    "false",
    "null",
    "undefined",
    "this",
    "console",
    "Math",
    "JSON",
    "window",
    "document",
];
/// Builtin names and constants of Go.
const GO_BUILTINS: &[&str] = &[
    "true", "false", "nil", "iota", "int", "string", "bool", "byte", "error", "float64", "make", "len", "append", "new",
];
/// Builtin names and constants of Rust.
const RUST_BUILTINS: &[&str] = &[
    "true", "false", "self", "Self", "Some", "None", "Ok", "Err", "i32", "i64", "u8", "u32", "u64", "usize", "f32",
    "f64", "bool", "str", "String", "Vec",
];

/// The clues of [`detect`], compiled once.
static CLUES: LazyLock<Vec<Clue>> = LazyLock::new(|| {
    let clue = |pattern: &str, lang: Lang, weight: u32| Clue {
        pattern: Regex::new(&format!("(?m){pattern}")).unwrap(),
        lang,
        weight,
    };
    vec![
        clue(r"^\s*def \w+\(", Lang::Python, 3),
        clue(r"^\s*(?:import \w|from \w[\w.]* import)", Lang::Python, 3),
        clue(
            r"^\s*(?:if|elif|for|while|with|else|try|except)\b[^{;]*:\s*(?:#.*)?$",
            Lang::Python,
            1,
        ),
        clue(r"\bself\.", Lang::Python, 2),
        clue(r"\b(?:None|True|False|elif|lambda)\b", Lang::Python, 1),
        clue(r"^\s*#\s*\w", Lang::Python, 1),
        clue(r"^\s*#include\b", Lang::CFamily, 4),
        clue(
            r"\bstd::|\bprintf\(|\bpublic static\b|\bSystem\.out\b",
            Lang::CFamily,
            3,
        ),
        clue(
            r"\b(?:int|void|char|double|float|bool|unsigned)\s+\**\w+\s*[(=;,\[]",
            Lang::CFamily,
            2,
        ),
        clue(r";\s*$", Lang::CFamily, 1),
        clue(r"\b(?:function\s+\w*\(|console\.\w+\(|=>|===|!==)", Lang::JavaScript, 3),
        clue(r"^\s*(?:const|let|var)\s+\w+\s*=", Lang::JavaScript, 2),
        clue(r"^\s*(?:package\s+\w+|func\s+(?:\(\w+ \*?\w+\)\s*)?\w+\()", Lang::Go, 4),
        clue(r":=|\bfmt\.\w+\(|\bnil\b", Lang::Go, 2),
        clue(r"^\s*(?:pub\s+)?fn\s+\w+|\blet mut\b|\bimpl\b|\w+!\(", Lang::Rust, 3),
    ]
});

/// Keywords of each language.
fn keywords(lang: Lang) -> &'static [&'static str] {
    match lang {
        Lang::Python => PYTHON_KEYWORDS,
        Lang::CFamily => CFAMILY_KEYWORDS,
        Lang::JavaScript => JAVASCRIPT_KEYWORDS,
        Lang::Go => GO_KEYWORDS,
        Lang::Rust => RUST_KEYWORDS,
        Lang::Generic => &[],
    }
}

/// Builtin names and constants of each language.
fn builtins(lang: Lang) -> &'static [&'static str] {
    match lang {
        Lang::Python => PYTHON_BUILTINS,
        Lang::CFamily => CFAMILY_BUILTINS,
        Lang::JavaScript => JAVASCRIPT_BUILTINS,
        Lang::Go => GO_BUILTINS,
        Lang::Rust => RUST_BUILTINS,
        Lang::Generic => &[],
    }
}

/// Guesses the language of `code`. Returns the language and whether the
/// guess is confident (otherwise the listing is shown plain by default).
pub(crate) fn detect(code: &str) -> (Lang, bool) {
    let mut scores: Vec<(Lang, u32)> = Vec::new();
    for clue in CLUES.iter() {
        let hits = clue.pattern.find_iter(code).count() as u32;
        if hits == 0 {
            continue;
        }
        match scores.iter_mut().find(|(lang, _)| *lang == clue.lang) {
            Some((_, score)) => *score += hits * clue.weight,
            None => scores.push((clue.lang, hits * clue.weight)),
        }
    }
    scores.sort_by_key(|(_, score)| std::cmp::Reverse(*score));
    match scores.as_slice() {
        [] => (Lang::Generic, false),
        [(lang, score)] => (*lang, *score >= CONFIDENT_MIN_SCORE),
        [(lang, score), (_, second), ..] => (
            *lang,
            *score >= CONFIDENT_MIN_SCORE && *score >= CONFIDENT_MIN_LEAD * second,
        ),
    }
}

/// One line of code as HTML with its tokens wrapped in `tok-*` spans.
/// `carry` is the state left by the previous line and is updated.
pub(crate) fn highlight_line(line: &str, lang: Lang, carry: &mut Carry) -> String {
    let chars: Vec<char> = line.chars().collect();
    let mut out = String::new();
    let mut pos = 0;
    while pos < chars.len() {
        let (class, end) = match next_token(&chars, pos, lang, carry) {
            Some(token) => token,
            None => {
                let end = plain_run_end(&chars, pos);
                out.push_str(&esc(&chars[pos..end].iter().collect::<String>()));
                pos = end;
                continue;
            }
        };
        let text: String = chars[pos..end].iter().collect();
        out.push_str(&format!("<span class=\"tok-{class}\">{}</span>", esc(&text)));
        pos = end;
    }
    out
}

/// The token starting at `pos` (its class and end), or `None` when the
/// character starts no token. Also continues a comment or string opened on
/// an earlier line (see [`Carry`]).
fn next_token(chars: &[char], pos: usize, lang: Lang, carry: &mut Carry) -> Option<(&'static str, usize)> {
    match *carry {
        Carry::BlockComment => return Some(("com", block_comment_end(chars, pos, carry))),
        Carry::TripleString(quote) => return Some(("str", triple_string_end(chars, pos, quote, carry))),
        Carry::None => {}
    }
    let ch = chars[pos];
    let next = chars.get(pos + 1).copied();
    if starts_line_comment(chars, pos, lang) {
        return Some(("com", chars.len()));
    }
    if ch == '/' && next == Some('*') && lang != Lang::Python {
        *carry = Carry::BlockComment;
        return Some(("com", block_comment_end(chars, pos + 2, carry)));
    }
    if matches!(ch, '"' | '\'' | '`') {
        return Some(("str", string_end(chars, pos, lang, carry)));
    }
    if ch.is_ascii_digit() || (ch == '.' && next.is_some_and(|nc| nc.is_ascii_digit())) {
        if pos > 0 && is_ident_char(chars[pos - 1]) {
            return None;
        }
        return Some(("num", number_end(chars, pos)));
    }
    if ch == '@' && lang != Lang::Generic && next.is_some_and(is_ident_start) {
        return Some(("dec", ident_end(chars, pos + 1)));
    }
    if is_ident_start(ch) && lang != Lang::Generic {
        return identifier(chars, pos, lang);
    }
    None
}

/// Whether a line comment starts at `pos`: `#` in Python (and generic
/// code, at the start or after a space), `//` in brace languages.
fn starts_line_comment(chars: &[char], pos: usize, lang: Lang) -> bool {
    let ch = chars[pos];
    let next = chars.get(pos + 1).copied();
    let after_space = pos == 0 || chars[pos - 1].is_whitespace();
    match lang {
        Lang::Python => ch == '#',
        Lang::Generic => (ch == '#' && after_space) || (ch == '/' && next == Some('/') && after_space),
        _ => ch == '/' && next == Some('/'),
    }
}

/// An identifier at `pos`: a keyword, builtin, type (capitalised name in
/// brace languages), function (followed by `(`), or plain (`None`).
fn identifier(chars: &[char], pos: usize, lang: Lang) -> Option<(&'static str, usize)> {
    let end = ident_end(chars, pos);
    let word: String = chars[pos..end].iter().collect();
    if pos > 0 && chars[pos - 1] == '.' {
        // A member: only calls are marked.
        return next_non_space(chars, end).filter(|ch| *ch == '(').map(|_| ("fn", end));
    }
    if keywords(lang).contains(&word.as_str()) {
        return Some(("kw", end));
    }
    if builtins(lang).contains(&word.as_str()) {
        return Some(("builtin", end));
    }
    if next_non_space(chars, end) == Some('(') {
        return Some(("fn", end));
    }
    let capitalised = word.chars().next().is_some_and(char::is_uppercase) && word.chars().any(char::is_lowercase);
    if capitalised && lang != Lang::Python {
        return Some(("type", end));
    }
    None
}

/// The end of a run of characters starting at `pos` that start no token: a
/// whole word, or punctuation and spaces up to the next character that may
/// start one.
fn plain_run_end(chars: &[char], pos: usize) -> usize {
    if is_ident_char(chars[pos]) {
        return ident_end(chars, pos);
    }
    let mut end = pos + 1;
    while end < chars.len()
        && !is_ident_char(chars[end])
        && !matches!(chars[end], '"' | '\'' | '`' | '#' | '/' | '@' | '.')
    {
        end += 1;
    }
    end
}

/// The end of a string starting at `pos` (an unclosed string runs to the
/// end of the line; a Python triple quote may continue on the next lines).
fn string_end(chars: &[char], pos: usize, lang: Lang, carry: &mut Carry) -> usize {
    let quote = chars[pos];
    if lang == Lang::Python && chars.get(pos + 1) == Some(&quote) && chars.get(pos + 2) == Some(&quote) {
        *carry = Carry::TripleString(quote);
        return triple_string_end(chars, pos + 3, quote, carry);
    }
    let mut end = pos + 1;
    while end < chars.len() {
        match chars[end] {
            '\\' => end += 2,
            ch if ch == quote => return end + 1,
            _ => end += 1,
        }
    }
    chars.len()
}

/// The end of a triple-quoted string continued from `pos`; clears `carry`
/// when it closes on this line.
fn triple_string_end(chars: &[char], pos: usize, quote: char, carry: &mut Carry) -> usize {
    let mut end = pos;
    while end < chars.len() {
        if chars[end] == quote && chars.get(end + 1) == Some(&quote) && chars.get(end + 2) == Some(&quote) {
            *carry = Carry::None;
            return end + 3;
        }
        end += 1;
    }
    chars.len()
}

/// The end of a block comment continued from `pos`; clears `carry` when it
/// closes on this line.
fn block_comment_end(chars: &[char], pos: usize, carry: &mut Carry) -> usize {
    let mut end = pos;
    while end + 1 < chars.len() {
        if chars[end] == '*' && chars[end + 1] == '/' {
            *carry = Carry::None;
            return end + 2;
        }
        end += 1;
    }
    chars.len()
}

/// The end of a number starting at `pos` (digits, `.`, `_`, exponent,
/// hex digits, type suffixes).
fn number_end(chars: &[char], pos: usize) -> usize {
    let mut end = pos;
    while end < chars.len() {
        let ch = chars[end];
        let exponent_sign = matches!(ch, '+' | '-') && end > pos && matches!(chars[end - 1], 'e' | 'E');
        if ch.is_ascii_alphanumeric() || ch == '.' || ch == '_' || exponent_sign {
            end += 1;
        } else {
            break;
        }
    }
    end
}

/// The end of an identifier starting at `pos`.
fn ident_end(chars: &[char], pos: usize) -> usize {
    let mut end = pos;
    while end < chars.len() && is_ident_char(chars[end]) {
        end += 1;
    }
    end.max(pos + 1)
}

/// The first non-space character at or after `pos`.
fn next_non_space(chars: &[char], pos: usize) -> Option<char> {
    chars[pos.min(chars.len())..].iter().copied().find(|ch| *ch != ' ')
}

/// Whether `ch` can start an identifier.
fn is_ident_start(ch: char) -> bool {
    ch.is_alphabetic() || ch == '_'
}

/// Whether `ch` can continue an identifier.
fn is_ident_char(ch: char) -> bool {
    ch.is_alphanumeric() || ch == '_'
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Highlights a whole snippet line by line.
    fn colour(code: &str, lang: Lang) -> String {
        let mut carry = Carry::None;
        code.lines()
            .map(|line| highlight_line(line, lang, &mut carry))
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Removes the token spans and unescapes the text.
    fn strip(html: &str) -> String {
        let tags = Regex::new(r"</?span[^>]*>").unwrap();
        tags.replace_all(html, "")
            .replace("&lt;", "<")
            .replace("&gt;", ">")
            .replace("&quot;", "\"")
            .replace("&amp;", "&")
    }

    #[test]
    fn detects_common_languages() {
        assert_eq!(
            detect("import numpy as np\ndef f(x):\n    return np.exp(x)\n"),
            (Lang::Python, true)
        );
        assert_eq!(
            detect("#include <stdio.h>\nint main(void) {\n  printf(\"hi\");\n}\n").0,
            Lang::CFamily
        );
        assert_eq!(
            detect("const f = (x) => x * 2;\nconsole.log(f(3));\n").0,
            Lang::JavaScript
        );
        assert_eq!(detect("package main\nfunc main() {\n  x := 1\n}\n").0, Lang::Go);
        assert!(!detect("my dog is [MASK]").1);
    }

    #[test]
    fn colouring_keeps_the_text() {
        let code = "def f(a, b=\"x<y\"):  # note & more\n    return a @ b + 1.5e-3\n'''doc\nstring'''";
        assert_eq!(strip(&colour(code, Lang::Python)), code);
        let html = colour(code, Lang::Python);
        assert!(html.contains("<span class=\"tok-kw\">def</span>"));
        assert!(html.contains("<span class=\"tok-fn\">f</span>"));
        assert!(html.contains("<span class=\"tok-com\"># note &amp; more</span>"));
        assert!(html.contains("<span class=\"tok-num\">1.5e-3</span>"));
        assert!(html.contains("<span class=\"tok-str\">string'''</span>"));
        let brace = "/* a\n b */ int x = 0x1F; // end";
        assert_eq!(strip(&colour(brace, Lang::CFamily)), brace);
    }
}