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
use crate::error::Hwp2MdError;
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;
use std::io::{Cursor, Write};

use super::{CharPrKey, RefTables, LANG_SLOTS};
use crate::ir;

// ---------------------------------------------------------------------------
// header.xml -- OWPML reference tables
// ---------------------------------------------------------------------------

/// OWPML numbering definition ID for ordered (digit) lists.
///
/// Referenced by `numPrIDRef` on list-item paragraphs in section XML.
/// Unordered (bullet) list items do **not** use a `numPrIDRef` — their
/// visual bullet character and indentation are handled by the paragraph
/// property (`paraPrIDRef`) alone.
pub(super) const NUM_PR_DIGIT: &str = "1";

/// OWPML paragraph-property ID for top-level list items (depth = 0).
///
/// This entry carries the left-indent that visually separates list items
/// from the enclosing paragraph text.
pub(super) const PARA_PR_LIST_D0: &str = "2";

/// OWPML paragraph-property ID for nested list items (depth ≥ 1).
///
/// Each additional nesting level adds another step of left-indentation.
pub(super) const PARA_PR_LIST_D1: &str = "3";

/// Left margin (HWP units) applied to top-level list item paragraphs.
///
/// One HWP unit = 0.01 mm, so 400 ≈ 4 mm.
const LIST_D0_MARGIN_LEFT: &str = "400";

/// Left margin (HWP units) applied to nested list item paragraphs (depth ≥ 1).
const LIST_D1_MARGIN_LEFT: &str = "800";

pub(super) fn generate_header_xml(
    doc: &ir::Document,
    tables: &RefTables,
) -> Result<String, Hwp2MdError> {
    let mut buf = Cursor::new(Vec::new());
    let mut w = Writer::new_with_indent(&mut buf, b' ', 2);

    w.write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))?;

    let sec_cnt = doc.sections.len().max(1);

    // <hh:head version="1.1" secCnt="N">
    let mut head = BytesStart::new("hh:head");
    head.push_attribute(("xmlns:hh", "http://www.hancom.co.kr/hwpml/2011/head"));
    head.push_attribute(("version", "1.1"));
    head.push_attribute(("secCnt", sec_cnt.to_string().as_str()));
    w.write_event(Event::Start(head))?;

    // <hh:beginNum> -- required structural marker
    let mut begin_num = BytesStart::new("hh:beginNum");
    begin_num.push_attribute(("page", "1"));
    begin_num.push_attribute(("footnote", "1"));
    begin_num.push_attribute(("endnote", "1"));
    begin_num.push_attribute(("pic", "1"));
    begin_num.push_attribute(("tbl", "1"));
    begin_num.push_attribute(("equation", "1"));
    w.write_event(Event::Empty(begin_num))?;

    // <hh:refList> -- contains fontfaces, charProperties, paraProperties, styles,
    // and numbering definitions.
    w.write_event(Event::Start(BytesStart::new("hh:refList")))?;

    write_font_faces(&mut w, tables)?;
    write_border_fills(&mut w, tables)?;
    write_char_properties(&mut w, tables)?;
    write_para_properties(&mut w)?;
    write_styles(&mut w, tables)?;

    // <hh:numberingList> must be inside <hh:refList> per OWPML schema.
    // id=1 → unordered (bullet ●), id=2 → ordered (digit %d.)
    write_numbering_list(&mut w)?;

    w.write_event(Event::End(BytesEnd::new("hh:refList")))?;

    // <hh:trackchangeConfig> -- required by schema (minOccurs=1)
    w.write_event(Event::Empty(BytesStart::new("hh:trackchangeConfig")))?;

    w.write_event(Event::End(BytesEnd::new("hh:head")))?;

    String::from_utf8(buf.into_inner())
        .map_err(|e| Hwp2MdError::HwpxWrite(format!("header XML is not valid UTF-8: {e}")))
}

fn write_font_faces<W: Write>(
    w: &mut Writer<W>,
    tables: &RefTables,
) -> Result<(), quick_xml::Error> {
    let font_cnt = tables.font_names.len().to_string();

    let mut fontfaces = BytesStart::new("hh:fontfaces");
    fontfaces.push_attribute(("itemCnt", LANG_SLOTS.len().to_string().as_str()));
    w.write_event(Event::Start(fontfaces))?;

    for lang in LANG_SLOTS {
        let mut face = BytesStart::new("hh:fontface");
        face.push_attribute(("lang", lang));
        face.push_attribute(("fontCnt", font_cnt.as_str()));
        w.write_event(Event::Start(face))?;

        for (id, name) in tables.font_names.iter().enumerate() {
            let mut font = BytesStart::new("hh:font");
            font.push_attribute(("id", id.to_string().as_str()));
            font.push_attribute(("face", name.as_str()));
            font.push_attribute(("type", "REP"));
            w.write_event(Event::Empty(font))?;
        }

        w.write_event(Event::End(BytesEnd::new("hh:fontface")))?;
    }

    w.write_event(Event::End(BytesEnd::new("hh:fontfaces")))?;
    Ok(())
}

fn write_border_fills<W: Write>(
    w: &mut Writer<W>,
    tables: &RefTables,
) -> Result<(), quick_xml::Error> {
    let mut border_fills = BytesStart::new("hh:borderFills");
    border_fills.push_attribute(("itemCnt", "1"));
    w.write_event(Event::Start(border_fills))?;

    let id_str = tables.border_fill_id.to_string();
    let mut bf = BytesStart::new("hh:borderFill");
    bf.push_attribute(("id", id_str.as_str()));
    bf.push_attribute(("threeD", "false"));
    bf.push_attribute(("shadow", "false"));
    w.write_event(Event::Start(bf))?;

    let mut slash = BytesStart::new("hh:slash");
    slash.push_attribute(("type", "NONE"));
    slash.push_attribute(("Crooked", "false"));
    slash.push_attribute(("isCounter", "false"));
    w.write_event(Event::Empty(slash))?;

    let mut back_slash = BytesStart::new("hh:backSlash");
    back_slash.push_attribute(("type", "NONE"));
    back_slash.push_attribute(("Crooked", "false"));
    back_slash.push_attribute(("isCounter", "false"));
    w.write_event(Event::Empty(back_slash))?;

    for border_name in &[
        "hh:leftBorder",
        "hh:rightBorder",
        "hh:topBorder",
        "hh:bottomBorder",
        "hh:diagonal",
    ] {
        let mut border = BytesStart::new(*border_name);
        border.push_attribute(("type", "NONE"));
        border.push_attribute(("width", "0.12 mm"));
        border.push_attribute(("color", "000000"));
        w.write_event(Event::Empty(border))?;
    }

    w.write_event(Event::End(BytesEnd::new("hh:borderFill")))?;
    w.write_event(Event::End(BytesEnd::new("hh:borderFills")))?;
    Ok(())
}

fn write_char_properties<W: Write>(
    w: &mut Writer<W>,
    tables: &RefTables,
) -> Result<(), quick_xml::Error> {
    // Collect and sort by ID for deterministic output.
    let mut entries: Vec<(&CharPrKey, u32)> =
        tables.char_pr_ids.iter().map(|(k, &id)| (k, id)).collect();
    entries.sort_by_key(|(_, id)| *id);

    let mut char_properties = BytesStart::new("hh:charProperties");
    char_properties.push_attribute(("itemCnt", entries.len().to_string().as_str()));
    w.write_event(Event::Start(char_properties))?;

    for (key, id) in entries {
        let font_id: u32 = key
            .font_name
            .as_deref()
            .and_then(|name| {
                tables
                    .font_names
                    .iter()
                    .position(|f| f == name)
                    .map(|i| i as u32)
            })
            .unwrap_or(0);

        let color = key.color.as_deref().unwrap_or("#000000");

        let height_str = key.height.to_string();
        let border_fill_ref = tables.border_fill_id.to_string();
        // OWPML schema only allows: id, height, textColor, shadeColor,
        // useFontSpace, useKerning, symMark, borderFillIDRef.
        // bold/italic/underline/strikeout are NOT valid attributes here.
        let mut char_pr = BytesStart::new("hh:charPr");
        char_pr.push_attribute(("id", id.to_string().as_str()));
        char_pr.push_attribute(("height", height_str.as_str()));
        char_pr.push_attribute(("textColor", color));
        char_pr.push_attribute(("borderFillIDRef", border_fill_ref.as_str()));
        // OWPML uses a single `supscript` attribute for both superscript and
        // subscript.  The value is either "superscript" or "subscript".
        if key.superscript {
            char_pr.push_attribute(("supscript", "superscript"));
        } else if key.subscript {
            char_pr.push_attribute(("supscript", "subscript"));
        }
        w.write_event(Event::Start(char_pr))?;

        let font_id_str = font_id.to_string();
        let mut font_ref = BytesStart::new("hh:fontRef");
        font_ref.push_attribute(("hangul", font_id_str.as_str()));
        font_ref.push_attribute(("latin", font_id_str.as_str()));
        font_ref.push_attribute(("hanja", font_id_str.as_str()));
        font_ref.push_attribute(("japanese", font_id_str.as_str()));
        font_ref.push_attribute(("other", font_id_str.as_str()));
        font_ref.push_attribute(("symbol", font_id_str.as_str()));
        font_ref.push_attribute(("user", font_id_str.as_str()));
        w.write_event(Event::Empty(font_ref))?;

        // Required children (minOccurs=1) with default values.
        let mut ratio = BytesStart::new("hh:ratio");
        ratio.push_attribute(("hangul", "100"));
        ratio.push_attribute(("latin", "100"));
        ratio.push_attribute(("hanja", "100"));
        ratio.push_attribute(("japanese", "100"));
        ratio.push_attribute(("other", "100"));
        ratio.push_attribute(("symbol", "100"));
        ratio.push_attribute(("user", "100"));
        w.write_event(Event::Empty(ratio))?;

        let mut spacing = BytesStart::new("hh:spacing");
        spacing.push_attribute(("hangul", "0"));
        spacing.push_attribute(("latin", "0"));
        spacing.push_attribute(("hanja", "0"));
        spacing.push_attribute(("japanese", "0"));
        spacing.push_attribute(("other", "0"));
        spacing.push_attribute(("symbol", "0"));
        spacing.push_attribute(("user", "0"));
        w.write_event(Event::Empty(spacing))?;

        let mut rel_sz = BytesStart::new("hh:relSz");
        rel_sz.push_attribute(("hangul", "100"));
        rel_sz.push_attribute(("latin", "100"));
        rel_sz.push_attribute(("hanja", "100"));
        rel_sz.push_attribute(("japanese", "100"));
        rel_sz.push_attribute(("other", "100"));
        rel_sz.push_attribute(("symbol", "100"));
        rel_sz.push_attribute(("user", "100"));
        w.write_event(Event::Empty(rel_sz))?;

        let mut offset = BytesStart::new("hh:offset");
        offset.push_attribute(("hangul", "0"));
        offset.push_attribute(("latin", "0"));
        offset.push_attribute(("hanja", "0"));
        offset.push_attribute(("japanese", "0"));
        offset.push_attribute(("other", "0"));
        offset.push_attribute(("symbol", "0"));
        offset.push_attribute(("user", "0"));
        w.write_event(Event::Empty(offset))?;

        w.write_event(Event::End(BytesEnd::new("hh:charPr")))?;
    }

    w.write_event(Event::End(BytesEnd::new("hh:charProperties")))?;
    Ok(())
}

/// Left margin value for block-quote paragraphs (HWP units; 800 ≈ 20 mm).
pub(super) const QUOTE_MARGIN_LEFT: &str = "800";

/// OWPML paragraph-property ID for heading paragraphs (H1–H6).
///
/// Headings use a distinct `paraPr` entry with increased line spacing (180%)
/// to provide more visual separation from surrounding body text.
pub(super) const PARA_PR_HEADING: &str = "4";

/// Configuration for a single `<hh:paraPr>` entry emitted to the OWPML
/// header's `<hh:paraProperties>` list.
///
/// All margin values are in HWP units (1 HWP unit = 0.01 mm).
/// Line spacing is expressed as an integer percentage (e.g. `160` = 160%).
struct ParaPrConfig<'a> {
    /// The numeric paragraph-property ID referenced by `paraPrIDRef` in section XML.
    id: &'a str,
    /// Left margin in HWP units.  Controls visual indentation level.
    left_margin: &'a str,
    /// Line spacing as a percentage string (e.g. `"160"`, `"180"`).
    line_spacing: &'a str,
    /// First-line indent in HWP units (`<hh:intent value="…"/>`).
    first_indent: &'a str,
}

impl<'a> ParaPrConfig<'a> {
    /// Construct a config with the given ID and left margin, using standard
    /// defaults: 160% line spacing and no first-line indent.
    const fn standard(id: &'a str, left_margin: &'a str) -> Self {
        Self {
            id,
            left_margin,
            line_spacing: "160",
            first_indent: "0",
        }
    }

    /// Construct a config with all fields specified explicitly.
    const fn with_spacing(
        id: &'a str,
        left_margin: &'a str,
        line_spacing: &'a str,
        first_indent: &'a str,
    ) -> Self {
        Self {
            id,
            left_margin,
            line_spacing,
            first_indent,
        }
    }
}

fn write_para_properties<W: Write>(w: &mut Writer<W>) -> Result<(), quick_xml::Error> {
    // Five paraPr entries:
    //   id=0: default paragraph (no left indent, 160% line spacing)
    //   id=1: block-quote (left indent = 800, 160% line spacing)
    //   id=2: list item depth 0 (left indent = 400, 160% line spacing)
    //   id=3: list item depth 1+ (left indent = 800, 160% line spacing)
    //   id=4: heading (no left indent, 180% line spacing for visual separation)
    let mut para_properties = BytesStart::new("hh:paraProperties");
    para_properties.push_attribute(("itemCnt", "5"));
    w.write_event(Event::Start(para_properties))?;

    // ── paraPr id=0: default (no left indent, standard line spacing) ──
    write_single_para_pr(w, &ParaPrConfig::standard("0", "0"))?;

    // ── paraPr id=1: block-quote (left indent) ──
    write_single_para_pr(w, &ParaPrConfig::standard("1", QUOTE_MARGIN_LEFT))?;

    // ── paraPr id=2: top-level list item ──
    write_single_para_pr(
        w,
        &ParaPrConfig::standard(PARA_PR_LIST_D0, LIST_D0_MARGIN_LEFT),
    )?;

    // ── paraPr id=3: nested list item (depth ≥ 1) ──
    write_single_para_pr(
        w,
        &ParaPrConfig::standard(PARA_PR_LIST_D1, LIST_D1_MARGIN_LEFT),
    )?;

    // ── paraPr id=4: heading (wider line spacing for readability) ──
    write_single_para_pr(
        w,
        &ParaPrConfig::with_spacing(PARA_PR_HEADING, "0", "180", "0"),
    )?;

    w.write_event(Event::End(BytesEnd::new("hh:paraProperties")))?;
    Ok(())
}

/// Emit a single `<hh:paraPr>` element with all required OWPML children.
///
/// `config.left_margin` is the HWP-unit value for the `<hh:left>` child inside
/// `<hh:margin>`.  `config.line_spacing` controls the `<hh:lineSpacing>` value
/// attribute (percentage string).  `config.first_indent` sets the `<hh:intent>`
/// first-line indent (HWP units).  All other margin children are emitted as `"0"`.
fn write_single_para_pr<W: Write>(
    w: &mut Writer<W>,
    config: &ParaPrConfig<'_>,
) -> Result<(), quick_xml::Error> {
    let mut para_pr = BytesStart::new("hh:paraPr");
    para_pr.push_attribute(("id", config.id));
    w.write_event(Event::Start(para_pr))?;

    // <align> requires both horizontal and vertical attributes.
    let mut align = BytesStart::new("hh:align");
    align.push_attribute(("horizontal", "JUSTIFY"));
    align.push_attribute(("vertical", "BASELINE"));
    w.write_event(Event::Empty(align))?;

    // Required children (minOccurs=1) with default values.
    let mut heading = BytesStart::new("hh:heading");
    heading.push_attribute(("type", "NONE"));
    heading.push_attribute(("idRef", "0"));
    heading.push_attribute(("level", "0"));
    w.write_event(Event::Empty(heading))?;

    // breakSetting requires all seven attributes (schema: all required).
    let mut break_setting = BytesStart::new("hh:breakSetting");
    break_setting.push_attribute(("breakLatinWord", "KEEP_WORD"));
    break_setting.push_attribute(("breakNonLatinWord", "KEEP_WORD"));
    break_setting.push_attribute(("widowOrphan", "false"));
    break_setting.push_attribute(("keepWithNext", "false"));
    break_setting.push_attribute(("keepLines", "false"));
    break_setting.push_attribute(("pageBreakBefore", "false"));
    break_setting.push_attribute(("lineWrap", "BREAK"));
    w.write_event(Event::Empty(break_setting))?;

    let mut line_spacing = BytesStart::new("hh:lineSpacing");
    line_spacing.push_attribute(("type", "PERCENT"));
    line_spacing.push_attribute(("value", config.line_spacing));
    w.write_event(Event::Empty(line_spacing))?;

    // margin children are HWPValue elements (value attribute, no attrs on margin itself).
    w.write_event(Event::Start(BytesStart::new("hh:margin")))?;
    for child_name in &["hh:intent", "hh:left", "hh:right", "hh:prev", "hh:next"] {
        let mut child = BytesStart::new(*child_name);
        let value = match *child_name {
            "hh:left" => config.left_margin,
            "hh:intent" => config.first_indent,
            _ => "0",
        };
        child.push_attribute(("value", value));
        w.write_event(Event::Empty(child))?;
    }
    w.write_event(Event::End(BytesEnd::new("hh:margin")))?;

    let mut border = BytesStart::new("hh:border");
    border.push_attribute(("borderFillIDRef", "1"));
    w.write_event(Event::Empty(border))?;

    let mut auto_spacing = BytesStart::new("hh:autoSpacing");
    auto_spacing.push_attribute(("eAsianEng", "false"));
    auto_spacing.push_attribute(("eAsianNum", "false"));
    w.write_event(Event::Empty(auto_spacing))?;

    w.write_event(Event::End(BytesEnd::new("hh:paraPr")))?;
    Ok(())
}

fn write_styles<W: Write>(w: &mut Writer<W>, tables: &RefTables) -> Result<(), quick_xml::Error> {
    // 7 styles: Normal (id=0) + Heading1-6 (id=1..6).
    let mut styles = BytesStart::new("hh:styles");
    styles.push_attribute(("itemCnt", "7"));
    w.write_event(Event::Start(styles))?;

    // Normal style uses the default charPr (id=0).
    let mut normal = BytesStart::new("hh:style");
    normal.push_attribute(("id", "0"));
    normal.push_attribute(("type", "PARA"));
    normal.push_attribute(("name", "Normal"));
    normal.push_attribute(("paraPrIDRef", "0"));
    normal.push_attribute(("charPrIDRef", "0"));
    w.write_event(Event::Empty(normal))?;

    // Heading styles reference level-specific charPr entries.
    for level in 1..=6u8 {
        let char_pr_id = tables.heading_char_pr_id(level);
        let id_str = level.to_string();
        let char_pr_id_str = char_pr_id.to_string();
        let name = format!("Heading{level}");
        let mut style = BytesStart::new("hh:style");
        style.push_attribute(("id", id_str.as_str()));
        style.push_attribute(("type", "PARA"));
        style.push_attribute(("name", name.as_str()));
        style.push_attribute(("paraPrIDRef", "0"));
        style.push_attribute(("charPrIDRef", char_pr_id_str.as_str()));
        w.write_event(Event::Empty(style))?;
    }

    w.write_event(Event::End(BytesEnd::new("hh:styles")))?;
    Ok(())
}

/// Emit the OWPML `<hh:numberings>` element containing the ordered-list
/// numbering definition used by ordered list-item paragraphs in section XML.
///
/// Only one entry is emitted (id=1, `NUM_PR_DIGIT`) for ordered (decimal digit)
/// lists.  Unordered (bullet) lists do **not** reference a `<hh:numbering>`
/// entry — their indentation is carried entirely by the paragraph property.
///
/// The `<hh:paraHead>` child carries the required OWPML attributes and the
/// numbering format string (`%d.`).
fn write_numbering_list<W: Write>(w: &mut Writer<W>) -> Result<(), quick_xml::Error> {
    let mut num_list = BytesStart::new("hh:numberings");
    num_list.push_attribute(("itemCnt", "1"));
    w.write_event(Event::Start(num_list))?;

    // ── id=1: ordered (digit %d.) ────────────────────────────────────────
    let mut digit_num = BytesStart::new("hh:numbering");
    digit_num.push_attribute(("id", NUM_PR_DIGIT));
    digit_num.push_attribute(("start", "1"));
    w.write_event(Event::Start(digit_num))?;

    let mut digit_head = BytesStart::new("hh:paraHead");
    digit_head.push_attribute(("level", "1"));
    digit_head.push_attribute(("align", "LEFT"));
    digit_head.push_attribute(("useInstWidth", "false"));
    digit_head.push_attribute(("autoIndent", "true"));
    digit_head.push_attribute(("widthAdjust", "0"));
    digit_head.push_attribute(("textOffset", "400"));
    digit_head.push_attribute(("numFormat", "DIGIT"));
    digit_head.push_attribute(("charPrIDRef", "0"));
    digit_head.push_attribute(("checkable", "false"));
    digit_head.push_attribute(("start", "1"));
    w.write_event(Event::Start(digit_head))?;
    // Numbering format string: %d. (HWP uses %d as the counter placeholder)
    w.write_event(Event::Text(BytesText::new("%d.")))?;
    w.write_event(Event::End(BytesEnd::new("hh:paraHead")))?;

    w.write_event(Event::End(BytesEnd::new("hh:numbering")))?;

    w.write_event(Event::End(BytesEnd::new("hh:numberings")))?;
    Ok(())
}