text-document-io 1.8.0

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
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
//! Feature tests for the DOCX exporter.
//!
//! Documents are built with the (well-tested) djot importer, then exported via
//! the file-less builder [`document_io_controller::build_docx_document`], and
//! the resulting [`docx_rs::Docx`] structure is asserted directly. This
//! exercises the exact builder used to write `.docx` files without touching the
//! filesystem.

extern crate text_document_io as document_io;

use common::long_operation::{LongOperationManager, OperationStatus};
use document_io::docx_rs::{
    AlignmentType, DocumentChild, Docx, HyperlinkData, Paragraph, ParagraphChild, RunChild,
};
use document_io::{ExportDocxDto, ImportDjotDto, document_io_controller};
use test_harness::{EventHub, setup};

use std::sync::Arc;

/// A document touching every implemented feature, used by the on-disk test.
const RICH_DJOT: &str = "\
# Title

{alignment=center}
Centered intro with a [link](https://example.com).

- bullet one
- bullet two

1. first
2. second

- [x] done task
- [ ] pending task

> a quoted line
>
> > nested quote

```rust
let answer = 42;
```";

// --- harness ---------------------------------------------------------------

fn wait(mgr: &LongOperationManager, op_id: &str) {
    while let Some(OperationStatus::Running) = mgr.get_operation_status(op_id) {
        std::thread::sleep(std::time::Duration::from_millis(2));
    }
}

/// Import `djot` into a fresh document and return the built DOCX model.
fn docx_from_djot(djot: &str) -> Docx {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, djot);
    document_io_controller::build_docx_document(&db, &ExportDocxDto::default())
        .expect("build_docx_document")
}

fn import_djot(db: &test_harness::DbContext, ev: &Arc<EventHub>, djot: &str) {
    let mut mgr = LongOperationManager::new();
    let op = document_io_controller::import_djot(
        db,
        ev,
        &mut mgr,
        &ImportDjotDto {
            djot_text: djot.to_string(),
            options: Default::default(),
        },
    )
    .expect("import_djot");
    wait(&mgr, &op);
    assert_eq!(
        mgr.get_operation_status(&op),
        Some(OperationStatus::Completed),
        "import of {djot:?} did not complete"
    );
}

// --- inspection helpers ----------------------------------------------------

fn paragraphs(docx: &Docx) -> Vec<&Paragraph> {
    docx.document
        .children
        .iter()
        .filter_map(|c| match c {
            DocumentChild::Paragraph(p) => Some(&**p),
            _ => None,
        })
        .collect()
}

fn collect_text(children: &[ParagraphChild], out: &mut String) {
    for child in children {
        match child {
            ParagraphChild::Run(run) => {
                for rc in &run.children {
                    if let RunChild::Text(t) = rc {
                        out.push_str(&t.text);
                    }
                }
            }
            ParagraphChild::Hyperlink(h) => collect_text(&h.children, out),
            _ => {}
        }
    }
}

fn para_text(p: &Paragraph) -> String {
    let mut s = String::new();
    collect_text(&p.children, &mut s);
    s
}

fn alignment(p: &Paragraph) -> Option<&str> {
    p.property.alignment.as_ref().map(|j| j.val.as_str())
}

fn numbering_id(p: &Paragraph) -> Option<usize> {
    p.property
        .numbering_property
        .as_ref()
        .and_then(|np| np.id.as_ref())
        .map(|id| id.id)
}

fn left_indent(p: &Paragraph) -> Option<i32> {
    p.property.indent.as_ref().and_then(|i| i.start)
}

/// First paragraph whose visible text contains `needle`.
fn para_containing<'a>(docx: &'a Docx, needle: &str) -> &'a Paragraph {
    paragraphs(docx)
        .into_iter()
        .find(|p| para_text(p).contains(needle))
        .unwrap_or_else(|| panic!("no paragraph containing {needle:?}"))
}

fn hyperlink_paths(p: &Paragraph) -> Vec<String> {
    p.children
        .iter()
        .filter_map(|c| match c {
            ParagraphChild::Hyperlink(h) => match &h.link {
                HyperlinkData::External { path, .. } => Some(path.clone()),
                HyperlinkData::Anchor { anchor } => Some(anchor.clone()),
            },
            _ => None,
        })
        .collect()
}

// --- alignment -------------------------------------------------------------

#[test]
fn alignment_center_maps_to_jc() {
    let docx = docx_from_djot("{alignment=center}\nCentered paragraph");
    let p = para_containing(&docx, "Centered paragraph");
    assert_eq!(alignment(p), Some("center"));
}

#[test]
fn alignment_all_variants_map() {
    for (attr, expected) in [
        ("left", AlignmentType::Left),
        ("right", AlignmentType::Right),
        ("center", AlignmentType::Center),
        ("justify", AlignmentType::Justified),
    ] {
        let marker = format!("aligned-{attr}");
        let docx = docx_from_djot(&format!("{{alignment={attr}}}\n{marker}"));
        let p = para_containing(&docx, &marker);
        let got = alignment(p).expect("alignment set");
        // docx-rs renders AlignmentType via Display; compare its string form.
        assert_eq!(got, expected.to_string(), "attr={attr}");
    }
}

#[test]
fn no_alignment_leaves_jc_unset() {
    let docx = docx_from_djot("Plain unaligned paragraph");
    let p = para_containing(&docx, "Plain unaligned paragraph");
    assert_eq!(alignment(p), None);
}

// --- hyperlinks ------------------------------------------------------------

#[test]
fn hyperlink_is_emitted_with_destination() {
    let docx = docx_from_djot("See [the site](https://example.com/page) now");
    let p = para_containing(&docx, "the site");
    let paths = hyperlink_paths(p);
    assert_eq!(paths.len(), 1, "exactly one hyperlink");
    assert!(
        paths[0].contains("example.com/page"),
        "href preserved, got {:?}",
        paths[0]
    );
    // The link's visible text is carried inside the hyperlink.
    assert!(para_text(p).contains("the site"));
}

#[test]
fn plain_text_has_no_hyperlink() {
    let docx = docx_from_djot("Just words, no link here");
    let p = para_containing(&docx, "Just words");
    assert!(hyperlink_paths(p).is_empty());
}

// --- code blocks -----------------------------------------------------------

#[test]
fn code_block_uses_monospace_font_and_preserves_text() {
    let docx = docx_from_djot("```rust\nlet x = 41 + 1;\n```");
    let p = para_containing(&docx, "let x = 41 + 1;");
    // Text is preserved verbatim, no markdown fences.
    assert!(!para_text(p).contains("```"));
    assert_eq!(para_text(p), "let x = 41 + 1;");
    // The monospace font is applied; RunFonts fields are private, so assert via
    // the serialized form.
    let json = docx.json();
    assert!(
        json.contains("Courier New"),
        "expected a Courier New run in the document"
    );
}

#[test]
fn code_block_inline_formatting_is_flattened() {
    // Even if the fenced content looks like emphasis, it stays literal.
    let docx = docx_from_djot("```\na * b * c\n```");
    let p = para_containing(&docx, "a * b * c");
    assert_eq!(para_text(p), "a * b * c");
}

// --- lists -----------------------------------------------------------------

#[test]
fn bullet_list_items_carry_numbering() {
    let docx = docx_from_djot("- first\n- second\n- third");
    let items: Vec<&Paragraph> = paragraphs(&docx)
        .into_iter()
        .filter(|p| numbering_id(p).is_some())
        .collect();
    assert_eq!(items.len(), 3, "all three bullets numbered");
    // All share one bullet list => one numbering instance.
    let ids: std::collections::HashSet<usize> =
        items.iter().filter_map(|p| numbering_id(p)).collect();
    assert_eq!(ids.len(), 1, "single bullet list => single numbering id");

    // The numbering definition exists and is a bullet format.
    let id = *ids.iter().next().unwrap();
    assert_numbering_format(&docx, id, "bullet");
}

#[test]
fn ordered_list_uses_decimal_format() {
    let docx = docx_from_djot("1. alpha\n2. beta");
    let id = numbering_id(para_containing(&docx, "alpha")).expect("numbered");
    assert_numbering_format(&docx, id, "decimal");
}

#[test]
fn two_separate_lists_get_independent_numbering() {
    // A paragraph between the lists splits them into two list instances.
    let docx = docx_from_djot("1. one\n2. two\n\nbreak\n\n1. uno\n2. dos");
    let first = numbering_id(para_containing(&docx, "one")).expect("first numbered");
    let second = numbering_id(para_containing(&docx, "uno")).expect("second numbered");
    assert_ne!(
        first, second,
        "distinct lists must use distinct numbering ids so counters restart"
    );
}

#[test]
fn task_items_render_checkbox_glyphs_without_numbering() {
    let docx = docx_from_djot("- [x] done\n- [ ] todo");
    let done = para_containing(&docx, "done");
    let todo = para_containing(&docx, "todo");
    assert!(para_text(done).contains('\u{2612}'), "checked glyph ☒");
    assert!(para_text(todo).contains('\u{2610}'), "unchecked glyph ☐");
    // Task items are indented but not auto-numbered.
    assert_eq!(numbering_id(done), None);
    assert!(left_indent(done).unwrap_or(0) > 0);
}

fn assert_numbering_format(docx: &Docx, numbering_id: usize, expected_format: &str) {
    let num = docx
        .numberings
        .numberings
        .iter()
        .find(|n| n.id == numbering_id)
        .unwrap_or_else(|| panic!("numbering {numbering_id} registered"));
    let abstract_num = docx
        .numberings
        .abstract_nums
        .iter()
        .find(|a| a.id == num.abstract_num_id)
        .expect("abstract numbering registered");
    let level0 = &abstract_num.levels[0];
    assert_eq!(
        level0.format.val, expected_format,
        "numbering {numbering_id} level-0 format"
    );
}

// --- blockquotes -----------------------------------------------------------

#[test]
fn blockquote_paragraph_is_indented() {
    let docx = docx_from_djot("> a quoted line");
    let p = para_containing(&docx, "a quoted line");
    assert_eq!(left_indent(p), Some(720), "one quote level => 720 twips");
}

#[test]
fn nested_blockquote_indents_deeper() {
    let docx = docx_from_djot("> outer quote\n>\n> > inner quote");
    let outer = para_containing(&docx, "outer quote");
    let inner = para_containing(&docx, "inner quote");
    assert_eq!(left_indent(outer), Some(720));
    assert_eq!(
        left_indent(inner),
        Some(1440),
        "two quote levels => 1440 twips"
    );
}

// --- headings & plain ------------------------------------------------------

#[test]
fn heading_levels_use_heading_styles() {
    for level in 1..=6 {
        let hashes = "#".repeat(level);
        let marker = format!("Title{level}");
        let docx = docx_from_djot(&format!("{hashes} {marker}"));
        let p = para_containing(&docx, &marker);
        let style = p.property.style.as_ref().map(|s| s.val.as_str());
        assert_eq!(style, Some(format!("Heading{level}").as_str()));
    }
}

#[test]
fn plain_paragraph_has_no_numbering_indent_or_style() {
    let docx = docx_from_djot("An ordinary paragraph");
    let p = para_containing(&docx, "An ordinary paragraph");
    assert_eq!(numbering_id(p), None);
    assert_eq!(left_indent(p), None);
    assert_eq!(p.property.style, None);
}

// --- inline marks still work ----------------------------------------------

// --- end-to-end pack/unpack ------------------------------------------------

#[test]
fn rich_document_packs_to_a_valid_docx_file() {
    use document_io::docx_rs::read_docx;

    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, RICH_DJOT);

    let dir = std::env::temp_dir();
    let path = dir.join(format!("docx_export_rich_{}.docx", std::process::id()));
    let path_str = path.to_string_lossy().to_string();

    let mut mgr = LongOperationManager::new();
    let op = document_io_controller::export_docx(
        &db,
        &ev,
        &mut mgr,
        &ExportDocxDto {
            output_path: path_str.clone(),
            options: Default::default(),
        },
    )
    .expect("export_docx");
    wait(&mgr, &op);
    assert_eq!(
        mgr.get_operation_status(&op),
        Some(OperationStatus::Completed),
        "export should complete"
    );

    // The packed file exists and is a structurally valid .docx that docx-rs can
    // read back (this exercises the numbering/hyperlink relationship wiring done
    // at `build()`/`pack()` time, which the in-memory builder skips).
    let bytes = std::fs::read(&path).expect("output file exists");
    let parsed = read_docx(&bytes).expect("packed docx must be readable");
    assert!(
        !parsed.document.children.is_empty(),
        "round-tripped document has content"
    );
    assert!(
        !parsed.numberings.numberings.is_empty(),
        "list numbering definitions survive the pack/unpack"
    );

    let _ = std::fs::remove_file(&path);
}

#[test]
fn bold_run_is_marked_bold() {
    let docx = docx_from_djot("normal *bolded* normal");
    let p = para_containing(&docx, "bolded");
    let has_bold = p.children.iter().any(|c| match c {
        ParagraphChild::Run(r) => {
            r.run_property.bold.is_some()
                && r.children
                    .iter()
                    .any(|rc| matches!(rc, RunChild::Text(t) if t.text.contains("bolded")))
        }
        _ => false,
    });
    assert!(has_bold, "the 'bolded' run should be bold");
}

// --- Manuscript / RTL export options (M5) ----------------------------------

use common::parser_tools::DocxExportOptions;

fn docx_from_djot_with_options(djot: &str, options: DocxExportOptions) -> Docx {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, djot);
    document_io_controller::build_docx_document(
        &db,
        &ExportDocxDto {
            output_path: String::new(),
            options,
        },
    )
    .expect("build_docx_document")
}

/// A block tagged `{direction=rtl}` must export with a paragraph-level `<w:bidi/>` — the one
/// bidi primitive docx-rs offers, and the fix for DOCX previously dropping direction entirely.
#[test]
fn rtl_block_exports_paragraph_bidi() {
    let docx = docx_from_djot("{direction=rtl}\nمرحبا بالعالم\n");
    let ps = paragraphs(&docx);
    assert_eq!(ps.len(), 1, "one paragraph");
    assert_eq!(
        ps[0].property.bidi,
        Some(true),
        "an RTL block gets paragraph bidi"
    );
}

/// A plain LTR block stays un-bidi (the default `to_docx` behaviour is untouched).
#[test]
fn ltr_block_has_no_bidi() {
    let docx = docx_from_djot("Hello world\n");
    let ps = paragraphs(&docx);
    assert_eq!(
        ps[0].property.bidi, None,
        "an LTR block is never marked bidi"
    );
}

/// Page geometry from the options lands on the document's section property, and the base font
/// size lands on the document defaults.
#[test]
fn options_apply_page_size_and_font_defaults() {
    let opts = DocxExportOptions {
        page_width_twips: Some(11906), // A4
        page_height_twips: Some(16838),
        font_family: Some("Courier New".to_string()),
        font_half_points: Some(24), // 12pt
        justify: true,
        first_line_indent_twips: Some(720),
        line_spacing_twips: Some(480),
        ..Default::default()
    };
    let docx = docx_from_djot_with_options("The wind rose over the hills.\n", opts);
    // PageSize's w/h fields are private; assert via the crate's own JSON serialization (the
    // same fallback the monospace-font test uses). 11906×16838 twips are the A4 dimensions.
    let json = docx.json();
    assert!(
        json.contains("11906") && json.contains("16838"),
        "A4 page size in section props"
    );

    // The single body paragraph is justified, spaced, and first-line indented.
    let ps = paragraphs(&docx);
    assert_eq!(
        alignment(ps[0]),
        Some("justified"),
        "justify → jc=justified"
    );
    assert!(ps[0].property.line_spacing.is_some(), "line spacing set");
    let ind = ps[0].property.indent.as_ref().expect("indent set");
    assert!(
        matches!(
            ind.special_indent,
            Some(document_io::docx_rs::SpecialIndentType::FirstLine(720))
        ),
        "first-line indent of 720 twips"
    );
}

/// A page-numbered running header is attached when requested.
#[test]
fn page_numbers_attach_a_header() {
    let opts = DocxExportOptions {
        page_numbers: true,
        running_header: Some("Vane / THE LIGHTHOUSE".to_string()),
        ..Default::default()
    };
    let docx = docx_from_djot_with_options("Prose.\n", opts);
    assert!(
        docx.document_rels.header_count > 0,
        "a header relationship was registered"
    );
}