paperscope 0.1.1

Paper: local-first document search with exact page and line citations. CLI + MCP server in one binary.
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
use super::{single, Block, Capabilities, ExtractedDocument, Extractor, MAX_EXTRACTED_BYTES};
use anyhow::Result;
use quick_xml::events::Event;
use quick_xml::Reader;
use std::io::{Cursor, Read, Seek};
use zip::ZipArchive;

fn open_zip(bytes: &[u8]) -> Result<ZipArchive<Cursor<&[u8]>>> {
    Ok(ZipArchive::new(Cursor::new(bytes))?)
}

fn read_entry<R: Read + Seek>(zip: &mut ZipArchive<R>, name: &str) -> Option<String> {
    let mut file = zip.by_name(name).ok()?;
    if file.size() as usize > MAX_EXTRACTED_BYTES {
        return None;
    }
    let mut out = String::new();
    file.by_ref()
        .take((MAX_EXTRACTED_BYTES + 1) as u64)
        .read_to_string(&mut out)
        .ok()?;
    if out.len() > MAX_EXTRACTED_BYTES {
        return None;
    }
    Some(out)
}

fn zip_has_entry(bytes: &[u8], name: &str) -> bool {
    match open_zip(bytes) {
        Ok(mut zip) => zip.by_name(name).is_ok(),
        Err(_) => false,
    }
}

fn zip_has_prefix(bytes: &[u8], prefix: &str) -> bool {
    match open_zip(bytes) {
        Ok(mut zip) => {
            for i in 0..zip.len().min(64) {
                let Ok(file) = zip.by_index(i) else { continue };
                let fname = file.name().map(|cow| cow.into_owned()).unwrap_or_default();
                if fname.starts_with(prefix) {
                    return true;
                }
            }
            false
        }
        Err(_) => false,
    }
}

fn attr_value(e: &quick_xml::events::BytesStart<'_>, key: &str) -> Option<String> {
    for attr in e.attributes().flatten() {
        if attr.key.as_ref() == key {
            return Some(attr.value.to_string());
        }
    }
    None
}

pub struct Docx;

impl Extractor for Docx {
    fn name(&self) -> &'static str {
        "docx"
    }

    fn extensions(&self) -> &'static [&'static str] {
        &["docx"]
    }

    fn sniff(&self, bytes: &[u8]) -> bool {
        zip_has_entry(bytes, "word/document.xml")
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            max_file_bytes: 128 * 1024 * 1024, // 128 MB for DOCX
            max_pages: 1,
            max_entries: 1,
            streaming: false,
            thread_safe: true,
            requires_external: false,
        }
    }

    fn extract(&self, name: &str, bytes: &[u8]) -> Result<Option<Vec<ExtractedDocument>>> {
        let mut zip = open_zip(bytes)?;
        let Some(xml) = read_entry(&mut zip, "word/document.xml") else {
            return Ok(None);
        };

        let mut reader = Reader::from_str(&xml);
        let mut buf = Vec::new();
        let mut paragraphs: Vec<Paragraph> = Vec::new();
        let mut current = String::new();
        let mut in_text = false;
        let mut level: Option<u8> = None;

        let finish_paragraph =
            |paragraphs: &mut Vec<Paragraph>, text: String, level: Option<u8>| {
                paragraphs.push(Paragraph { text, level });
            };

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(e)) => {
                    if e.name().as_ref() == "w:pStyle" {
                        level = style_level(&e);
                    } else if e.name().as_ref() == "w:t" {
                        in_text = true;
                    }
                }
                Ok(Event::Empty(e)) if e.name().as_ref() == "w:pStyle" => {
                    level = style_level(&e);
                }
                Ok(Event::Text(e)) if in_text => {
                    current.push_str(e.xml10_content().as_ref());
                }
                Ok(Event::End(e)) => {
                    if e.name().as_ref() == "w:t" {
                        in_text = false;
                    } else if e.name().as_ref() == "w:p" {
                        finish_paragraph(&mut paragraphs, std::mem::take(&mut current), level);
                        level = None;
                    }
                }
                Ok(Event::Eof) => break,
                Err(err) => {
                    return Ok(Some(single(
                        name,
                        "docx",
                        Vec::new(),
                        vec![format!("malformed document.xml: {err}")],
                    )))
                }
                _ => {}
            }
            buf.clear();
        }
        let blocks = merge_paragraphs(paragraphs, 350);
        Ok(Some(single(name, "docx", blocks, Vec::new())))
    }
}

struct Paragraph {
    text: String,
    level: Option<u8>,
}

fn style_level(e: &quick_xml::events::BytesStart<'_>) -> Option<u8> {
    let value = attr_value(e, "w:val")?.to_ascii_lowercase();
    if value == "title" {
        return Some(1);
    }
    if !value.starts_with("heading") {
        return None;
    }
    value
        .trim_start_matches("heading")
        .trim()
        .parse::<u8>()
        .ok()
        .or(Some(1))
}

fn merge_paragraphs(paragraphs: Vec<Paragraph>, max_words: usize) -> Vec<Block> {
    let mut blocks = Vec::new();
    let mut path: Vec<(u8, String)> = Vec::new();
    let mut bucket = String::new();
    let mut first = 1usize;
    let mut words = 0usize;

    let mut current_line = 1i64;
    let flush = |blocks: &mut Vec<Block>,
                 bucket: &mut String,
                 first: usize,
                 last: usize,
                 path: &[(u8, String)],
                 current_line: &mut i64| {
        if bucket.trim().is_empty() {
            return;
        }
        let anchor = if first == last {
            format!("para {first}")
        } else {
            format!("para {first}-{last}")
        };
        let text = std::mem::take(bucket);
        let line_count = text.lines().count().max(1) as i64;
        let start = *current_line;
        let end = start + line_count - 1;
        *current_line += line_count;
        blocks.push(Block {
            text,
            page: 0,
            anchor: Some(anchor),
            section: section_of(path),
            line_start: start,
            line_end: end,
        });
    };

    for (idx, paragraph) in paragraphs.iter().enumerate() {
        let number = idx + 1;
        if let Some(level) = paragraph.level {
            flush(
                &mut blocks,
                &mut bucket,
                first,
                number - 1,
                &path,
                &mut current_line,
            );
            words = 0;
            path.retain(|(existing, _)| *existing < level);
            path.push((level, paragraph.text.trim().to_string()));
            first = number;
        }
        if bucket.trim().is_empty() {
            first = number;
        }
        let paragraph_words = paragraph.text.split_whitespace().count();
        bucket.push_str(&paragraph.text);
        bucket.push('\n');
        words += paragraph_words;
        if words >= max_words {
            flush(
                &mut blocks,
                &mut bucket,
                first,
                number,
                &path,
                &mut current_line,
            );
            words = 0;
        }
    }
    flush(
        &mut blocks,
        &mut bucket,
        first,
        paragraphs.len().max(1),
        &path,
        &mut current_line,
    );
    blocks
}

fn section_of(path: &[(u8, String)]) -> Option<String> {
    if path.is_empty() {
        return None;
    }
    Some(
        path.iter()
            .map(|(_, title)| title.as_str())
            .collect::<Vec<_>>()
            .join(" / "),
    )
}

pub struct Pptx;

impl Extractor for Pptx {
    fn name(&self) -> &'static str {
        "pptx"
    }

    fn extensions(&self) -> &'static [&'static str] {
        &["pptx"]
    }

    fn sniff(&self, bytes: &[u8]) -> bool {
        zip_has_prefix(bytes, "ppt/slides/slide")
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            max_file_bytes: 256 * 1024 * 1024, // 256 MB for PPTX
            max_pages: 1_000,                  // max slides
            max_entries: 1_000,
            streaming: false,
            thread_safe: true,
            requires_external: false,
        }
    }

    fn extract(&self, name: &str, bytes: &[u8]) -> Result<Option<Vec<ExtractedDocument>>> {
        let mut zip = open_zip(bytes)?;
        let mut slides: Vec<(u32, String)> = Vec::new();
        for i in 0..zip.len().min(1000) {
            let Ok(mut file) = zip.by_index(i) else {
                continue;
            };
            if file.size() as usize > MAX_EXTRACTED_BYTES {
                continue;
            }
            let fname = file.name().map(|cow| cow.into_owned()).unwrap_or_default();
            let Some(rest) = fname.strip_prefix("ppt/slides/slide") else {
                continue;
            };
            let Some(num) = rest
                .strip_suffix(".xml")
                .and_then(|n| n.parse::<u32>().ok())
            else {
                continue;
            };
            let mut data = String::new();
            if file
                .by_ref()
                .take((MAX_EXTRACTED_BYTES + 1) as u64)
                .read_to_string(&mut data)
                .is_ok()
                && data.len() <= MAX_EXTRACTED_BYTES
            {
                slides.push((num, data));
            }
        }
        slides.sort_by_key(|(n, _)| *n);

        let mut blocks = Vec::new();
        for (number, xml) in slides {
            let mut reader = Reader::from_str(&xml);
            let mut buf = Vec::new();
            let mut text = String::new();
            let mut in_text = false;
            loop {
                match reader.read_event_into(&mut buf) {
                    Ok(Event::Start(e)) => {
                        if e.name().as_ref() == "a:t" {
                            in_text = true;
                        } else if e.name().as_ref() == "a:p"
                            && !text.is_empty()
                            && !text.ends_with('\n')
                        {
                            text.push('\n');
                        }
                    }
                    Ok(Event::Text(e)) if in_text => {
                        text.push_str(e.xml10_content().as_ref());
                    }
                    Ok(Event::End(e)) => {
                        if e.name().as_ref() == "a:t" {
                            in_text = false;
                        } else if e.name().as_ref() == "a:p" {
                            text.push('\n');
                        }
                    }
                    Ok(Event::Eof) => break,
                    Err(_) => break,
                    _ => {}
                }
                buf.clear();
            }
            if text.trim().is_empty() {
                continue;
            }
            let line_count = text.lines().count().max(1) as i64;
            blocks.push(Block {
                text,
                page: number as i64,
                anchor: Some(format!("slide {number}")),
                section: None,
                line_start: 1,
                line_end: line_count,
            });
        }
        Ok(Some(single(name, "pptx", blocks, Vec::new())))
    }
}

pub struct Epub;

impl Extractor for Epub {
    fn name(&self) -> &'static str {
        "epub"
    }

    fn extensions(&self) -> &'static [&'static str] {
        &["epub"]
    }

    fn sniff(&self, bytes: &[u8]) -> bool {
        zip_has_entry(bytes, "META-INF/container.xml")
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            max_file_bytes: 128 * 1024 * 1024, // 128 MB for EPUB
            max_pages: 5_000,                  // spine items
            max_entries: 5_000,
            streaming: false,
            thread_safe: true,
            requires_external: false,
        }
    }

    fn extract(&self, name: &str, bytes: &[u8]) -> Result<Option<Vec<ExtractedDocument>>> {
        let mut zip = open_zip(bytes)?;
        let container = read_entry(&mut zip, "META-INF/container.xml").unwrap_or_default();
        let opf_path = container_path(&container);

        let (mut spine, mut manifest) = (Vec::new(), Vec::new());
        if let Some(opf) = opf_path.as_deref().and_then(|p| read_entry(&mut zip, p)) {
            parse_opf(&opf, &mut manifest, &mut spine);
        }
        let base = opf_path
            .as_deref()
            .and_then(|p| p.rsplit_once('/').map(|(dir, _)| dir.to_string()))
            .unwrap_or_default();

        let mut blocks = Vec::new();
        let mut order: Vec<String> = Vec::new();
        for idref in &spine {
            if let Some(href) = manifest.iter().find(|(id, _)| id == idref).map(|(_, h)| h) {
                order.push(join_path(&base, href));
            }
        }
        if order.is_empty() {
            for i in 0..zip.len() {
                let Ok(f) = zip.by_index(i) else { continue };
                let fname = f.name().map(|cow| cow.into_owned()).unwrap_or_default();
                if fname.ends_with(".xhtml") || fname.ends_with(".html") || fname.ends_with(".htm")
                {
                    order.push(fname);
                }
            }
            order.sort();
        }

        for (index, name) in order.iter().enumerate() {
            let Some(raw) = read_entry(&mut zip, name) else {
                continue;
            };
            let text = html2text::from_read(raw.as_bytes(), 100).unwrap_or(raw);
            if text.trim().is_empty() {
                continue;
            }
            let line_count = text.lines().count().max(1) as i64;
            blocks.push(Block {
                text,
                page: (index + 1) as i64,
                anchor: Some(format!("spine {}: {name}", index + 1)),
                section: None,
                line_start: 1,
                line_end: line_count,
            });
        }
        Ok(Some(single(name, "epub", blocks, Vec::new())))
    }
}

fn container_path(xml: &str) -> Option<String> {
    let mut reader = Reader::from_str(xml);
    let mut buf = Vec::new();
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) | Ok(Event::Empty(e)) if e.name().as_ref() == "rootfile" => {
                if let Some(path) = attr_value(&e, "full-path") {
                    return Some(path);
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }
    None
}

fn parse_opf(xml: &str, manifest: &mut Vec<(String, String)>, spine: &mut Vec<String>) {
    let mut reader = Reader::from_str(xml);
    let mut buf = Vec::new();
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) | Ok(Event::Empty(e)) => {
                if e.name().as_ref() == "item" {
                    if let (Some(id), Some(href)) = (attr_value(&e, "id"), attr_value(&e, "href")) {
                        manifest.push((id, href));
                    }
                } else if e.name().as_ref() == "itemref" {
                    if let Some(idref) = attr_value(&e, "idref") {
                        spine.push(idref);
                    }
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }
}

fn join_path(base: &str, href: &str) -> String {
    let href = href.split('#').next().unwrap_or(href);
    if base.is_empty() {
        href.to_string()
    } else {
        format!("{base}/{href}")
    }
}