lmocr 0.1.1

Convert PDFs and images to Markdown using OpenRouter multimodal LLMs
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
pub fn is_page_number_line(line: &str) -> bool {
    if line.chars().all(|c| c.is_ascii_digit()) {
        return true;
    }
    let lower = line.to_ascii_lowercase();
    if lower.len() > 8 {
        return false;
    }
    lower
        .chars()
        .all(|c| matches!(c, 'i' | 'v' | 'x' | 'l' | 'c' | 'd' | 'm'))
}

pub fn contains_page_number_token(line: &str) -> bool {
    let tokens: Vec<&str> = line.split_whitespace().collect();
    if tokens.len() > 5 {
        return false;
    }
    tokens.iter().any(|token| {
        let trimmed = token
            .trim_matches(|c: char| !c.is_ascii_alphanumeric())
            .to_string();
        if trimmed.is_empty() {
            return false;
        }
        is_page_number_line(trimmed.as_str())
    })
}

pub fn looks_like_running_header(line: &str) -> bool {
    let trimmed = line.trim();
    if trimmed.len() < 3 || trimmed.len() > 60 {
        return false;
    }
    let mut letters = 0usize;
    let mut uppercase = 0usize;
    for ch in trimmed.chars() {
        if ch.is_ascii_alphabetic() {
            letters += 1;
            if ch.is_ascii_uppercase() {
                uppercase += 1;
            }
        }
    }
    letters > 0 && uppercase == letters
}

pub fn looks_like_numbered_running_header(line: &str) -> bool {
    let tokens: Vec<&str> = line.split_whitespace().collect();
    if tokens.len() < 3 || tokens.len() > 10 {
        return false;
    }

    let page_token = tokens[tokens.len() - 1].trim_matches(|c: char| !c.is_ascii_alphanumeric());
    if !is_page_number_line(page_token) {
        return false;
    }

    let mut letters = 0usize;
    let mut uppercase = 0usize;
    for token in &tokens[..tokens.len() - 1] {
        for ch in token.chars() {
            if ch.is_ascii_alphabetic() {
                letters += 1;
                if ch.is_ascii_uppercase() {
                    uppercase += 1;
                }
            }
        }
    }
    letters > 0 && uppercase * 100 / letters >= 80
}

pub fn looks_like_boundary_header(line: &str) -> bool {
    let trimmed = line.trim();
    if trimmed.len() < 3 || trimmed.len() > 60 || starts_with_block(trimmed) {
        return false;
    }
    let tokens: Vec<&str> = trimmed.split_whitespace().collect();
    if tokens.is_empty() || tokens.len() > 6 {
        return false;
    }
    if tokens
        .iter()
        .any(|token| token.chars().any(|ch| ch.is_ascii_digit()))
    {
        return false;
    }
    if tokens.iter().any(|token| {
        token
            .chars()
            .any(|ch| matches!(ch, '.' | ',' | ';' | ':' | '!' | '?'))
    }) {
        return false;
    }
    tokens.iter().all(|token| is_title_or_upper_word(token))
}

pub fn is_title_or_upper_word(token: &str) -> bool {
    let word = token.trim_matches(|c: char| !c.is_ascii_alphabetic());
    if word.is_empty() {
        return false;
    }
    if word.chars().all(|ch| ch.is_ascii_uppercase()) {
        return true;
    }
    let mut chars = word.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    first.is_ascii_uppercase() && chars.all(|ch| ch.is_ascii_lowercase())
}

pub fn starts_with_block(s: &str) -> bool {
    let trimmed = s.trim_start();
    trimmed.starts_with('#')
        || trimmed.starts_with("```")
        || trimmed.starts_with("<pre>")
        || trimmed.starts_with('>')
        || trimmed.starts_with("- ")
        || trimmed.starts_with("* ")
        || trimmed.starts_with("+ ")
        || is_numbered_list(trimmed)
}

pub fn ends_with_block(s: &str) -> bool {
    let trimmed = s.trim_end();
    trimmed.ends_with("</pre>")
        || trimmed.ends_with("</table>")
        || trimmed.ends_with("</blockquote>")
        || trimmed.ends_with("***")
        || trimmed.ends_with("---")
}

pub fn is_numbered_list(s: &str) -> bool {
    let mut chars = s.chars();
    let mut saw_digit = false;
    for ch in &mut chars {
        if ch.is_ascii_digit() {
            saw_digit = true;
            continue;
        }
        if saw_digit && (ch == '.' || ch == ')') {
            return true;
        }
        break;
    }
    false
}

pub fn ends_with_hyphen(s: &str) -> bool {
    s.ends_with('-') || s.ends_with('') || s.ends_with('')
}

pub fn last_line_is_list_item(s: &str) -> bool {
    let last_line = s.rsplit('\n').next().unwrap_or(s).trim_start();
    last_line.starts_with("- ") || last_line.starts_with("* ") || last_line.starts_with("+ ")
}

pub fn is_terminal_punct(ch: char) -> bool {
    matches!(ch, '.' | '!' | '?' | '')
}

pub fn last_significant_char(s: &str) -> Option<char> {
    for ch in s.trim_end().chars().rev() {
        if ch.is_whitespace() {
            continue;
        }
        if matches!(ch, '"' | '\'' | '\u{201D}' | '\u{2019}' | ')' | ']' | '}') {
            continue;
        }
        return Some(ch);
    }
    None
}

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

    // ── is_page_number_line ──

    #[test]
    fn page_number_pure_digits() {
        assert!(is_page_number_line("42"));
        assert!(is_page_number_line("1"));
        assert!(is_page_number_line("999"));
    }

    #[test]
    fn page_number_roman_numerals() {
        assert!(is_page_number_line("iv"));
        assert!(is_page_number_line("xii"));
        assert!(is_page_number_line("VII"));
        assert!(is_page_number_line("MCMLXIV")); // 7 chars, under limit
    }

    #[test]
    fn page_number_roman_too_long() {
        // 9 chars of roman-like letters should be rejected
        assert!(!is_page_number_line("mmmmmmmmm"));
    }

    #[test]
    fn page_number_false_positive_short_roman_words() {
        // "civil" (5 chars) is all roman-numeral chars and under the 8-char limit,
        // so it's a known false positive. Acceptable because these short words
        // rarely appear as standalone lines in real PDFs.
        assert!(is_page_number_line("civil"));
        assert!(is_page_number_line("dim"));
        // But longer words are rejected by the length guard
        assert!(!is_page_number_line("civilization"));
    }

    #[test]
    fn page_number_empty_string() {
        // empty string: all(digit) on empty iterator is true
        assert!(is_page_number_line(""));
    }

    #[test]
    fn page_number_mixed_content() {
        assert!(!is_page_number_line("page 5"));
        assert!(!is_page_number_line("42a"));
    }

    // ── contains_page_number_token ──

    #[test]
    fn page_token_with_punctuation() {
        assert!(contains_page_number_token("(7)"));
        assert!(contains_page_number_token("—42—"));
        assert!(contains_page_number_token("CHAPTER iv"));
    }

    #[test]
    fn page_token_too_many_tokens() {
        assert!(!contains_page_number_token("one two three four five 42"));
    }

    #[test]
    fn page_token_at_five_tokens() {
        assert!(contains_page_number_token("A B C D 42"));
    }

    #[test]
    fn page_token_all_punctuation_stripped_empty() {
        // token "---" trims to empty → not a page number
        assert!(!contains_page_number_token("---"));
    }

    // ── looks_like_running_header ──

    #[test]
    fn running_header_all_caps() {
        assert!(looks_like_running_header("CHAPTER ONE"));
        assert!(looks_like_running_header("THE GREAT WAR"));
    }

    #[test]
    fn running_header_mixed_case_rejected() {
        assert!(!looks_like_running_header("Chapter One"));
        assert!(!looks_like_running_header("the great war"));
    }

    #[test]
    fn running_header_too_short() {
        assert!(!looks_like_running_header("AB")); // len 2 < 3
    }

    #[test]
    fn running_header_at_boundary_length() {
        assert!(looks_like_running_header("ABC")); // len 3, exactly at min
        let long = "A".repeat(60);
        assert!(looks_like_running_header(&long)); // len 60, exactly at max
        let too_long = "A".repeat(61);
        assert!(!looks_like_running_header(&too_long));
    }

    #[test]
    fn running_header_with_spaces_and_numbers() {
        // numbers and spaces are not ascii_alphabetic, so they're ignored
        // as long as all letters are uppercase it matches
        assert!(looks_like_running_header("CHAPTER 3"));
    }

    // ── looks_like_numbered_running_header ──

    #[test]
    fn numbered_header_typical() {
        assert!(looks_like_numbered_running_header("FREDERICK THE GREAT 9"));
    }

    #[test]
    fn numbered_header_too_few_tokens() {
        assert!(!looks_like_numbered_running_header("TITLE 9")); // 2 tokens < 3
    }

    #[test]
    fn numbered_header_no_page_number_at_end() {
        assert!(!looks_like_numbered_running_header("CHAPTER THE THIRD end"));
    }

    #[test]
    fn numbered_header_below_80_percent_uppercase() {
        // "ABc DEF 9": before last token: A,B,c,D,E,F = 6 letters, 5 upper = 83% ≥ 80%
        assert!(looks_like_numbered_running_header("ABc DEF 9"));
        // To actually get below 80%: need >20% lowercase
        // "Abcd EF 9": A,b,c,d,E,F = 6 letters, 3 upper = 50% < 80%
        assert!(!looks_like_numbered_running_header("Abcd EF 9"));
    }

    #[test]
    fn numbered_header_at_80_percent_uppercase() {
        // "ABCd EFG 9": before last token: A,B,C,d,E,F,G = 7 letters, 6 upper = 85% ≥ 80%
        assert!(looks_like_numbered_running_header("ABCd EFG 9"));
    }

    #[test]
    fn numbered_header_eleven_tokens_rejected() {
        let line = "A B C D E F G H I J 9";
        let tokens: Vec<&str> = line.split_whitespace().collect();
        assert_eq!(tokens.len(), 11);
        assert!(!looks_like_numbered_running_header(line));
    }

    // ── looks_like_boundary_header ──

    #[test]
    fn boundary_header_title_case() {
        assert!(looks_like_boundary_header("Introduction"));
        assert!(looks_like_boundary_header("Chapter One"));
        assert!(looks_like_boundary_header("CONCLUSION"));
    }

    #[test]
    fn boundary_header_rejects_digits() {
        assert!(!looks_like_boundary_header("Chapter 3"));
    }

    #[test]
    fn boundary_header_rejects_punctuation() {
        assert!(!looks_like_boundary_header("Hello, World"));
        assert!(!looks_like_boundary_header("Section:"));
    }

    #[test]
    fn boundary_header_rejects_block_start() {
        assert!(!looks_like_boundary_header("# Introduction"));
        assert!(!looks_like_boundary_header("> Quote"));
        assert!(!looks_like_boundary_header("- List item"));
    }

    #[test]
    fn boundary_header_rejects_too_many_tokens() {
        assert!(!looks_like_boundary_header(
            "One Two Three Four Five Six Seven"
        )); // 7 tokens
    }

    #[test]
    fn boundary_header_rejects_lowercase_word() {
        assert!(!looks_like_boundary_header("hello world"));
    }

    #[test]
    fn boundary_header_too_short_or_long() {
        assert!(!looks_like_boundary_header("AB")); // 2 chars
        let long = format!("{} End", "Abcdefghij".repeat(6)); // >60 chars
        assert!(!looks_like_boundary_header(&long));
    }

    // ── is_title_or_upper_word ──

    #[test]
    fn title_word_cases() {
        assert!(is_title_or_upper_word("Hello"));
        assert!(is_title_or_upper_word("HELLO"));
        assert!(!is_title_or_upper_word("hello"));
        assert!(!is_title_or_upper_word("hELLO"));
        assert!(!is_title_or_upper_word("HeLLO"));
    }

    #[test]
    fn title_word_with_surrounding_punctuation() {
        assert!(is_title_or_upper_word("(Hello)"));
        assert!(is_title_or_upper_word("\"WORLD\""));
    }

    #[test]
    fn title_word_empty_or_no_alpha() {
        assert!(!is_title_or_upper_word(""));
        assert!(!is_title_or_upper_word("123"));
        assert!(!is_title_or_upper_word("---"));
    }

    // ── starts_with_block / ends_with_block ──

    #[test]
    fn starts_with_block_variants() {
        assert!(starts_with_block("# Heading"));
        assert!(starts_with_block("```rust"));
        assert!(starts_with_block("<pre>code</pre>"));
        assert!(starts_with_block("> quote"));
        assert!(starts_with_block("- item"));
        assert!(starts_with_block("* item"));
        assert!(starts_with_block("+ item"));
        assert!(starts_with_block("1. numbered"));
        assert!(starts_with_block("12) numbered"));
        assert!(!starts_with_block("regular text"));
    }

    #[test]
    fn starts_with_block_leading_whitespace() {
        assert!(starts_with_block("  # Heading"));
        assert!(starts_with_block("\t- item"));
    }

    #[test]
    fn ends_with_block_variants() {
        assert!(ends_with_block("text</pre>"));
        assert!(ends_with_block("text</table>"));
        assert!(ends_with_block("text</blockquote>"));
        assert!(ends_with_block("***"));
        assert!(ends_with_block("---"));
        assert!(!ends_with_block("regular text"));
    }

    #[test]
    fn ends_with_block_trailing_whitespace() {
        assert!(ends_with_block("text</pre>  "));
        assert!(ends_with_block("---\n"));
    }

    // ── is_numbered_list ──

    #[test]
    fn numbered_list_cases() {
        assert!(is_numbered_list("1. item"));
        assert!(is_numbered_list("12. item"));
        assert!(is_numbered_list("1) item"));
        assert!(!is_numbered_list("a. item"));
        assert!(!is_numbered_list("text"));
        assert!(!is_numbered_list("5 text")); // digit but no dot/paren
    }

    // ── ends_with_hyphen ──

    #[test]
    fn hyphen_ascii_and_unicode() {
        assert!(ends_with_hyphen("word-"));
        assert!(ends_with_hyphen("word\u{2010}")); //        assert!(ends_with_hyphen("word\u{2011}")); //        assert!(!ends_with_hyphen("word"));
        assert!(!ends_with_hyphen("word—")); // em dash is NOT a hyphen
    }

    // ── last_line_is_list_item ──

    #[test]
    fn last_line_list_item_multiline() {
        assert!(last_line_is_list_item("paragraph\n- item"));
        assert!(last_line_is_list_item("paragraph\n* item"));
        assert!(last_line_is_list_item("paragraph\n+ item"));
        assert!(!last_line_is_list_item("paragraph\nnot a list"));
    }

    #[test]
    fn last_line_list_item_single_line() {
        assert!(last_line_is_list_item("- item"));
        assert!(!last_line_is_list_item("no dash"));
    }

    // ── is_terminal_punct ──

    #[test]
    fn terminal_punct() {
        assert!(is_terminal_punct('.'));
        assert!(is_terminal_punct('!'));
        assert!(is_terminal_punct('?'));
        assert!(is_terminal_punct(''));
        assert!(!is_terminal_punct(','));
        assert!(!is_terminal_punct(';'));
        assert!(!is_terminal_punct('-'));
    }

    // ── last_significant_char ──

    #[test]
    fn last_significant_skips_quotes_and_brackets() {
        assert_eq!(last_significant_char("word.\""), Some('.'));
        assert_eq!(last_significant_char("word!)"), Some('!'));
        assert_eq!(last_significant_char("end']"), Some('d'));
    }

    #[test]
    fn last_significant_skips_smart_quotes() {
        assert_eq!(last_significant_char("word.\u{201D}"), Some('.'));
        assert_eq!(last_significant_char("word\u{2019}"), Some('d'));
    }

    #[test]
    fn last_significant_all_skippable() {
        assert_eq!(last_significant_char("\"')}]"), None);
        assert_eq!(last_significant_char("   "), None);
        assert_eq!(last_significant_char(""), None);
    }

    #[test]
    fn last_significant_trailing_whitespace() {
        assert_eq!(last_significant_char("word   "), Some('d'));
        assert_eq!(last_significant_char("end.\t\n"), Some('.'));
    }
}