kakemono 0.19.1

Convert PDF, Word and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
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
//! High-level entry points: PDF, Word, EPUB or HTML bytes (or a file, or
//! an address of the Web) in, structured document and HTML (or Word,
//! Markdown, LaTeX or EPUB) out.

use crate::analysis::pipeline::{Analysis, analyze};
use crate::backend::{PageRenderer, PdfFile, PngCompression};
use crate::config::AnalyzeOptions;
use crate::model::{Document, Meta};
use crate::render::{
    Chunk, ChunkOptions, ContentReport, DocxOptions, DocxOutput, EpubOptions, HtmlOptions, HtmlOutput, JsonOptions,
    JsonOutput, LatexOptions, LatexOutput, MarkdownOptions, MarkdownOutput, Mode, SourceInfo, TypstOptions,
    TypstOutput, apply_content, render, render_chunks, render_docx, render_docx_with_images, render_epub, render_json,
    render_latex, render_markdown, render_to, render_typst,
};
use crate::word::{self, WordFormat};
use anyhow::{Context, Result};
use sha2::{Digest, Sha256};
use std::path::Path;

/// The format of the file a [`Paper`] was loaded from. More formats may
/// be added: a `match` on it needs a `_` arm.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum InputFormat {
    /// A PDF.
    Pdf,
    /// A Word document in Office Open XML (`.docx`).
    Docx,
    /// A Word 97–2003 document (`.doc`).
    Doc,
    /// An EPUB book (`.epub`).
    Epub,
    /// A web page (`.html`, or an address of the Web).
    Html,
}

impl InputFormat {
    /// The usual file extension (`pdf`, `docx`, `doc`, `epub`, `html`), also the name of the
    /// format in the JSON output.
    pub fn name(self) -> &'static str {
        match self {
            InputFormat::Pdf => "pdf",
            InputFormat::Docx => "docx",
            InputFormat::Doc => "doc",
            InputFormat::Epub => "epub",
            InputFormat::Html => "html",
        }
    }

    /// Whether a document of this format has pages to show: only a PDF.
    /// The `hard`, `hard-95` and `medium` modes need them.
    pub fn has_pages(self) -> bool {
        self == InputFormat::Pdf
    }
}

/// What a [`Paper`] holds.
enum Source {
    /// A PDF, analysed on demand.
    Pdf(PdfFile),
    /// A Word document, read into its structure when loaded, with its file
    /// (read again with its formatting for the layout).
    Word(Box<Document>, InputFormat, Vec<u8>),
    /// An EPUB book or a web page (the format), read into its structure
    /// when loaded.
    Book(Box<Document>, InputFormat),
}

/// A loaded PDF, Word document, EPUB book or web page, ready to be
/// analysed and rendered.
///
/// Typical use: [`Paper::from_bytes`], [`Paper::analyze`], then
/// [`Paper::to_html`] once per output mode (the analysis does not depend on
/// the mode, so it is computed once).
pub struct Paper {
    /// The document itself.
    file: Source,
    /// Where the document came from (file name, arXiv id, ...), recorded in
    /// the output.
    source: Option<String>,
    /// SHA-256 of the bytes of the file, in hexadecimal: the JSON output
    /// names its source with it, and an edited JSON file is only applied to
    /// the same file.
    sha256: String,
    /// A Word document laid out in pages, read back as a PDF and analysed,
    /// for the modes that show pages (made when first needed; `None` when
    /// the layout failed).
    laid: std::sync::OnceLock<Option<Laid>>,
}

/// A Word document laid out in pages as Word lays it out.
struct Laid {
    /// The PDF of the pages.
    pdf: PdfFile,
    /// Its analysis.
    analysis: Analysis,
}

impl Paper {
    /// Loads a PDF, a Word document (`.docx`, `.doc`), an EPUB book
    /// (`.epub`) or a web page (HTML) from its bytes; the format is
    /// recognised from the bytes. The pictures of a web page loaded so are
    /// only those inside it (`data:` URLs): [`Paper::from_path`] reads them
    /// next to its file, [`Paper::from_url`] fetches them.
    ///
    /// # Errors
    /// When the data is not a readable PDF, Word document, EPUB book or
    /// web page, or the PDF has no pages.
    pub fn from_bytes(data: Vec<u8>) -> Result<Self> {
        if crate::html::is_html(&data) {
            return Self::from_html(&data, crate::html::Origin::Bytes);
        }
        let sha256 = sha256_hex(&data);
        if crate::epub::is_epub(&data) {
            return Ok(Self {
                file: Source::Book(Box::new(crate::epub::read(&data, None)?), InputFormat::Epub),
                source: None,
                sha256,
                laid: std::sync::OnceLock::new(),
            });
        }
        let file = match word::detect(&data) {
            Some(format) => {
                let doc = word::read(&data, format, None)?;
                let format = match format {
                    WordFormat::Docx => InputFormat::Docx,
                    WordFormat::Doc => InputFormat::Doc,
                };
                Source::Word(Box::new(doc), format, data)
            }
            None => Source::Pdf(PdfFile::open(data)?),
        };
        Ok(Self {
            file,
            source: None,
            sha256,
            laid: std::sync::OnceLock::new(),
        })
    }

    /// Loads the file at `path`, as [`Paper::from_bytes`] does; the
    /// pictures and style sheets of a web page are read next to it.
    ///
    /// # Errors
    /// When the file cannot be read, or [`Paper::from_bytes`] fails.
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();
        let data = std::fs::read(path).with_context(|| format!("reading {}", path.display()))?;
        if crate::html::is_html(&data) {
            return Self::from_html(&data, crate::html::Origin::File(path));
        }
        Self::from_bytes(data)
    }

    /// Fetches the document at the `http(s)` address `url` and loads it, as
    /// [`Paper::from_bytes`] does: a web page with its pictures (fetched
    /// too), or a PDF, a Word document or an EPUB book. Its source
    /// ([`Meta::source`]) is the address.
    ///
    /// # Errors
    /// When the document cannot be fetched (see
    /// [`source::web::fetch`](crate::source::web::fetch)) or read.
    pub fn from_url(url: &str) -> Result<Self> {
        let fetched = crate::source::web::fetch(url)?;
        let paper = if fetched.is_html() || crate::html::is_html(&fetched.data) {
            let origin = crate::html::Origin::Url {
                url: &fetched.url,
                charset: fetched.charset(),
            };
            Self::from_html(&fetched.data, origin)?
        } else {
            Self::from_bytes(fetched.data)?
        };
        Ok(paper.with_source(url.trim()))
    }

    /// Loads the web page `data` saved from the `http(s)` address `url`: its
    /// links and pictures are read as at that address, and its pictures are
    /// fetched. Its source ([`Meta::source`]) is the address.
    ///
    /// # Errors
    /// When `url` is not a URL.
    pub fn from_page_at(data: &[u8], url: &str) -> Result<Self> {
        let origin = crate::html::Origin::Url {
            url: url.trim(),
            charset: None,
        };
        Ok(Self::from_html(data, origin)?.with_source(url.trim()))
    }

    /// Loads the web page `data` from `origin`.
    fn from_html(data: &[u8], origin: crate::html::Origin<'_>) -> Result<Self> {
        Ok(Self {
            file: Source::Book(Box::new(crate::html::read(data, origin, None)?), InputFormat::Html),
            source: None,
            sha256: sha256_hex(data),
            laid: std::sync::OnceLock::new(),
        })
    }

    /// The format of the file.
    pub fn format(&self) -> InputFormat {
        match &self.file {
            Source::Pdf(_) => InputFormat::Pdf,
            Source::Word(_, format, _) => *format,
            Source::Book(_, format) => *format,
        }
    }

    /// The PDF, when the paper is one.
    fn pdf(&self) -> Option<&PdfFile> {
        match &self.file {
            Source::Pdf(pdf) => Some(pdf),
            Source::Word(..) | Source::Book(..) => None,
        }
    }

    /// The format a Word document is read with.
    fn word_format(&self) -> Option<WordFormat> {
        match &self.file {
            Source::Pdf(_) | Source::Book(..) => None,
            Source::Word(_, InputFormat::Doc, _) => Some(WordFormat::Doc),
            Source::Word(..) => Some(WordFormat::Docx),
        }
    }

    /// The Word document laid out in pages and analysed, made once.
    fn laid(&self) -> Option<&Laid> {
        self.word_format()?;
        self.laid
            .get_or_init(|| {
                let pdf = PdfFile::open(self.to_pdf().ok()?).ok()?;
                let (raw, fonts) = pdf.extract(1);
                let meta = Meta {
                    source: self.source.clone(),
                    ..Default::default()
                };
                let analysis = analyze(raw, &fonts, meta, AnalyzeOptions::default());
                Some(Laid { pdf, analysis })
            })
            .as_ref()
    }

    /// The HTML of a Word document in a mode that shows its layout: its
    /// pages (`hard`, `hard-95`), or Word's formatting in one continuous
    /// page (`medium`). `None` for a PDF, the `friendly` mode, or when the
    /// layout failed.
    fn word_layout_html(&self, opts: &HtmlOptions) -> Option<HtmlOutput> {
        let Source::Word(_, _, data) = &self.file else {
            return None;
        };
        match opts.mode {
            Mode::Friendly => None,
            Mode::Medium => {
                let doc = word::fmt::read(data, self.word_format()?).ok()?;
                Some(HtmlOutput {
                    html: crate::layout::html::to_html(&doc),
                    files: Vec::new(),
                })
            }
            Mode::Hard | Mode::Hard95 => {
                let laid = self.laid()?;
                Some(render(&laid.analysis, Some(&laid.pdf), opts))
            }
        }
    }

    /// The document laid out in pages as Word lays it out, as a PDF: only
    /// for a Word document, an EPUB book or a web page (a PDF already has
    /// its pages). A book or a page is written as a Word document first,
    /// which is laid out.
    ///
    /// # Errors
    /// When the paper is a PDF, or the layout cannot be written.
    pub fn to_pdf(&self) -> Result<Vec<u8>> {
        match &self.file {
            Source::Pdf(_) => anyhow::bail!("the input is already a PDF"),
            Source::Book(..) => {
                let docx = self.to_docx(&self.analyze(), &DocxOptions::default());
                Paper::from_bytes(docx)?.to_pdf()
            }
            Source::Word(_, format, data) => {
                let format = match format {
                    InputFormat::Doc => WordFormat::Doc,
                    _ => WordFormat::Docx,
                };
                let doc = word::fmt::read(data, format)?;
                crate::layout::to_pdf(&doc)
            }
        }
    }

    /// Records where the PDF came from (file name, arXiv id, ...); it ends up
    /// in [`Meta::source`].
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    /// Sets how hard the PNG pictures of every output are compressed
    /// ([`PngCompression::Balanced`] by default). The pixels stay the same;
    /// [`PngCompression::Fast`] draws the pictures faster for larger files.
    /// Pictures with many colours are stored as JPEG when that is much
    /// smaller than their PNG, so a few more of them may be with `Fast`.
    #[must_use]
    pub fn with_png_compression(mut self, compression: PngCompression) -> Self {
        if let Source::Pdf(pdf) = &mut self.file {
            pdf.set_png_compression(compression);
        }
        self
    }

    /// Number of pages; 0 for a Word document or a book, which have none.
    pub fn page_count(&self) -> usize {
        self.pdf().map_or(0, PdfFile::page_count)
    }

    /// Runs content extraction and layout analysis with default options.
    pub fn analyze(&self) -> Analysis {
        self.analyze_with(AnalyzeOptions::default())
    }

    /// Runs content extraction and layout analysis.
    ///
    /// Pages are read, and their lines built, on `opts.jobs` threads; the
    /// result does not depend on the number of threads.
    /// A Word document or a book needs no analysis: its structure, read
    /// when it was loaded, is returned (`opts` does not apply to it).
    pub fn analyze_with(&self, opts: AnalyzeOptions) -> Analysis {
        let pdf = match &self.file {
            Source::Pdf(pdf) => pdf,
            Source::Word(doc, _, _) | Source::Book(doc, _) => {
                let mut doc = doc.as_ref().clone();
                doc.meta.source = self.source.clone();
                return Analysis {
                    doc,
                    loose: Vec::new(),
                    rotated: Vec::new(),
                };
            }
        };
        let (raw, fonts) = pdf.extract(opts.jobs);
        let meta = Meta {
            source: self.source.clone(),
            ..Default::default()
        };
        let mut analysis = analyze(raw, &fonts, meta, opts);
        // A cover that is a picture holds no title text: the document
        // information of the PDF names the document then.
        if analysis.doc.meta.title.is_none() {
            analysis.doc.meta.title = pdf.info_title();
        }
        analysis
    }

    /// A renderer for pictures of pages and page regions, or `None` for a
    /// Word document, which has no pages. Not thread-safe: every thread
    /// that draws needs its own.
    pub fn renderer(&self) -> Option<PageRenderer<'_>> {
        self.pdf().map(PdfFile::renderer)
    }

    /// Renders an analysis to HTML in the requested mode. `analysis` must
    /// come from this paper, since images are drawn from its PDF. A Word
    /// document is first laid out as Word lays it out: `hard` and `hard-95`
    /// show its pages, `medium` its formatting in one continuous page
    /// (neither shows text edited with [`Paper::apply_content`]); when the
    /// layout fails, they give the `friendly` page.
    pub fn to_html(&self, analysis: &Analysis, opts: &HtmlOptions) -> HtmlOutput {
        if let Some(output) = self.word_layout_html(opts) {
            return output;
        }
        render(analysis, self.pdf(), &self.html_options(opts))
    }

    /// `opts`, with the friendly mode for a document without pages.
    fn html_options(&self, opts: &HtmlOptions) -> HtmlOptions {
        let mut opts = opts.clone();
        if !self.format().has_pages() {
            opts.mode = Mode::Friendly;
        }
        opts
    }

    /// Writes an analysis as HTML to `out`, like [`Paper::to_html`], but
    /// without holding the whole page in memory: the pictures are drawn a
    /// few at a time and written as soon as they are ready, which matters
    /// for long documents (all the page pictures of `hard` mode). When
    /// pictures are not inlined, each picture file is given to `file` as
    /// (path relative to the HTML file, contents) instead of being
    /// returned. The bytes written are those of [`HtmlOutput::html`].
    ///
    /// # Errors
    /// The first error of `out` or `file`.
    pub fn write_html(
        &self,
        analysis: &Analysis,
        opts: &HtmlOptions,
        out: &mut dyn std::io::Write,
        file: &mut dyn FnMut(&str, &[u8]) -> std::io::Result<()>,
    ) -> std::io::Result<()> {
        if self.word_format().is_some() && opts.mode != Mode::Friendly {
            if opts.mode == Mode::Medium {
                if let Some(output) = self.word_layout_html(opts) {
                    return out.write_all(output.html.as_bytes());
                }
            } else if let Some(laid) = self.laid() {
                return render_to(&laid.analysis, Some(&laid.pdf), opts, out, &mut |name, data| {
                    file(&name, &data)
                });
            }
        }
        render_to(
            analysis,
            self.pdf(),
            &self.html_options(opts),
            out,
            &mut |name, data| file(&name, &data),
        )
    }

    /// Writes an analysis as a Word document (`.docx`) and returns the
    /// bytes of the file. `analysis` must come from this paper, since the
    /// pictures are drawn from its PDF.
    pub fn to_docx(&self, analysis: &Analysis, opts: &DocxOptions) -> Vec<u8> {
        render_docx(analysis, self.pdf(), opts)
    }

    /// Writes an analysis as a Word document, like [`Paper::to_docx`], and
    /// also returns each picture of the document as a file of its own in
    /// `image_dir` (a relative path, such as `images`): the pictures stay
    /// embedded in the document too.
    pub fn to_docx_with_images(&self, analysis: &Analysis, opts: &DocxOptions, image_dir: &str) -> DocxOutput {
        render_docx_with_images(analysis, self.pdf(), opts, image_dir)
    }

    /// Writes an analysis as Markdown. `analysis` must come from this paper,
    /// since the pictures are drawn from its PDF.
    pub fn to_markdown(&self, analysis: &Analysis, opts: &MarkdownOptions) -> MarkdownOutput {
        render_markdown(analysis, self.pdf(), opts)
    }

    /// Writes an analysis as a LaTeX document (`.tex`) with its picture
    /// files. `analysis` must come from this paper, since the pictures are
    /// drawn from its PDF.
    pub fn to_latex(&self, analysis: &Analysis, opts: &LatexOptions) -> LatexOutput {
        render_latex(analysis, self.pdf(), opts)
    }

    /// Writes an analysis as a Typst document (`.typ`) with its picture
    /// files, in the layout of an article or of a printed book. `analysis`
    /// must come from this paper, since the pictures are drawn from its PDF.
    pub fn to_typst(&self, analysis: &Analysis, opts: &TypstOptions) -> TypstOutput {
        render_typst(analysis, self.pdf(), opts)
    }

    /// Writes an analysis as an EPUB book (`.epub`) and returns the bytes of
    /// the file. `analysis` must come from this paper, since the pictures
    /// are drawn from its PDF.
    pub fn to_epub(&self, analysis: &Analysis, opts: &EpubOptions) -> Vec<u8> {
        render_epub(analysis, self.pdf(), opts)
    }

    /// Writes an analysis as JSON: the content of the document, block by
    /// block, with its text in a form that can be edited and applied back
    /// with [`Paper::apply_content`]. `analysis` must come from this paper,
    /// since the pictures are drawn from its PDF.
    pub fn to_json(&self, analysis: &Analysis, opts: &JsonOptions) -> JsonOutput {
        render_json(analysis, self.pdf(), opts, &self.source_info())
    }

    /// Cuts an analysis into chunks for retrieval (RAG): pieces of at most
    /// `opts.max_tokens` tokens that follow the sections of the document,
    /// each headed by its section path, with the blocks and pages it comes
    /// from. Write them as JSON Lines with [`chunks_jsonl`](crate::chunks_jsonl).
    pub fn to_chunks(&self, analysis: &Analysis, opts: &ChunkOptions) -> Vec<Chunk> {
        render_chunks(&analysis.doc, opts)
    }

    /// Applies an edited JSON file (written by [`Paper::to_json`] for this
    /// same PDF) to `analysis`: the edited text replaces the text of its
    /// blocks, and every output written from `analysis` afterwards shows
    /// it. Text that was not edited is left as it is. The hard mode, whose
    /// text layer follows the lines of the PDF, has no text for the edited
    /// blocks.
    ///
    /// # Errors
    /// When `json` is not a kakemono JSON file, was made from another PDF,
    /// or names blocks this analysis does not have (it was made with other
    /// analysis options or another version of kakemono). Problems with the
    /// text of single blocks are not errors: those blocks keep their text,
    /// and [`ContentReport::warnings`] says why.
    pub fn apply_content(&self, analysis: &mut Analysis, json: &str) -> Result<ContentReport> {
        self.apply_content_from(analysis, json, None)
    }

    /// [`Paper::apply_content`] for a JSON file in the folder `base`: a
    /// figure's `replace_image` that is a relative path is read from there.
    ///
    /// # Errors
    /// As [`Paper::apply_content`].
    pub fn apply_content_in(&self, analysis: &mut Analysis, json: &str, base: &Path) -> Result<ContentReport> {
        self.apply_content_from(analysis, json, Some(base))
    }

    /// Applies `json` (relative `replace_image` paths read from `base`),
    /// then draws again the figures whose text was edited
    /// (`figure_lines`): their new picture replaces the old one.
    fn apply_content_from(&self, analysis: &mut Analysis, json: &str, base: Option<&Path>) -> Result<ContentReport> {
        let (mut report, edits) = apply_content(analysis, json, &self.source_info(), base)?;
        let Some(renderer) = self.renderer() else {
            return Ok(report);
        };
        for edit in edits {
            let block = &analysis.doc.blocks[edit.block_idx];
            let Some(svg) = crate::render::figure_svg(&renderer, block, &edit, &mut report.warnings) else {
                report.warnings.push(format!(
                    "block {}: the figure could not be drawn again with its edited text",
                    block.id
                ));
                continue;
            };
            analysis.doc.pictures.push(crate::model::Picture {
                data: svg.into_bytes(),
                jpeg: false,
            });
            let index = analysis.doc.pictures.len() - 1;
            if let crate::model::BlockKind::Figure { image: Some(spec), .. } =
                &mut analysis.doc.blocks[edit.block_idx].kind
            {
                spec.embedded = Some(index);
            }
        }
        Ok(report)
    }

    /// The source as the JSON output names it.
    fn source_info(&self) -> SourceInfo {
        SourceInfo {
            format: self.format().name(),
            name: self.source.clone(),
            sha256: self.sha256.clone(),
        }
    }
}

/// The SHA-256 of `data`, in hexadecimal.
fn sha256_hex(data: &[u8]) -> String {
    Sha256::digest(data).iter().map(|byte| format!("{byte:02x}")).collect()
}

/// One-shot conversion: loads the PDF, analyses it and renders it.
///
/// # Errors
/// When the data is not a readable PDF, or the PDF has no pages.
pub fn convert(data: Vec<u8>, opts: &HtmlOptions) -> Result<HtmlOutput> {
    let paper = Paper::from_bytes(data)?;
    let analysis = paper.analyze();
    Ok(paper.to_html(&analysis, opts))
}