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

// ── generate_section_xml: structural tests ────────────────────────────

#[test]
fn section_xml_empty_section_produces_valid_wrapper() {
    let xml = section_xml(vec![]);
    assert!(xml.contains("<hs:sec"), "root element missing: {xml}");
    assert!(xml.contains("</hs:sec>"), "closing tag missing: {xml}");
    assert!(xml.contains(r#"xmlns:hs="http://www.hancom.co.kr/hwpml/2011/section""#));
}

#[test]
fn section_xml_paragraph_plain_text() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("hello world")],
    }]);
    assert!(xml.contains("<hp:p "), "paragraph open: {xml}");
    assert!(xml.contains("</hp:p>"), "paragraph close: {xml}");
    assert!(xml.contains("<hp:t>"), "text run open: {xml}");
    assert!(xml.contains("hello world"), "text content: {xml}");
}

#[test]
fn section_xml_empty_paragraph() {
    let xml = section_xml(vec![Block::Paragraph { inlines: vec![] }]);
    assert!(xml.contains("<hp:p "));
    assert!(xml.contains("</hp:p>"));
}

#[test]
fn section_xml_heading_level_1() {
    let xml = section_xml(vec![Block::Heading {
        level: 1,
        inlines: vec![inline("Title")],
    }]);
    // Heading 1 → numeric styleIDRef="1" (matches hh:styles table id=1)
    assert!(
        xml.contains(r#"hp:styleIDRef="1""#),
        "h1 style ref must be numeric 1: {xml}"
    );
    assert!(xml.contains("Title"));
}

#[test]
fn section_xml_heading_level_6() {
    let xml = section_xml(vec![Block::Heading {
        level: 6,
        inlines: vec![inline("Deep")],
    }]);
    // Heading 6 → numeric styleIDRef="6"
    assert!(xml.contains(r#"hp:styleIDRef="6""#), "{xml}");
    assert!(xml.contains("Deep"));
}

#[test]
fn section_xml_bold_inline_has_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_inline("strong")],
    }]);
    // Bold formatting is emitted as an inline <hp:charPr bold="true"/> inside
    // the <hp:run> (section-level charPr), in addition to the header charPr
    // table entry referenced by charPrIDRef.
    assert!(
        xml.contains("<hp:charPr "),
        "inline charPr must be emitted for bold: {xml}"
    );
    assert!(
        xml.contains(r#"bold="true""#),
        "bold attribute must appear on inline charPr: {xml}"
    );
    assert!(xml.contains("charPrIDRef="), "charPrIDRef: {xml}");
    assert!(xml.contains("strong"));
}

#[test]
fn section_xml_italic_inline_has_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![italic_inline("em")],
    }]);
    assert!(xml.contains("charPrIDRef="), "{xml}");
}

#[test]
fn section_xml_underline_inline_has_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![underline_inline("ul")],
    }]);
    assert!(xml.contains("charPrIDRef="), "{xml}");
}

#[test]
fn section_xml_strikethrough_inline_has_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "del".into(),
            strikethrough: true,
            ..Inline::default()
        }],
    }]);
    assert!(xml.contains("charPrIDRef="), "{xml}");
}

#[test]
fn section_xml_plain_inline_has_no_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("plain")],
    }]);
    // Plain text → no inline hp:charPr element (formatting is only in header table)
    assert!(
        !xml.contains("<hp:charPr"),
        "unexpected inline charPr: {xml}"
    );
}

#[test]
fn section_xml_plain_inline_has_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("plain")],
    }]);
    // Plain text → charPrIDRef="0" on the run element
    assert!(
        xml.contains(r#"charPrIDRef="0""#),
        "charPrIDRef missing: {xml}"
    );
}

#[test]
fn section_xml_bold_inline_has_nonzero_charpr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_inline("bold")],
    }]);
    // Bold → charPrIDRef pointing to id=1 (first non-default entry)
    assert!(xml.contains("charPrIDRef="), "charPrIDRef missing: {xml}");
}

#[test]
fn section_xml_paragraph_has_para_pr_id_ref() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("text")],
    }]);
    assert!(
        xml.contains(r#"paraPrIDRef="0""#),
        "paraPrIDRef missing: {xml}"
    );
}

#[test]
fn section_xml_nested_inlines_bold_then_italic() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_inline("B"), italic_inline("I")],
    }]);
    // Each formatting variant gets a distinct charPrIDRef; bold and italic
    // runs are separate so they must have different IDs (both non-zero).
    assert!(xml.contains("charPrIDRef="), "{xml}");
    assert!(xml.contains('B'));
    assert!(xml.contains('I'));
}

#[test]
fn section_xml_image_block() {
    let xml = section_xml(vec![Block::Image {
        src: "image001.png".into(),
        alt: "a cat".into(),
    }]);
    assert!(xml.contains("<hp:p "), "{xml}");
    assert!(
        xml.contains(r#"hp:binaryItemIDRef="image001.png""#),
        "{xml}"
    );
    assert!(xml.contains(r#"alt="a cat""#), "{xml}");
    assert!(xml.contains("<hp:img"), "{xml}");
    // 6-A: image must be wrapped in <hp:run><hp:pic>...</hp:pic></hp:run>
    assert!(
        xml.contains(r#"<hp:run charPrIDRef="0">"#),
        "image run wrapper missing: {xml}"
    );
    assert!(xml.contains("<hp:pic>"), "hp:pic wrapper missing: {xml}");
    assert!(xml.contains("</hp:pic>"), "hp:pic close missing: {xml}");
    // Verify nesting order: run → pic → img
    let run_pos = xml
        .find(r#"<hp:run charPrIDRef="0">"#)
        .expect("run position");
    let pic_pos = xml.find("<hp:pic>").expect("pic position");
    let img_pos = xml.find("<hp:img").expect("img position");
    assert!(run_pos < pic_pos, "run must precede pic: {xml}");
    assert!(pic_pos < img_pos, "pic must precede img: {xml}");
}

#[test]
fn section_xml_table_2x2() {
    let cell = |text: &str| TableCell {
        blocks: vec![Block::Paragraph {
            inlines: vec![inline(text)],
        }],
        colspan: 1,
        rowspan: 1,
    };
    let xml = section_xml(vec![Block::Table {
        col_count: 2,
        rows: vec![
            TableRow {
                cells: vec![cell("A"), cell("B")],
                is_header: false,
            },
            TableRow {
                cells: vec![cell("C"), cell("D")],
                is_header: false,
            },
        ],
        inner_margin: None,
    }]);
    // 5-B: table must be wrapped in a paragraph container
    assert!(xml.contains("<hp:p "), "p wrapper: {xml}");
    assert!(
        xml.contains(r#"<hp:run charPrIDRef="0">"#),
        "run wrapper: {xml}"
    );
    // 5-C: tbl must carry rowCnt, colCnt, borderFillIDRef and noAdjust
    assert!(
        xml.contains(r#"rowCnt="2""#),
        "tbl must have rowCnt=2: {xml}"
    );
    assert!(
        xml.contains(r#"colCnt="2""#),
        "tbl must have colCnt=2: {xml}"
    );
    assert!(
        xml.contains(r#"borderFillIDRef="2""#),
        "tbl must have borderFillIDRef=2: {xml}"
    );
    assert!(
        xml.contains(r#"noAdjust="0""#),
        "tbl must have noAdjust=0: {xml}"
    );
    assert!(xml.contains("</hp:tbl>"), "tbl close: {xml}");
    assert_eq!(xml.matches("<hp:tr>").count(), 2, "two rows: {xml}");
    assert_eq!(xml.matches("<hp:tc>").count(), 4, "four cells: {xml}");
    assert!(xml.contains('A'), "{xml}");
    assert!(xml.contains('D'), "{xml}");
}

#[test]
fn section_xml_table_colspan_rowspan_present() {
    // After the fix, the writer emits <hp:cellAddr colSpan="…" rowSpan="…"/>
    // inside <hp:tc> when either value differs from 1.
    let wide_cell = TableCell {
        blocks: vec![],
        colspan: 2,
        rowspan: 1,
    };
    let xml = section_xml(vec![Block::Table {
        col_count: 2,
        rows: vec![TableRow {
            cells: vec![wide_cell],
            is_header: false,
        }],
        inner_margin: None,
    }]);
    assert!(xml.contains("<hp:tc>"), "plain tc emitted: {xml}");
    assert!(
        xml.contains("colSpan=\"2\""),
        "colSpan should appear: {xml}"
    );
    assert!(
        xml.contains("rowSpan=\"1\""),
        "rowSpan should appear: {xml}"
    );
    assert!(xml.contains("<hp:cellAddr"), "cellAddr element: {xml}");
}

#[test]
fn section_xml_table_no_celladdr_for_1x1() {
    // The OWPML-compliant writer always emits <hp:cellAddr> and <hp:cellSpan>
    // for every cell — including 1×1 cells — so that validators and readers
    // can always find the position and span information without special-casing.
    let cell = TableCell {
        blocks: vec![Block::Paragraph {
            inlines: vec![inline("normal")],
        }],
        colspan: 1,
        rowspan: 1,
    };
    let xml = section_xml(vec![Block::Table {
        col_count: 1,
        rows: vec![TableRow {
            cells: vec![cell],
            is_header: false,
        }],
        inner_margin: None,
    }]);
    assert!(xml.contains("<hp:tc>"), "tc emitted: {xml}");
    assert!(
        xml.contains("<hp:cellAddr "),
        "cellAddr must appear for all cells: {xml}"
    );
    assert!(
        xml.contains("<hp:cellSpan "),
        "cellSpan must appear for all cells: {xml}"
    );
    // For a single 1×1 cell the position is col=0, row=0.
    assert!(
        xml.contains(r#"colAddr="0" rowAddr="0""#),
        "1x1 cell must be at colAddr=0 rowAddr=0: {xml}"
    );
    // Span values must be 1×1.
    assert!(
        xml.contains(r#"colSpan="1" rowSpan="1""#),
        "1x1 cell must have colSpan=1 rowSpan=1: {xml}"
    );
}

#[test]
fn section_xml_math_block() {
    let xml = section_xml(vec![Block::Math {
        display: true,
        tex: r"E = mc^2".into(),
    }]);
    assert!(xml.contains("<hp:equation>"), "equation open: {xml}");
    assert!(xml.contains("</hp:equation>"), "equation close: {xml}");
    assert!(xml.contains(r"E = mc^2"), "{xml}");
    // 6-B: equation must be wrapped in <hp:run charPrIDRef="0">
    assert!(
        xml.contains(r#"<hp:run charPrIDRef="0">"#),
        "equation run wrapper missing: {xml}"
    );
    // Verify nesting order: run → equation
    let run_pos = xml
        .find(r#"<hp:run charPrIDRef="0">"#)
        .expect("run position");
    let eq_pos = xml.find("<hp:equation>").expect("equation position");
    assert!(run_pos < eq_pos, "run must precede equation: {xml}");
}

#[test]
fn section_xml_ordered_list() {
    let xml = section_xml(vec![Block::List {
        ordered: true,
        start: 1,
        items: vec![
            ListItem {
                blocks: vec![Block::Paragraph {
                    inlines: vec![inline("first")],
                }],
                children: vec![],
                checked: None,
            },
            ListItem {
                blocks: vec![Block::Paragraph {
                    inlines: vec![inline("second")],
                }],
                children: vec![],
                checked: None,
            },
        ],
    }]);
    assert!(xml.contains("first"), "{xml}");
    assert!(xml.contains("second"), "{xml}");
    assert_eq!(xml.matches("<hp:p ").count(), 2, "{xml}");
}

#[test]
fn section_xml_unordered_list() {
    let xml = section_xml(vec![Block::List {
        ordered: false,
        start: 1,
        items: vec![ListItem {
            blocks: vec![Block::Paragraph {
                inlines: vec![inline("bullet")],
            }],
            children: vec![],
            checked: None,
        }],
    }]);
    assert!(xml.contains("bullet"), "{xml}");
}

#[test]
fn section_xml_footnote_block() {
    let xml = section_xml(vec![Block::Footnote {
        id: "fn1".into(),
        content: vec![Block::Paragraph {
            inlines: vec![inline("footnote text")],
        }],
    }]);
    assert!(xml.contains("footnote text"), "{xml}");
}

#[test]
fn section_xml_blockquote() {
    let xml = section_xml(vec![Block::BlockQuote {
        blocks: vec![Block::Paragraph {
            inlines: vec![inline("quoted")],
        }],
    }]);
    assert!(xml.contains("quoted"), "{xml}");
    assert!(xml.contains("<hp:p "), "{xml}");
    // Block-quote paragraphs must reference paraPrIDRef="1" (indented style).
    assert!(
        xml.contains(r#"paraPrIDRef="1""#),
        "blockquote paragraph must use paraPrIDRef=\"1\": {xml}"
    );
    // Must NOT use paraPrIDRef="0" for the quoted paragraph.
    assert!(
        !xml.contains(r#"paraPrIDRef="0""#),
        "blockquote paragraph must NOT use paraPrIDRef=\"0\": {xml}"
    );
}

#[test]
fn section_xml_horizontal_rule() {
    let xml = section_xml(vec![Block::HorizontalRule]);
    assert!(xml.contains("<hp:p "), "{xml}");
    // The writer emits a line of em-dashes as a visual rule.
    assert!(xml.contains("───"), "{xml}");
}

#[test]
fn section_xml_page_break_emits_newpage_ctrl() {
    let xml = section_xml(vec![Block::PageBreak]);
    assert!(
        xml.contains("<hp:p "),
        "page break wrapped in paragraph: {xml}"
    );
    assert!(
        xml.contains("<hp:run"),
        "page break ctrl must live inside a run: {xml}"
    );
    assert!(
        xml.contains(r#"id="newPage""#),
        "page break must emit hp:ctrl id=newPage: {xml}"
    );
    // Must not emit any text run.
    assert!(
        !xml.contains("<hp:t>"),
        "page break paragraph must contain no text node: {xml}"
    );
}

#[test]
fn section_xml_code_block() {
    let xml = section_xml(vec![Block::CodeBlock {
        language: Some("rust".into()),
        code: "fn main() {}".into(),
    }]);
    assert!(xml.contains("<hp:p "), "{xml}");
    assert!(
        !xml.contains(r#"charPrIDRef="code""#),
        "charPrIDRef must not be the string 'code': {xml}"
    );
    assert!(
        xml.contains("charPrIDRef="),
        "charPrIDRef must be present: {xml}"
    );
    assert!(xml.contains("fn main() {}"), "{xml}");
}