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
use super::*;

// ── header.xml reference table tests ─────────────────────────────────────

#[test]
fn header_xml_contains_char_properties() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![bold_inline("hello")],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("hh:charProperties"),
        "charProperties section: {content}"
    );
    assert!(content.contains("hh:charPr"), "charPr entry: {content}");
    // OWPML schema does not allow bold/italic/underline/strikeout as charPr
    // attributes; bold formatting is expressed through distinct charPr IDs.
    assert!(
        !content.contains(r#"bold="true""#),
        "bold must NOT appear as a charPr attribute (schema violation): {content}"
    );
    // The bold inline must produce a non-default (id != 0) charPr entry.
    assert!(
        content.contains(r#"id="1""#),
        "bold inline must produce a charPr with id=1: {content}"
    );
}

#[test]
fn header_xml_contains_para_properties() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("hh:paraProperties"),
        "paraProperties section: {content}"
    );
    assert!(content.contains("hh:paraPr"), "paraPr entry: {content}");
}

#[test]
fn header_xml_contains_font_faces() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("hh:fontfaces"),
        "fontfaces section: {content}"
    );
    assert!(content.contains("바탕"), "default Batang font: {content}");
}

#[test]
fn header_xml_default_charpr_has_id_zero() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(content.contains(r#"id="0""#), "id=0 charPr: {content}");
}

// ── Phase 7 tests: borderFill table ─────────────────────────────────────

#[test]
fn header_xml_contains_border_fills_section() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("hh:borderFills"),
        "borderFills section must be present: {content}"
    );
    assert!(
        content.contains("hh:borderFill"),
        "borderFill entry must be present: {content}"
    );
}

#[test]
fn header_xml_border_fill_has_default_entry() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    // borderFill id=1 with required attributes.
    assert!(
        content.contains(r#"id="1""#),
        "borderFill id=1 must exist: {content}"
    );
    assert!(
        content.contains(r#"threeD="false""#),
        "threeD attribute: {content}"
    );
    assert!(
        content.contains(r#"shadow="false""#),
        "shadow attribute: {content}"
    );
    // Border elements must be present.
    assert!(content.contains("hh:leftBorder"), "leftBorder: {content}");
    assert!(content.contains("hh:rightBorder"), "rightBorder: {content}");
    assert!(content.contains("hh:topBorder"), "topBorder: {content}");
    assert!(
        content.contains("hh:bottomBorder"),
        "bottomBorder: {content}"
    );
    assert!(content.contains("hh:diagonal"), "diagonal: {content}");
    // Slash and backSlash elements must be present.
    assert!(content.contains("hh:slash"), "slash element: {content}");
    assert!(
        content.contains("hh:backSlash"),
        "backSlash element: {content}"
    );
}

#[test]
fn header_xml_charpr_has_border_fill_id_ref() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![inline("text")],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    // Every charPr entry must have borderFillIDRef="1".
    assert!(
        content.contains(r#"borderFillIDRef="1""#),
        "charPr must reference borderFill id=1: {content}"
    );
    // Must NOT reference borderFillIDRef="0" (nonexistent entry).
    assert!(
        !content.contains(r#"borderFillIDRef="0""#),
        "borderFillIDRef=0 must not appear (no such entry): {content}"
    );
}

#[test]
fn header_xml_border_fills_precedes_char_properties() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    let bf_pos = content
        .find("hh:borderFills")
        .expect("borderFills must exist");
    let cp_pos = content
        .find("hh:charProperties")
        .expect("charProperties must exist");
    assert!(
        bf_pos < cp_pos,
        "borderFills must appear before charProperties in header.xml"
    );
}

// ── Phase 4 tests: styles ────────────────────────────────────────────────

#[test]
fn header_xml_contains_styles() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = Document::new();
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("hh:styles"),
        "hh:styles section must be present: {content}"
    );
    assert!(
        content.contains("hh:style"),
        "hh:style entries must be present: {content}"
    );
    // Style id=0 (Normal) and id=1..6 (Heading1..6) must all appear.
    for id in 0..=6u8 {
        assert!(
            content.contains(&format!(r#"id="{id}""#)),
            "style id={id} must be present: {content}"
        );
    }
    // Verify known style names.
    assert!(
        content.contains(r#"name="Normal""#),
        "Normal style: {content}"
    );
    assert!(
        content.contains(r#"name="Heading1""#),
        "Heading1 style: {content}"
    );
    assert!(
        content.contains(r#"name="Heading6""#),
        "Heading6 style: {content}"
    );
}

// ── Phase 8 tests: CharPrKey unit tests ──────────────────────────────────

#[test]
fn charpr_key_from_inline_code_sets_code_true_and_monospace_font() {
    let inline_val = Inline {
        text: "x".into(),
        code: true,
        ..Inline::default()
    };
    let key = CharPrKey::from_inline(&inline_val);
    assert!(key.code, "code flag must be true");
    assert_eq!(
        key.font_name.as_deref(),
        Some("Courier New"),
        "code inline must use monospace font"
    );
}

#[test]
fn charpr_key_from_inline_code_overrides_custom_font() {
    // Even if the inline has a font_name, code=true forces Courier New.
    let inline_val = Inline {
        text: "x".into(),
        code: true,
        font_name: Some("Arial".into()),
        ..Inline::default()
    };
    let key = CharPrKey::from_inline(&inline_val);
    assert_eq!(
        key.font_name.as_deref(),
        Some("Courier New"),
        "code flag must override custom font"
    );
}

#[test]
fn charpr_key_plain_has_code_false() {
    let key = CharPrKey::plain();
    assert!(!key.code, "plain key must not be code");
}

#[test]
fn charpr_key_code_block_has_code_true() {
    let key = CharPrKey::code_block();
    assert!(key.code, "code_block key must have code=true");
    assert_eq!(key.font_name.as_deref(), Some("Courier New"));
}

#[test]
fn inline_code_gets_distinct_charpr_id_from_plain() {
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![
            inline("normal"),
            Inline {
                text: "code".into(),
                code: true,
                ..Inline::default()
            },
        ],
    }]);
    let tables = RefTables::build(&doc);

    let plain_id = tables.char_pr_id(&CharPrKey::plain());
    let code_key = CharPrKey::from_inline(&Inline {
        text: "code".into(),
        code: true,
        ..Inline::default()
    });
    let code_id = tables.char_pr_id(&code_key);

    assert_ne!(
        plain_id, code_id,
        "inline code must have a different charPr ID than plain text"
    );
}

#[test]
fn header_xml_inline_code_registers_courier_new_font() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "code".into(),
            code: true,
            ..Inline::default()
        }],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains("Courier New"),
        "Courier New font must be in header for inline code: {content}"
    );
}

// ── Phase 9 tests: superscript/subscript ──────────────────────────────────

#[test]
fn charpr_key_superscript_gets_distinct_id_from_plain() {
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![
            inline("normal"),
            Inline {
                text: "sup".into(),
                superscript: true,
                ..Inline::default()
            },
        ],
    }]);
    let tables = RefTables::build(&doc);

    let plain_id = tables.char_pr_id(&CharPrKey::plain());
    let sup_key = CharPrKey::from_inline(&Inline {
        text: "sup".into(),
        superscript: true,
        ..Inline::default()
    });
    let sup_id = tables.char_pr_id(&sup_key);

    assert_ne!(
        plain_id, sup_id,
        "superscript must have a different charPr ID than plain text"
    );
}

#[test]
fn charpr_key_subscript_gets_distinct_id_from_plain() {
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![
            inline("normal"),
            Inline {
                text: "sub".into(),
                subscript: true,
                ..Inline::default()
            },
        ],
    }]);
    let tables = RefTables::build(&doc);

    let plain_id = tables.char_pr_id(&CharPrKey::plain());
    let sub_key = CharPrKey::from_inline(&Inline {
        text: "sub".into(),
        subscript: true,
        ..Inline::default()
    });
    let sub_id = tables.char_pr_id(&sub_key);

    assert_ne!(
        plain_id, sub_id,
        "subscript must have a different charPr ID than plain text"
    );
}

#[test]
fn section_xml_superscript_inline_gets_correct_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![
            inline("text "),
            Inline {
                text: "sup".into(),
                superscript: true,
                ..Inline::default()
            },
        ],
    }]);

    // Must have at least two runs with different charPrIDRef values.
    let marker = "charPrIDRef=\"";
    let ids: Vec<&str> = xml
        .match_indices(marker)
        .map(|(pos, _)| {
            let rest = &xml[pos + marker.len()..];
            let end = rest.find('"').unwrap();
            &rest[..end]
        })
        .collect();
    assert!(
        ids.len() >= 2,
        "must have at least 2 charPrIDRef values: {ids:?}"
    );
    let has_nonzero = ids.iter().any(|&id| id != "0");
    assert!(
        has_nonzero,
        "superscript inline must produce a non-zero charPrIDRef: {ids:?}"
    );
}

#[test]
fn header_xml_charpr_has_supscript_superscript_attribute() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "sup".into(),
            superscript: true,
            ..Inline::default()
        }],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains(r#"supscript="superscript""#),
        "charPr must contain supscript=\"superscript\" attribute: {content}"
    );
}

#[test]
fn header_xml_charpr_has_supscript_subscript_attribute() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "sub".into(),
            subscript: true,
            ..Inline::default()
        }],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains(r#"supscript="subscript""#),
        "charPr must contain supscript=\"subscript\" attribute: {content}"
    );
}

#[test]
fn header_xml_plain_charpr_has_no_supscript_attribute() {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![inline("plain text only")],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    // Plain text charPr (id=0) must NOT have a supscript attribute.
    // However heading charPrs are also present, and they also lack supscript.
    // We verify that no charPr has supscript when no inline requests it.
    assert!(
        !content.contains("supscript="),
        "plain doc must not contain any supscript attribute: {content}"
    );
}

#[test]
fn bold_superscript_combo_gets_unique_charpr_and_supscript_attribute() {
    // An inline with both bold=true AND superscript=true must receive a
    // charPrIDRef that is not 0 (distinct from the plain entry), and the
    // header.xml must contain a charPr with supscript="superscript".
    let bold_sup_inline = Inline {
        text: "X".into(),
        bold: true,
        superscript: true,
        ..Inline::default()
    };

    // Verify charPrIDRef is non-zero via section XML.
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_sup_inline.clone()],
    }]);
    let marker = "charPrIDRef=\"";
    let start = xml.find(marker).expect("charPrIDRef must be present");
    let rest = &xml[start + marker.len()..];
    let end = rest.find('"').expect("closing quote");
    let value = &rest[..end];
    assert_ne!(
        value, "0",
        "bold+superscript charPrIDRef must not be 0 (plain): {xml}"
    );

    // Verify header.xml carries the supscript attribute via write_hwpx.
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![bold_sup_inline],
    }]);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    let file = std::fs::File::open(tmp.path()).expect("open");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");
    let mut entry = archive.by_name("Contents/header.xml").expect("header.xml");
    let mut content = String::new();
    entry.read_to_string(&mut content).expect("read");

    assert!(
        content.contains(r#"supscript="superscript""#),
        "header must contain supscript=\"superscript\" for bold+superscript inline: {content}"
    );
}