markkit 0.1.1

Extract clean markdown from any format – HTML, PDF, DOCX, and more
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
//! Programmatic fixture builders for office-document tests.
//!
//! Fixtures are generated in-memory (zip + minimal OOXML/ODF/BIFF8) instead of
//! committing binaries, so each test documents exactly the structure it exercises.

#![allow(dead_code)]

use std::io::{Cursor, Write};

use zip::CompressionMethod;
use zip::write::{SimpleFileOptions, ZipWriter};

/// Build a ZIP archive from `(name, bytes)` entries (deflated).
pub fn zip_bytes(entries: &[(&str, &[u8])]) -> Vec<u8> {
    zip_bytes_with(entries, CompressionMethod::Deflated)
}

pub fn zip_bytes_with(entries: &[(&str, &[u8])], method: CompressionMethod) -> Vec<u8> {
    let mut w = ZipWriter::new(Cursor::new(Vec::new()));
    for (name, data) in entries {
        let opts = if *name == "mimetype" {
            SimpleFileOptions::default().compression_method(CompressionMethod::Stored)
        } else {
            SimpleFileOptions::default()
                .compression_method(method)
                .large_file(data.len() > u32::MAX as usize / 2)
        };
        w.start_file(*name, opts).unwrap();
        w.write_all(data).unwrap();
    }
    w.finish().unwrap().into_inner()
}

// ─── DOCX ───────────────────────────────────────────────────────────

pub const W_NS: &str = r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships""#;

const DOCX_CONTENT_TYPES: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>"#;

const DOCX_ROOT_RELS: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
</Relationships>"#;

/// Optional DOCX parts.
#[derive(Default)]
pub struct DocxParts<'a> {
    /// `<w:body>` inner XML.
    pub body: &'a str,
    /// Extra `<Relationship>` elements for `word/_rels/document.xml.rels`.
    pub rels: &'a str,
    /// Inner XML of `<w:styles>`.
    pub styles: Option<&'a str>,
    /// Inner XML of `<w:numbering>`.
    pub numbering: Option<&'a str>,
    /// `dc:title` in `docProps/core.xml`.
    pub title: Option<&'a str>,
    /// Additional raw entries.
    pub extra: Vec<(&'a str, Vec<u8>)>,
}

pub fn docx(body: &str) -> Vec<u8> {
    docx_with(DocxParts {
        body,
        ..Default::default()
    })
}

pub fn docx_with(p: DocxParts<'_>) -> Vec<u8> {
    let document = format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document {W_NS}><w:body>{}</w:body></w:document>"#,
        p.body
    );
    let rels = format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{}</Relationships>"#,
        p.rels
    );
    let mut entries: Vec<(String, Vec<u8>)> = vec![
        ("[Content_Types].xml".into(), DOCX_CONTENT_TYPES.into()),
        ("_rels/.rels".into(), DOCX_ROOT_RELS.into()),
        ("word/document.xml".into(), document.into_bytes()),
        ("word/_rels/document.xml.rels".into(), rels.into_bytes()),
    ];
    if let Some(styles) = p.styles {
        entries.push((
            "word/styles.xml".into(),
            format!(r#"<?xml version="1.0"?><w:styles {W_NS}>{styles}</w:styles>"#).into_bytes(),
        ));
    }
    if let Some(numbering) = p.numbering {
        entries.push((
            "word/numbering.xml".into(),
            format!(r#"<?xml version="1.0"?><w:numbering {W_NS}>{numbering}</w:numbering>"#)
                .into_bytes(),
        ));
    }
    if let Some(title) = p.title {
        entries.push((
            "docProps/core.xml".into(),
            format!(
                r#"<?xml version="1.0"?><cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>{title}</dc:title></cp:coreProperties>"#
            )
            .into_bytes(),
        ));
    }
    for (name, data) in p.extra {
        entries.push((name.to_string(), data));
    }
    let refs: Vec<(&str, &[u8])> = entries
        .iter()
        .map(|(n, d)| (n.as_str(), d.as_slice()))
        .collect();
    zip_bytes(&refs)
}

/// `<w:p>` with a single plain run.
pub fn para(text: &str) -> String {
    format!("<w:p><w:r><w:t xml:space=\"preserve\">{text}</w:t></w:r></w:p>")
}

/// `<w:p>` with a paragraph style.
pub fn styled_para(style: &str, text: &str) -> String {
    format!("<w:p><w:pPr><w:pStyle w:val=\"{style}\"/></w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>")
}

/// `<w:p>` that is a list item (numId / ilvl).
pub fn list_para(num_id: u32, ilvl: u32, text: &str) -> String {
    format!(
        "<w:p><w:pPr><w:pStyle w:val=\"ListParagraph\"/><w:numPr><w:ilvl w:val=\"{ilvl}\"/><w:numId w:val=\"{num_id}\"/></w:numPr></w:pPr><w:r><w:t>{text}</w:t></w:r></w:p>"
    )
}

/// numbering.xml: numId 1 = bullets, numId 2 = decimal.
pub const NUMBERING: &str = r#"
<w:abstractNum w:abstractNumId="0">
  <w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="bullet"/></w:lvl>
  <w:lvl w:ilvl="1"><w:start w:val="1"/><w:numFmt w:val="bullet"/></w:lvl>
</w:abstractNum>
<w:abstractNum w:abstractNumId="1">
  <w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/></w:lvl>
  <w:lvl w:ilvl="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/></w:lvl>
</w:abstractNum>
<w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>
<w:num w:numId="2"><w:abstractNumId w:val="1"/></w:num>
"#;

// ─── XLSX ───────────────────────────────────────────────────────────

/// A cell value for generated sheets.
#[derive(Clone, Debug)]
pub enum Cell {
    S(&'static str),
    Str(String),
    N(f64),
    B(bool),
    Empty,
}

pub struct Sheet {
    pub name: String,
    pub rows: Vec<Vec<Cell>>,
    pub hidden: bool,
}

impl Sheet {
    pub fn new(name: &str, rows: Vec<Vec<Cell>>) -> Self {
        Self {
            name: name.into(),
            rows,
            hidden: false,
        }
    }
}

pub fn col_name(mut c: usize) -> String {
    let mut s = Vec::new();
    loop {
        s.push(b'A' + (c % 26) as u8);
        if c < 26 {
            break;
        }
        c = c / 26 - 1;
    }
    s.reverse();
    String::from_utf8(s).unwrap()
}

pub fn xml_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

fn cell_xml(r: usize, c: usize, cell: &Cell) -> String {
    let at = format!("{}{}", col_name(c), r + 1);
    match cell {
        Cell::S(s) => format!(
            r#"<c r="{at}" t="inlineStr"><is><t xml:space="preserve">{}</t></is></c>"#,
            xml_escape(s)
        ),
        Cell::Str(s) => format!(
            r#"<c r="{at}" t="inlineStr"><is><t xml:space="preserve">{}</t></is></c>"#,
            xml_escape(s)
        ),
        Cell::N(n) => format!(r#"<c r="{at}"><v>{n}</v></c>"#),
        Cell::B(b) => format!(r#"<c r="{at}" t="b"><v>{}</v></c>"#, u8::from(*b)),
        Cell::Empty => String::new(),
    }
}

/// Raw worksheet XML from `<sheetData>` inner XML.
pub fn worksheet_xml(sheet_data: &str) -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheetData>{sheet_data}</sheetData></worksheet>"#
    )
}

pub fn sheet_data(rows: &[Vec<Cell>]) -> String {
    let mut out = String::new();
    for (r, row) in rows.iter().enumerate() {
        out.push_str(&format!(r#"<row r="{}">"#, r + 1));
        for (c, cell) in row.iter().enumerate() {
            out.push_str(&cell_xml(r, c, cell));
        }
        out.push_str("</row>");
    }
    out
}

/// Build an XLSX from sheets, plus extra raw entries.
pub fn xlsx_with(
    sheets: &[Sheet],
    raw_sheet_xml: &[Option<String>],
    extra: &[(&str, &[u8])],
) -> Vec<u8> {
    let mut ct = String::from(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>"#,
    );
    let mut wb_sheets = String::new();
    let mut wb_rels = String::new();
    let mut entries: Vec<(String, Vec<u8>)> = Vec::new();
    for (i, s) in sheets.iter().enumerate() {
        let n = i + 1;
        ct.push_str(&format!(r#"<Override PartName="/xl/worksheets/sheet{n}.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>"#));
        let state = if s.hidden { r#" state="hidden""# } else { "" };
        wb_sheets.push_str(&format!(
            r#"<sheet name="{}" sheetId="{n}"{state} r:id="rId{n}"/>"#,
            xml_escape(&s.name)
        ));
        wb_rels.push_str(&format!(r#"<Relationship Id="rId{n}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet{n}.xml"/>"#));
        let xml = raw_sheet_xml
            .get(i)
            .cloned()
            .flatten()
            .unwrap_or_else(|| worksheet_xml(&sheet_data(&s.rows)));
        entries.push((format!("xl/worksheets/sheet{n}.xml"), xml.into_bytes()));
    }
    ct.push_str("</Types>");
    let workbook = format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets>{wb_sheets}</sheets></workbook>"#
    );
    let rels = format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{wb_rels}</Relationships>"#
    );
    let root_rels = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#;
    let mut all: Vec<(String, Vec<u8>)> = vec![
        ("[Content_Types].xml".into(), ct.into_bytes()),
        ("_rels/.rels".into(), root_rels.as_bytes().to_vec()),
        ("xl/workbook.xml".into(), workbook.into_bytes()),
        ("xl/_rels/workbook.xml.rels".into(), rels.into_bytes()),
    ];
    all.extend(entries);
    for (n, d) in extra {
        all.push((n.to_string(), d.to_vec()));
    }
    let refs: Vec<(&str, &[u8])> = all
        .iter()
        .map(|(n, d)| (n.as_str(), d.as_slice()))
        .collect();
    zip_bytes(&refs)
}

pub fn xlsx(sheets: &[Sheet]) -> Vec<u8> {
    xlsx_with(sheets, &[], &[])
}

// ─── ODS ────────────────────────────────────────────────────────────

pub const ODS_MIME: &str = "application/vnd.oasis.opendocument.spreadsheet";

/// Build an ODS from `<office:spreadsheet>` inner XML.
pub fn ods(spreadsheet_inner: &str) -> Vec<u8> {
    let content = format!(
        r#"<?xml version="1.0" encoding="UTF-8"?><office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2"><office:body><office:spreadsheet>{spreadsheet_inner}</office:spreadsheet></office:body></office:document-content>"#
    );
    let manifest = r#"<?xml version="1.0" encoding="UTF-8"?><manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"><manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/><manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/></manifest:manifest>"#;
    zip_bytes(&[
        ("mimetype", ODS_MIME.as_bytes()),
        ("content.xml", content.as_bytes()),
        ("META-INF/manifest.xml", manifest.as_bytes()),
    ])
}

pub fn ods_string_cell(s: &str) -> String {
    format!(
        r#"<table:table-cell office:value-type="string"><text:p>{}</text:p></table:table-cell>"#,
        xml_escape(s)
    )
}

pub fn ods_float_cell(v: f64) -> String {
    format!(
        r#"<table:table-cell office:value-type="float" office:value="{v}"><text:p>{v}</text:p></table:table-cell>"#
    )
}

// ─── XLS (BIFF8 in an OLE2 compound file) ───────────────────────────

fn record(out: &mut Vec<u8>, typ: u16, data: &[u8]) {
    out.extend_from_slice(&typ.to_le_bytes());
    out.extend_from_slice(&(data.len() as u16).to_le_bytes());
    out.extend_from_slice(data);
}

fn bof(dt: u16) -> Vec<u8> {
    let mut d = Vec::new();
    d.extend_from_slice(&0x0600u16.to_le_bytes()); // BIFF8
    d.extend_from_slice(&dt.to_le_bytes());
    d.extend_from_slice(&0x0DBBu16.to_le_bytes());
    d.extend_from_slice(&0x07CCu16.to_le_bytes());
    d.extend_from_slice(&0u32.to_le_bytes());
    d.extend_from_slice(&0x0006u32.to_le_bytes());
    d
}

/// XLS cell for generated workbooks.
pub enum XlsCell {
    Num(u16, u16, f64),
    Label(u16, u16, &'static str),
}

/// A raw BIFF8 sheet: optional DIMENSIONS (rowFirst, rowLastPlus1, colFirst, colLastPlus1) and cells.
pub struct XlsSheet {
    pub name: &'static str,
    pub dims: Option<(u32, u32, u16, u16)>,
    pub cells: Vec<XlsCell>,
}

/// Build the BIFF8 `Workbook` stream.
pub fn biff8_workbook(sheets: &[XlsSheet]) -> Vec<u8> {
    // Globals: BOF, CODEPAGE, BOUNDSHEET*, EOF. BOUNDSHEET offsets patched afterwards.
    let mut globals = Vec::new();
    record(&mut globals, 0x0809, &bof(0x0005));
    record(&mut globals, 0x0042, &1200u16.to_le_bytes());
    let mut patch_at = Vec::new();
    for s in sheets {
        let mut d = Vec::new();
        d.extend_from_slice(&0u32.to_le_bytes()); // placeholder
        d.push(0); // visible
        d.push(0); // worksheet
        d.push(s.name.len() as u8);
        d.push(0); // compressed 8-bit chars
        d.extend_from_slice(s.name.as_bytes());
        patch_at.push(globals.len() + 4);
        record(&mut globals, 0x0085, &d);
    }
    record(&mut globals, 0x000A, &[]);

    let mut stream = globals;
    for (i, s) in sheets.iter().enumerate() {
        let pos = stream.len() as u32;
        stream[patch_at[i]..patch_at[i] + 4].copy_from_slice(&pos.to_le_bytes());
        record(&mut stream, 0x0809, &bof(0x0010));
        if let Some((rf, rl, cf, cl)) = s.dims {
            let mut d = Vec::new();
            d.extend_from_slice(&rf.to_le_bytes());
            d.extend_from_slice(&rl.to_le_bytes());
            d.extend_from_slice(&cf.to_le_bytes());
            d.extend_from_slice(&cl.to_le_bytes());
            d.extend_from_slice(&0u16.to_le_bytes());
            record(&mut stream, 0x0200, &d);
        }
        for c in &s.cells {
            match c {
                XlsCell::Num(r, col, v) => {
                    let mut d = Vec::new();
                    d.extend_from_slice(&r.to_le_bytes());
                    d.extend_from_slice(&col.to_le_bytes());
                    d.extend_from_slice(&0u16.to_le_bytes());
                    d.extend_from_slice(&v.to_le_bytes());
                    record(&mut stream, 0x0203, &d);
                }
                XlsCell::Label(r, col, text) => {
                    let mut d = Vec::new();
                    d.extend_from_slice(&r.to_le_bytes());
                    d.extend_from_slice(&col.to_le_bytes());
                    d.extend_from_slice(&0u16.to_le_bytes());
                    d.extend_from_slice(&(text.len() as u16).to_le_bytes());
                    d.push(0);
                    d.extend_from_slice(text.as_bytes());
                    record(&mut stream, 0x0204, &d);
                }
            }
        }
        record(&mut stream, 0x000A, &[]);
    }
    stream
}

/// Wrap a workbook stream into an OLE2 compound file.
pub fn xls_from_stream(stream: &[u8]) -> Vec<u8> {
    let mut comp = cfb::CompoundFile::create(Cursor::new(Vec::new())).unwrap();
    {
        let mut s = comp.create_stream("/Workbook").unwrap();
        s.write_all(stream).unwrap();
    }
    comp.flush().unwrap();
    comp.into_inner().into_inner()
}

pub fn xls(sheets: &[XlsSheet]) -> Vec<u8> {
    xls_from_stream(&biff8_workbook(sheets))
}