hwp2md 0.4.0

HWP/HWPX ↔ Markdown bidirectional converter
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use super::*;

// ── Roundtrip / integration tests ────────────────────────────────────────
//
// Golden, structural, and blockquote tests have been moved to
// writer_tests_golden.rs to keep each file under the 800-line guideline.
// They are registered in writer_tests.rs as `mod tests_golden`.

// ── helpers ─────────────────────────────────────────────────────────────

/// Build a simple document with one paragraph containing the given inlines.
fn roundtrip_doc(inlines: Vec<Inline>) -> Document {
    Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Paragraph { inlines }],

            page_layout: None,
        }],
        assets: Vec::new(),
    }
}

/// Write a document to HWPX, read it back, and return the first paragraph's inlines.
fn roundtrip_inlines(inlines: Vec<Inline>) -> Vec<Inline> {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = roundtrip_doc(inlines);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");
    let read_back = read_hwpx(tmp.path()).expect("read_hwpx");
    read_back
        .sections
        .into_iter()
        .flat_map(|s| s.blocks)
        .filter_map(|b| match b {
            Block::Paragraph { inlines } => Some(inlines),
            _ => None,
        })
        .next()
        .unwrap_or_default()
}

// ── bold/italic/underline/strike roundtrip ──────────────────────────────

#[test]
fn roundtrip_bold_text_preserved() {
    let result = roundtrip_inlines(vec![bold_inline("bold text")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold text");
    assert!(
        result[0].bold,
        "bold flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_italic_text_preserved() {
    let result = roundtrip_inlines(vec![italic_inline("italic text")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "italic text");
    assert!(
        result[0].italic,
        "italic flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_bold_italic_combined_preserved() {
    let input = Inline {
        text: "bold italic".into(),
        bold: true,
        italic: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold italic");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert!(
        result[0].italic,
        "italic must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_underline_text_preserved() {
    let result = roundtrip_inlines(vec![underline_inline("underlined")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "underlined");
    assert!(
        result[0].underline,
        "underline flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_strikethrough_text_preserved() {
    let input = Inline {
        text: "struck".into(),
        strikethrough: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "struck");
    assert!(
        result[0].strikethrough,
        "strikethrough flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_superscript_text_preserved() {
    let input = Inline {
        text: "sup".into(),
        superscript: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "sup");
    assert!(
        result[0].superscript,
        "superscript flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_subscript_text_preserved() {
    let input = Inline {
        text: "sub".into(),
        subscript: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "sub");
    assert!(
        result[0].subscript,
        "subscript flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_color_text_preserved() {
    let input = Inline {
        text: "red".into(),
        color: Some("#FF0000".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "red");
    assert_eq!(
        result[0].color.as_deref(),
        Some("#FF0000"),
        "color must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_mixed_plain_and_bold_preserved() {
    let result = roundtrip_inlines(vec![inline("normal "), bold_inline("bold")]);
    assert_eq!(result.len(), 2, "expected 2 inlines: {result:?}");
    assert_eq!(result[0].text, "normal ");
    assert!(!result[0].bold, "first inline must not be bold: {result:?}");
    assert_eq!(result[1].text, "bold");
    assert!(result[1].bold, "second inline must be bold: {result:?}");
}

#[test]
fn roundtrip_bold_italic_underline_strike_color_combined() {
    let input = Inline {
        text: "all".into(),
        bold: true,
        italic: true,
        underline: true,
        strikethrough: true,
        color: Some("#00FF00".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    let r = &result[0];
    assert!(r.bold, "bold: {result:?}");
    assert!(r.italic, "italic: {result:?}");
    assert!(r.underline, "underline: {result:?}");
    assert!(r.strikethrough, "strikethrough: {result:?}");
    assert_eq!(r.color.as_deref(), Some("#00FF00"), "color: {result:?}");
}

// ── section XML inline charPr emission ──────────────────────────────────

#[test]
fn section_xml_bold_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"bold="true""#),
        "section XML must contain inline charPr with bold=\"true\": {xml}"
    );
}

#[test]
fn section_xml_italic_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![italic_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"italic="true""#),
        "section XML must contain inline charPr with italic=\"true\": {xml}"
    );
}

#[test]
fn section_xml_underline_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![underline_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"underline="true""#),
        "section XML must contain inline charPr with underline=\"true\": {xml}"
    );
}

#[test]
fn section_xml_strikethrough_inline_emits_strikeout() {
    let input = Inline {
        text: "hello".into(),
        strikethrough: true,
        ..Inline::default()
    };
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    assert!(
        xml.contains(r#"strikeout="true""#),
        "section XML must contain inline charPr with strikeout=\"true\": {xml}"
    );
}

#[test]
fn section_xml_color_inline_emits_color_without_hash() {
    let input = Inline {
        text: "hello".into(),
        color: Some("#FF0000".into()),
        ..Inline::default()
    };
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    assert!(
        xml.contains(r#"color="FF0000""#),
        "section XML must contain inline charPr with color=\"FF0000\" (no hash): {xml}"
    );
    assert!(
        !xml.contains(r##"color="#FF0000""##),
        "color must not have leading # in OWPML: {xml}"
    );
}

#[test]
fn section_xml_plain_inline_no_charpr_element() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("plain")],
    }]);
    // Inside the <hp:run> there should be NO <hp:charPr> for plain text.
    // However the section XML itself may contain "hp:charPr" in other contexts
    // (e.g. attribute references), so we check specifically for the element pattern.
    assert!(
        !xml.contains("<hp:charPr "),
        "plain inline must NOT emit inline <hp:charPr> element: {xml}"
    );
}

// ── font_name roundtrip ────────────────────────────────────────────────

#[test]
fn roundtrip_font_name_preserved() {
    let input = Inline {
        text: "styled".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "styled");
    assert_eq!(
        result[0].font_name.as_deref(),
        Some("Malgun Gothic"),
        "font_name must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_font_name_with_bold_preserved() {
    let input = Inline {
        text: "bold styled".into(),
        bold: true,
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold styled");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert_eq!(
        result[0].font_name.as_deref(),
        Some("Malgun Gothic"),
        "font_name must survive roundtrip with bold: {result:?}"
    );
}

#[test]
fn section_xml_font_name_emits_face_name_id_ref() {
    let input = Inline {
        text: "hello".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    let tables = RefTables::build(&doc);
    let sec = &doc.sections[0];
    let xml = generate_section_xml(sec, 0, &tables, &ImageAssetMap::new())
        .expect("generate_section_xml failed");

    assert!(
        xml.contains("faceNameIDRef="),
        "section XML must contain faceNameIDRef for font_name inline: {xml}"
    );

    // Verify the specific index value: "Malgun Gothic" is the second font
    // registered (index 1) because DEFAULT_FONT ("바탕") occupies index 0.
    let expected_idx = tables
        .font_names
        .iter()
        .position(|f| f == "Malgun Gothic")
        .expect("Malgun Gothic must be in font_names");
    assert_eq!(
        expected_idx, 1,
        "Malgun Gothic should be at index 1 (바탕 is 0): {:?}",
        tables.font_names
    );
    let expected_attr = format!("faceNameIDRef=\"{expected_idx}\"");
    assert!(
        xml.contains(&expected_attr),
        "section XML must contain {expected_attr}: {xml}"
    );
}

#[test]
fn header_xml_font_name_registered_in_fontface() {
    let input = Inline {
        text: "hello".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    let tables = RefTables::build(&doc);
    let header =
        super::header::generate_header_xml(&doc, &tables).expect("generate_header_xml failed");
    assert!(
        header.contains("Malgun Gothic"),
        "header XML must contain the registered font name: {header}"
    );
}

// ── default font / edge case roundtrip ──────────────────────────────────

/// An inline with NO font_name set (default "batang" at index 0) must
/// roundtrip with font_name remaining None.  The writer does not emit
/// faceNameIDRef for the default font, and the reader leaves font_name
/// as None when no faceNameIDRef attribute is present.
#[test]
fn roundtrip_default_font_no_font_name_preserved() {
    let input = Inline {
        text: "default font text".into(),
        bold: true,
        ..Inline::default()
    };
    assert!(
        input.font_name.is_none(),
        "precondition: input must have no font_name"
    );

    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "default font text");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert!(
        result[0].font_name.is_none(),
        "font_name must remain None for default font after roundtrip: {result:?}"
    );
}

/// When an inline carries a font_name that is NOT registered in the fontface
/// table, write_inline_charpr silently omits the faceNameIDRef attribute.
/// On read-back the font_name will therefore be None.
///
/// This scenario cannot happen through normal document construction (since
/// RefTables::build calls collect_from_inlines which registers every font),
/// but the test documents the expected graceful degradation if it somehow did.
#[test]
fn roundtrip_unknown_font_name_not_in_table() {
    // Build a document with one inline that has a known font.
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "known font".into(),
            font_name: Some("Malgun Gothic".into()),
            ..Inline::default()
        }],
    }]);
    let tables = RefTables::build(&doc);

    // Verify that the unknown font is indeed absent from the table.
    assert!(
        !tables.font_names.iter().any(|f| f == "Comic Sans MS"),
        "precondition: Comic Sans MS must not be in font_names: {:?}",
        tables.font_names
    );

    // Manually construct a section with an inline referencing a font NOT in the table.
    let rogue_section = Section {
        blocks: vec![Block::Paragraph {
            inlines: vec![Inline {
                text: "rogue".into(),
                font_name: Some("Comic Sans MS".into()),
                ..Inline::default()
            }],
        }],

        page_layout: None,
    };
    let xml = generate_section_xml(&rogue_section, 0, &tables, &ImageAssetMap::new())
        .expect("generate_section_xml failed");

    // The writer should NOT emit faceNameIDRef for a font not in the table.
    assert!(
        !xml.contains("faceNameIDRef="),
        "unknown font must not produce faceNameIDRef in section XML: {xml}"
    );

    // Still emits the charPr element (because font_name is Some, triggering
    // the has_formatting check), but without the faceNameIDRef attribute.
    assert!(
        xml.contains("<hp:charPr"),
        "charPr element should still be emitted for the font_name inline: {xml}"
    );
}

// ── image roundtrip ─────────────────────────────────────────────────────

#[test]
fn write_hwpx_image_roundtrip_preserves_asset() {
    let png_bytes = vec![0x89u8, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");

    let original = Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Image {
                src: "photo.png".into(),
                alt: "a photo".into(),
            }],

            page_layout: None,
        }],
        assets: vec![Asset {
            name: "photo.png".into(),
            data: png_bytes.clone(),
            mime_type: "image/png".into(),
        }],
    };
    write_hwpx(&original, tmp.path(), None).expect("write");

    let read_back = read_hwpx(tmp.path()).expect("read_hwpx");

    let has_image = read_back
        .sections
        .iter()
        .flat_map(|s| &s.blocks)
        .any(|b| matches!(b, Block::Image { src, .. } if src.contains("photo")));
    assert!(
        has_image,
        "image block must survive HWPX roundtrip; sections: {:?}",
        read_back.sections
    );

    assert_eq!(
        read_back.assets.len(),
        1,
        "one asset expected after roundtrip"
    );
    assert_eq!(
        read_back.assets[0].data, png_bytes,
        "asset binary content must be preserved through roundtrip"
    );
    assert_eq!(
        read_back.assets[0].mime_type, "image/png",
        "asset MIME type must be preserved"
    );
}

// ── Phase B-4: page layout roundtrip tests ───────────────────────────────

#[test]
fn page_layout_survives_hwpx_roundtrip() {
    use crate::ir::PageLayout;

    // Construct a document with a custom page layout.
    let layout = PageLayout {
        width: Some(59528),
        height: Some(84188),
        landscape: false,
        margin_left: Some(2835),
        margin_right: Some(2835),
        margin_top: Some(2835),
        margin_bottom: Some(2835),
    };

    let original = Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Paragraph {
                inlines: vec![Inline::plain("roundtrip text")],
            }],
            page_layout: Some(layout.clone()),
        }],
        assets: Vec::new(),
    };

    let tmp = tempfile::NamedTempFile::with_suffix(".hwpx").unwrap();
    write_hwpx(&original, tmp.path(), None).expect("write_hwpx failed");

    let read_back = read_hwpx(tmp.path()).expect("read_hwpx failed");

    assert_eq!(
        read_back.sections.len(),
        1,
        "one section must survive roundtrip"
    );
    let read_layout = read_back.sections[0]
        .page_layout
        .as_ref()
        .expect("page_layout must survive HWPX roundtrip");

    assert_eq!(read_layout.width, layout.width, "width must roundtrip");
    assert_eq!(read_layout.height, layout.height, "height must roundtrip");
    assert_eq!(
        read_layout.landscape, layout.landscape,
        "landscape must roundtrip"
    );
    assert_eq!(
        read_layout.margin_left, layout.margin_left,
        "margin_left must roundtrip"
    );
    assert_eq!(
        read_layout.margin_right, layout.margin_right,
        "margin_right must roundtrip"
    );
    assert_eq!(
        read_layout.margin_top, layout.margin_top,
        "margin_top must roundtrip"
    );
    assert_eq!(
        read_layout.margin_bottom, layout.margin_bottom,
        "margin_bottom must roundtrip"
    );
}

#[test]
fn default_page_layout_roundtrip_produces_a4_values() {
    // A section with no page_layout (None) should be written with A4 defaults
    // and read back with a4_portrait values.
    use crate::ir::PageLayout;

    let original = Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Paragraph {
                inlines: vec![Inline::plain("default layout")],
            }],
            page_layout: None,
        }],
        assets: Vec::new(),
    };

    let tmp = tempfile::NamedTempFile::with_suffix(".hwpx").unwrap();
    write_hwpx(&original, tmp.path(), None).expect("write_hwpx failed");

    let read_back = read_hwpx(tmp.path()).expect("read_hwpx failed");

    assert_eq!(read_back.sections.len(), 1, "one section must survive");

    // The reader should parse the A4 defaults written by the writer.
    let read_layout = read_back.sections[0]
        .page_layout
        .as_ref()
        .expect("page_layout must be present after roundtrip of default layout");

    let expected = PageLayout::a4_portrait();
    assert_eq!(read_layout.width, expected.width, "width roundtrip");
    assert_eq!(read_layout.height, expected.height, "height roundtrip");
    assert_eq!(
        read_layout.landscape, expected.landscape,
        "landscape roundtrip"
    );
    assert_eq!(
        read_layout.margin_left, expected.margin_left,
        "margin_left roundtrip"
    );
    assert_eq!(
        read_layout.margin_right, expected.margin_right,
        "margin_right roundtrip"
    );
    assert_eq!(
        read_layout.margin_top, expected.margin_top,
        "margin_top roundtrip"
    );
    assert_eq!(
        read_layout.margin_bottom, expected.margin_bottom,
        "margin_bottom roundtrip"
    );
}