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
//! Tests for HWP binary list detection and grouping.
//!
//! Tests cover:
//! 1. `detect_list_kind` — per-paragraph classification
//! 2. `detect_list_kind_from_text` (via `detect_list_kind`) — text heuristics
//! 3. `hwp_to_ir` integration — grouping of consecutive list paragraphs

use super::{detect_list_kind, hwp_to_ir, ListKind};
use crate::hwp::model::{DocInfo, FileHeader, HwpDocument, HwpParagraph, HwpSection, ParaShape};
use crate::ir;
use std::collections::HashMap;

// -- helpers ------------------------------------------------------------------

fn make_para(text: &str) -> HwpParagraph {
    HwpParagraph {
        text: text.to_string(),
        char_shape_ids: Vec::new(),
        para_shape_id: 0,
        controls: Vec::new(),
        raw_para_text: None,
    }
}

fn make_para_with_ps(text: &str, ps_id: u16) -> HwpParagraph {
    HwpParagraph {
        text: text.to_string(),
        char_shape_ids: Vec::new(),
        para_shape_id: ps_id,
        controls: Vec::new(),
        raw_para_text: None,
    }
}

fn doc_info_empty() -> DocInfo {
    DocInfo::default()
}

/// `DocInfo` with `ParaShape[0].numbering_id = Some(1)`.
fn doc_info_with_numbering() -> DocInfo {
    let mut di = DocInfo::default();
    di.para_shapes.push(ParaShape {
        numbering_id: Some(1),
        ..Default::default()
    });
    di
}

fn make_hwp_doc(paras: Vec<HwpParagraph>) -> HwpDocument {
    HwpDocument {
        header: FileHeader::default(),
        doc_info: DocInfo::default(),
        sections: vec![HwpSection { paragraphs: paras }],
        bin_data: HashMap::new(),
        summary_title: None,
        summary_author: None,
        summary_subject: None,
        summary_keywords: Vec::new(),
    }
}

fn make_hwp_doc_with_di(doc_info: DocInfo, paras: Vec<HwpParagraph>) -> HwpDocument {
    HwpDocument {
        header: FileHeader::default(),
        doc_info,
        sections: vec![HwpSection { paragraphs: paras }],
        bin_data: HashMap::new(),
        summary_title: None,
        summary_author: None,
        summary_subject: None,
        summary_keywords: Vec::new(),
    }
}

// -- detect_list_kind: bullet characters (Tier 2 heuristic) ------------------

#[test]
fn detect_list_kind_bullet_circle_is_unordered() {
    let para = make_para("● First item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

#[test]
fn detect_list_kind_bullet_square_is_unordered() {
    let para = make_para("■ Second item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

#[test]
fn detect_list_kind_bullet_triangle_is_unordered() {
    let para = make_para("▶ Arrow item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

#[test]
fn detect_list_kind_bullet_dot_is_unordered() {
    let para = make_para("• Dot item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

#[test]
fn detect_list_kind_bullet_dash_is_unordered() {
    let para = make_para("- Dash item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

#[test]
fn detect_list_kind_bullet_star_is_unordered() {
    let para = make_para("* Star item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Unordered)
    );
}

// -- detect_list_kind: numbered prefixes (Tier 2 heuristic) ------------------

#[test]
fn detect_list_kind_number_dot_space_is_ordered() {
    let para = make_para("1. First item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Ordered)
    );
}

#[test]
fn detect_list_kind_number_paren_space_is_ordered() {
    let para = make_para("2) Second item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Ordered)
    );
}

#[test]
fn detect_list_kind_alpha_dot_space_is_ordered() {
    let para = make_para("a. Alpha item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Ordered)
    );
}

#[test]
fn detect_list_kind_alpha_paren_space_is_ordered() {
    let para = make_para("b) Beta item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Ordered)
    );
}

#[test]
fn detect_list_kind_three_digit_number_is_ordered() {
    let para = make_para("100. Centesimal item");
    assert_eq!(
        detect_list_kind(&para, &doc_info_empty()),
        Some(ListKind::Ordered)
    );
}

// -- detect_list_kind: plain paragraph -- no detection -----------------------

#[test]
fn detect_list_kind_plain_paragraph_returns_none() {
    let para = make_para("This is a normal paragraph.");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

#[test]
fn detect_list_kind_empty_paragraph_returns_none() {
    let para = make_para("");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

#[test]
fn detect_list_kind_whitespace_only_returns_none() {
    let para = make_para("   ");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

// -- edge cases: year-like patterns must NOT match ----------------------------

#[test]
fn detect_list_kind_year_number_is_not_ordered() {
    // 4-digit number followed by a non-separator character must NOT match.
    let para = make_para("2026 business plan");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

#[test]
fn detect_list_kind_four_digit_no_separator_is_not_ordered() {
    let para = make_para("1234 abcd");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

#[test]
fn detect_list_kind_number_without_space_after_dot_is_not_ordered() {
    // "1.Second" -- no space after dot.
    let para = make_para("1.Second");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

#[test]
fn detect_list_kind_bullet_without_trailing_space_is_not_list() {
    // Bullet directly followed by text, no separator space.
    let para = make_para("●text without space");
    assert_eq!(detect_list_kind(&para, &doc_info_empty()), None);
}

// -- Tier 1: binary numbering_id ---------------------------------------------

#[test]
fn detect_list_kind_numbering_id_with_bullet_text_is_unordered() {
    let di = doc_info_with_numbering();
    let para = make_para_with_ps("● Formal bullet", 0);
    assert_eq!(detect_list_kind(&para, &di), Some(ListKind::Unordered));
}

#[test]
fn detect_list_kind_numbering_id_with_ordered_text_is_ordered() {
    let di = doc_info_with_numbering();
    let para = make_para_with_ps("1. Formal numbered", 0);
    assert_eq!(detect_list_kind(&para, &di), Some(ListKind::Ordered));
}

#[test]
fn detect_list_kind_numbering_id_with_plain_text_defaults_to_unordered() {
    // numbering_id set but text has no recognisable prefix.
    let di = doc_info_with_numbering();
    let para = make_para_with_ps("Plain text in a formal list", 0);
    assert_eq!(detect_list_kind(&para, &di), Some(ListKind::Unordered));
}

#[test]
fn detect_list_kind_no_numbering_id_plain_text_returns_none() {
    let di = doc_info_with_numbering();
    // para_shape_id = 1 but doc_info only has one shape at index 0.
    let para = make_para_with_ps("Regular paragraph", 1);
    assert_eq!(detect_list_kind(&para, &di), None);
}

// -- hwp_to_ir: grouping consecutive list paragraphs -------------------------

#[test]
fn hwp_to_ir_bullet_paragraph_produces_list_block() {
    let hwp = make_hwp_doc(vec![make_para("- Single bullet")]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    assert!(
        matches!(
            doc.sections[0].blocks[0],
            ir::Block::List { ordered: false, .. }
        ),
        "expected unordered Block::List, got {:?}",
        doc.sections[0].blocks[0]
    );
}

#[test]
fn hwp_to_ir_numbered_paragraph_produces_ordered_list_block() {
    let hwp = make_hwp_doc(vec![make_para("1. Single item")]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    assert!(
        matches!(
            doc.sections[0].blocks[0],
            ir::Block::List { ordered: true, .. }
        ),
        "expected ordered Block::List, got {:?}",
        doc.sections[0].blocks[0]
    );
}

#[test]
fn hwp_to_ir_consecutive_bullets_produce_single_list() {
    let hwp = make_hwp_doc(vec![
        make_para("- Item one"),
        make_para("- Item two"),
        make_para("- Item three"),
    ]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(
        doc.sections[0].blocks.len(),
        1,
        "three consecutive bullet paragraphs must collapse into one Block::List"
    );
    if let ir::Block::List { ordered, items, .. } = &doc.sections[0].blocks[0] {
        assert!(!ordered, "must be unordered");
        assert_eq!(items.len(), 3);
    } else {
        panic!("expected Block::List");
    }
}

#[test]
fn hwp_to_ir_consecutive_numbered_produce_single_list() {
    let hwp = make_hwp_doc(vec![
        make_para("1. First"),
        make_para("2. Second"),
        make_para("3. Third"),
    ]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    if let ir::Block::List { ordered, items, .. } = &doc.sections[0].blocks[0] {
        assert!(*ordered, "must be ordered");
        assert_eq!(items.len(), 3);
    } else {
        panic!("expected Block::List");
    }
}

#[test]
fn hwp_to_ir_mixed_bullet_numbered_produces_two_lists() {
    let hwp = make_hwp_doc(vec![
        make_para("- Bullet A"),
        make_para("- Bullet B"),
        make_para("1. Number one"),
        make_para("2. Number two"),
    ]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(
        doc.sections[0].blocks.len(),
        2,
        "bullet list then numbered list must produce 2 Block::List values"
    );
    assert!(matches!(
        doc.sections[0].blocks[0],
        ir::Block::List { ordered: false, .. }
    ));
    assert!(matches!(
        doc.sections[0].blocks[1],
        ir::Block::List { ordered: true, .. }
    ));
}

#[test]
fn hwp_to_ir_plain_paragraph_between_lists_produces_three_blocks() {
    let hwp = make_hwp_doc(vec![
        make_para("- Bullet A"),
        make_para("- Bullet B"),
        make_para("Regular paragraph here."),
        make_para("- Bullet C"),
        make_para("- Bullet D"),
    ]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 3);
    assert!(matches!(doc.sections[0].blocks[0], ir::Block::List { .. }));
    assert!(matches!(
        doc.sections[0].blocks[1],
        ir::Block::Paragraph { .. }
    ));
    assert!(matches!(doc.sections[0].blocks[2], ir::Block::List { .. }));
}

#[test]
fn hwp_to_ir_regular_paragraph_not_detected_as_list() {
    let hwp = make_hwp_doc(vec![make_para("This is a normal sentence.")]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    assert!(
        matches!(doc.sections[0].blocks[0], ir::Block::Paragraph { .. }),
        "plain text must remain a Paragraph"
    );
}

#[test]
fn hwp_to_ir_year_paragraph_not_detected_as_list() {
    // 4-digit number followed by non-separator text must NOT become a list.
    let hwp = make_hwp_doc(vec![make_para("2026 business plan")]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    assert!(
        matches!(doc.sections[0].blocks[0], ir::Block::Paragraph { .. }),
        "year-like number prefix must remain a Paragraph, not become a list"
    );
}

#[test]
fn hwp_to_ir_heading_not_treated_as_list_item() {
    // Paragraph with heading_type = Some(0) must become a Heading, not a list.
    let mut di = DocInfo::default();
    di.para_shapes.push(ParaShape {
        heading_type: Some(0),
        ..Default::default()
    });
    // Even if text starts with "- " it should remain a Heading.
    let hwp = make_hwp_doc_with_di(di, vec![make_para_with_ps("- Heading text", 0)]);
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    assert!(
        matches!(doc.sections[0].blocks[0], ir::Block::Heading { .. }),
        "heading paragraph must remain a Heading even if text starts with bullet"
    );
}

#[test]
fn hwp_to_ir_binary_numbering_id_groups_plain_text_into_list() {
    // Paragraphs with numbering_id set (formal binary list) must become a
    // Block::List even when the text has no bullet/number prefix.
    let di = doc_info_with_numbering();
    let hwp = make_hwp_doc_with_di(
        di,
        vec![
            make_para_with_ps("First formal item", 0),
            make_para_with_ps("Second formal item", 0),
        ],
    );
    let doc = hwp_to_ir(&hwp);
    assert_eq!(doc.sections[0].blocks.len(), 1);
    if let ir::Block::List { items, .. } = &doc.sections[0].blocks[0] {
        assert_eq!(items.len(), 2);
    } else {
        panic!("expected Block::List for formal binary list paragraphs");
    }
}