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
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
use crate::ir::{self, InlineFormat};
use comrak::nodes::{AstNode, ListType, NodeValue};
use comrak::{parse_document, Arena, Options};

#[must_use]
// Top-level parser walks all CommonMark node kinds; splitting would lose structural coherence.
#[allow(clippy::too_many_lines)]
pub fn parse_markdown(input: &str) -> ir::Document {
    // Walk the AST nodes and route them into body, header, or footer
    // depending on the surrounding `<!-- header -->` / `<!-- /header -->` and
    // `<!-- footer -->` / `<!-- /footer -->` marker pairs.  The markers are
    // detected at the comrak AST level (before `node_to_block` is called) so
    // that they never appear as IR blocks themselves.
    #[derive(PartialEq, Debug)]
    enum Region {
        Body,
        Header,
        Footer,
    }

    let arena = Arena::new();
    let mut options = Options::default();
    options.extension.table = true;
    options.extension.strikethrough = true;
    options.extension.footnotes = true;
    options.extension.math_dollars = true;
    options.extension.superscript = true;
    options.extension.tasklist = true;

    let root = parse_document(&arena, input, &options);

    let mut doc = ir::Document::new();
    doc.metadata = extract_frontmatter(input);

    let mut region = Region::Body;
    let mut header_blocks: Vec<ir::Block> = Vec::new();
    let mut footer_blocks: Vec<ir::Block> = Vec::new();
    let mut body_blocks: Vec<ir::Block> = Vec::new();

    for child in root.children() {
        // Inspect the node value to detect marker comments.  The borrow of
        // `child.data` must be dropped before calling `node_to_block`, which
        // borrows the same RefCell internally.
        let is_marker = {
            let data = child.data.borrow();
            if let NodeValue::HtmlBlock(html) = &data.value {
                if is_header_start_marker(&html.literal) {
                    region = Region::Header;
                    true
                } else if is_header_end_marker(&html.literal) {
                    region = Region::Body;
                    true
                } else if is_footer_start_marker(&html.literal) {
                    region = Region::Footer;
                    true
                } else if is_footer_end_marker(&html.literal) {
                    region = Region::Body;
                    true
                } else {
                    false
                }
            } else {
                false
            }
        };

        if is_marker {
            continue;
        }

        if let Some(block) = node_to_block(child) {
            match region {
                Region::Body => body_blocks.push(block),
                Region::Header => header_blocks.push(block),
                Region::Footer => footer_blocks.push(block),
            }
        }
    }

    // Unclosed marker fallback: if we reach end-of-input still inside a
    // Header or Footer region, the marker was never closed.  Move the
    // misrouted blocks into body_blocks so content is not silently lost,
    // and warn the caller.
    if region != Region::Body {
        tracing::warn!("unclosed {region:?} marker in markdown input, falling back to body");
        body_blocks.append(&mut header_blocks);
        body_blocks.append(&mut footer_blocks);
    }

    let section = ir::Section {
        blocks: body_blocks,
        page_layout: None,
        header: if header_blocks.is_empty() {
            None
        } else {
            Some(header_blocks)
        },
        footer: if footer_blocks.is_empty() {
            None
        } else {
            Some(footer_blocks)
        },
        header_footer_type: None,
        break_setting: ir::BreakSetting::default(),
    };

    doc.sections.push(section);
    doc
}

fn extract_frontmatter(input: &str) -> ir::Metadata {
    let mut meta = ir::Metadata::default();

    let trimmed = input.trim_start();
    if !trimmed.starts_with("---") {
        return meta;
    }

    let rest = &trimmed[3..];
    if let Some(end) = rest.find("\n---") {
        let yaml_block = &rest[..end];
        for line in yaml_block.lines() {
            let line = line.trim();
            if let Some((key, val)) = line.split_once(':') {
                let key = key.trim();
                let val = val.trim().trim_matches('"').trim_matches('\'');
                match key {
                    "title" => meta.title = Some(val.to_string()),
                    "author" => meta.author = Some(val.to_string()),
                    "date" => meta.created = Some(val.to_string()),
                    "subject" => meta.subject = Some(val.to_string()),
                    "description" => meta.description = Some(val.to_string()),
                    "keywords" => {
                        // Accept both inline-array form `[a, b, c]` and
                        // plain comma-separated string `a, b, c`.
                        let raw = if val.starts_with('[') && val.ends_with(']') {
                            &val[1..val.len() - 1]
                        } else {
                            val
                        };
                        meta.keywords = raw
                            .split(',')
                            .map(|s| s.trim().trim_matches('"').trim_matches('\'').to_string())
                            .filter(|s| !s.is_empty())
                            .collect();
                    }
                    _ => {}
                }
            }
        }
    }

    meta
}

// Dispatches over all CommonMark node types; heading level is 1-6, always fits in u8.
#[allow(clippy::too_many_lines)]
#[allow(clippy::cast_possible_truncation)]
fn node_to_block<'a>(node: &'a AstNode<'a>) -> Option<ir::Block> {
    let data = node.data.borrow();
    match &data.value {
        NodeValue::Heading(heading) => {
            let inlines = collect_inlines(node);
            Some(ir::Block::Heading {
                level: heading.level,
                inlines,
            })
        }
        NodeValue::Paragraph => {
            let inlines = collect_inlines(node);
            if inlines.is_empty() {
                return None;
            }
            Some(ir::Block::Paragraph { inlines })
        }
        NodeValue::CodeBlock(cb) => Some(ir::Block::CodeBlock {
            language: if cb.info.is_empty() {
                None
            } else {
                Some(cb.info.clone())
            },
            code: cb.literal.clone(),
        }),
        NodeValue::BlockQuote => {
            let blocks: Vec<_> = node.children().filter_map(|c| node_to_block(c)).collect();
            Some(ir::Block::BlockQuote { blocks })
        }
        NodeValue::List(list) => {
            let ordered = list.list_type == ListType::Ordered;
            let start = list.start as u32;
            let items: Vec<_> = node
                .children()
                .filter_map(|item| {
                    let item_data = item.data.borrow();
                    match &item_data.value {
                        NodeValue::Item(_) => {
                            let blocks: Vec<_> =
                                item.children().filter_map(|c| node_to_block(c)).collect();
                            Some(ir::ListItem {
                                blocks,
                                children: Vec::new(),
                                checked: None,
                            })
                        }
                        NodeValue::TaskItem(symbol) => {
                            let checked = symbol.is_some();
                            let blocks: Vec<_> =
                                item.children().filter_map(|c| node_to_block(c)).collect();
                            Some(ir::ListItem {
                                blocks,
                                children: Vec::new(),
                                checked: Some(checked),
                            })
                        }
                        _ => None,
                    }
                })
                .collect();
            Some(ir::Block::List {
                ordered,
                start,
                items,
            })
        }
        NodeValue::Table(_) => {
            let mut rows = Vec::new();
            let mut col_count = 0;

            for (ri, row_node) in node.children().enumerate() {
                let row_data = row_node.data.borrow();
                if !matches!(row_data.value, NodeValue::TableRow(_)) {
                    continue;
                }

                let mut cells = Vec::new();
                for cell_node in row_node.children() {
                    let cell_data = cell_node.data.borrow();
                    if !matches!(cell_data.value, NodeValue::TableCell) {
                        continue;
                    }
                    let inlines = collect_inlines(cell_node);
                    cells.push(ir::TableCell {
                        blocks: vec![ir::Block::Paragraph { inlines }],
                        colspan: 1,
                        rowspan: 1,
                    });
                }
                col_count = col_count.max(cells.len());
                rows.push(ir::TableRow {
                    cells,
                    is_header: ri == 0,
                });
            }

            Some(ir::Block::Table { rows, col_count, inner_margin: None })
        }
        NodeValue::ThematicBreak => Some(ir::Block::HorizontalRule),
        NodeValue::HtmlBlock(html) => {
            if is_pagebreak_marker(&html.literal) {
                Some(ir::Block::PageBreak)
            } else {
                super::html_table::parse_html_table(&html.literal)
            }
        }
        NodeValue::Image(link) => {
            let alt = collect_alt_text(node);
            Some(ir::Block::Image {
                src: link.url.clone(),
                alt,
            })
        }
        NodeValue::FootnoteDefinition(def) => {
            let blocks: Vec<_> = node.children().filter_map(|c| node_to_block(c)).collect();
            Some(ir::Block::Footnote {
                id: def.name.clone(),
                content: blocks,
            })
        }
        NodeValue::Math(math) => Some(ir::Block::Math {
            display: math.display_math,
            tex: math.literal.clone(),
        }),
        _ => None,
    }
}

/// Return `true` when an HTML block contains exactly a `<!-- pagebreak -->`
/// (or `<!--pagebreak-->`) marker, ignoring surrounding whitespace and
/// comparing the keyword case-insensitively.  This is the round-trip marker
/// emitted by [`crate::md::write_markdown`] for [`ir::Block::PageBreak`].
fn is_pagebreak_marker(html: &str) -> bool {
    html_comment_keyword(html).is_some_and(|k| k.eq_ignore_ascii_case("pagebreak"))
}

/// Return `true` when the HTML block is a `<!-- header -->` opening marker.
fn is_header_start_marker(html: &str) -> bool {
    html_comment_keyword(html).is_some_and(|k| k.eq_ignore_ascii_case("header"))
}

/// Return `true` when the HTML block is a `<!-- /header -->` closing marker.
fn is_header_end_marker(html: &str) -> bool {
    html_comment_keyword(html).is_some_and(|k| k.eq_ignore_ascii_case("/header"))
}

/// Return `true` when the HTML block is a `<!-- footer -->` opening marker.
fn is_footer_start_marker(html: &str) -> bool {
    html_comment_keyword(html).is_some_and(|k| k.eq_ignore_ascii_case("footer"))
}

/// Return `true` when the HTML block is a `<!-- /footer -->` closing marker.
fn is_footer_end_marker(html: &str) -> bool {
    html_comment_keyword(html).is_some_and(|k| k.eq_ignore_ascii_case("/footer"))
}

/// Extract the single keyword inside an HTML comment of the form
/// `<!-- keyword -->` (or `<!--keyword-->`).  Returns `None` when the string
/// is not a well-formed single-keyword comment.
///
/// The trimmed outer whitespace is stripped before checking the delimiters so
/// that comrak's trailing newline in `HtmlBlock.literal` does not break the
/// match.  The inner content is returned as-is (not lowercased) so callers can
/// apply their own case comparison.
fn html_comment_keyword(html: &str) -> Option<&str> {
    let trimmed = html.trim();
    let inner = trimmed
        .strip_prefix("<!--")
        .and_then(|s| s.strip_suffix("-->"))?;
    // The inner content must be a single token (no embedded whitespace that
    // would indicate extra text) — but leading/trailing space around the
    // keyword is allowed (e.g. `<!-- header -->`).
    let keyword = inner.trim();
    // Reject if the keyword itself contains whitespace (multi-word comment).
    if keyword.contains(char::is_whitespace) {
        return None;
    }
    Some(keyword)
}

fn collect_alt_text<'a>(node: &'a AstNode<'a>) -> String {
    let mut text = String::new();
    for child in node.children() {
        let data = child.data.borrow();
        if let NodeValue::Text(t) = &data.value {
            text.push_str(t);
        }
    }
    text
}

fn collect_inlines<'a>(node: &'a AstNode<'a>) -> Vec<ir::Inline> {
    let mut inlines = Vec::new();
    let default_style = InlineStyle::default();
    collect_inlines_recursive(node, &mut inlines, &default_style);
    inlines
}

// Bool fields track independent Markdown inline format states (bold, italic, code, strikethrough).
#[allow(clippy::struct_excessive_bools)]
#[derive(Default, Clone)]
struct InlineStyle {
    bold: bool,
    italic: bool,
    underline: bool,
    strikethrough: bool,
    superscript: bool,
    subscript: bool,
    link: Option<String>,
}

// Handles all CommonMark node types in inline context; cannot be decomposed.
#[allow(clippy::too_many_lines)]
fn collect_inlines_recursive<'a>(
    node: &'a AstNode<'a>,
    inlines: &mut Vec<ir::Inline>,
    style: &InlineStyle,
) {
    let mut current_style = style.clone();

    // Ruby annotation state: tracks `<ruby>…<rt>annotation</rt></ruby>` sequences
    // in the inline HTML node stream emitted by comrak.
    let mut in_ruby = false;
    let mut in_rt = false;
    let mut ruby_annotation = String::new();
    // Index into `inlines` at which the current ruby base span started.
    let mut ruby_base_start: usize = 0;

    for child in node.children() {
        let data = child.data.borrow();
        match &data.value {
            NodeValue::Text(text) => {
                // When inside <rt>…</rt>, accumulate annotation text rather than
                // emitting a regular inline.
                if in_rt {
                    ruby_annotation.push_str(text);
                    continue;
                }
                let fmt = InlineFormat {
                    bold: current_style.bold,
                    italic: current_style.italic,
                    underline: current_style.underline,
                    strikethrough: current_style.strikethrough,
                    superscript: current_style.superscript,
                    subscript: current_style.subscript,
                    color: None,
                };
                inlines.push(
                    ir::Inline::with_formatting(text.clone(), &fmt)
                        .with_link(current_style.link.clone()),
                );
            }
            NodeValue::SoftBreak | NodeValue::LineBreak => {
                if !in_rt {
                    inlines.push(ir::Inline::plain("\n"));
                }
            }
            NodeValue::Code(code) => {
                if !in_rt {
                    inlines.push(ir::Inline {
                        text: code.literal.clone(),
                        bold: false,
                        italic: false,
                        underline: false,
                        strikethrough: false,
                        code: true,
                        superscript: false,
                        subscript: false,
                        link: None,
                        footnote_ref: None,
                        color: None,
                        font_name: None,
                        ruby: None,
                    });
                }
            }
            NodeValue::HtmlInline(html) => {
                let tag = html.trim();
                if tag.eq_ignore_ascii_case("<ruby>") {
                    if in_ruby {
                        tracing::warn!("nested <ruby> not supported, ignoring inner");
                    } else {
                        in_ruby = true;
                        in_rt = false;
                        ruby_annotation.clear();
                        ruby_base_start = inlines.len();
                    }
                } else if tag.eq_ignore_ascii_case("<rt>") {
                    in_rt = true;
                } else if tag.eq_ignore_ascii_case("</rt>") {
                    in_rt = false;
                } else if tag.eq_ignore_ascii_case("</ruby>") {
                    // Attach the accumulated annotation to every inline that
                    // was emitted as part of the ruby base span.
                    if in_ruby && !ruby_annotation.is_empty() {
                        let annotation = ruby_annotation.clone();
                        for inline in &mut inlines[ruby_base_start..] {
                            inline.ruby = Some(annotation.clone());
                        }
                    }
                    in_ruby = false;
                    in_rt = false;
                    ruby_annotation.clear();
                } else if tag.eq_ignore_ascii_case("<u>") {
                    current_style.underline = true;
                } else if tag.eq_ignore_ascii_case("</u>") {
                    current_style.underline = style.underline;
                } else if tag.eq_ignore_ascii_case("<sub>") {
                    current_style.subscript = true;
                } else if tag.eq_ignore_ascii_case("</sub>") {
                    current_style.subscript = style.subscript;
                } else {
                    inlines.push(ir::Inline::plain(html.clone()));
                }
            }
            NodeValue::Strong => {
                let mut s = current_style.clone();
                s.bold = true;
                // For ruby base spans that contain rich content (e.g. bold),
                // recurse and then patch the newly-added inlines with the ruby
                // annotation when </ruby> is encountered later.
                collect_inlines_recursive(child, inlines, &s);
            }
            NodeValue::Emph => {
                let mut s = current_style.clone();
                s.italic = true;
                collect_inlines_recursive(child, inlines, &s);
            }
            NodeValue::Strikethrough => {
                let mut s = current_style.clone();
                s.strikethrough = true;
                collect_inlines_recursive(child, inlines, &s);
            }
            NodeValue::Superscript => {
                let mut s = current_style.clone();
                s.superscript = true;
                collect_inlines_recursive(child, inlines, &s);
            }
            NodeValue::Link(link) => {
                let mut s = current_style.clone();
                s.link = Some(link.url.clone());
                collect_inlines_recursive(child, inlines, &s);
            }
            NodeValue::Image(link) => {
                let alt = collect_alt_text(child);
                inlines.push(ir::Inline::plain(format!("![{}]({})", alt, link.url)));
            }
            NodeValue::FootnoteReference(fref) => {
                inlines.push(ir::Inline::footnote_ref(fref.name.clone()));
            }
            NodeValue::Math(math) => {
                let delim = if math.display_math { "$$" } else { "$" };
                inlines.push(ir::Inline::plain(format!("{delim}{}{delim}", math.literal)));
            }
            _ => {
                collect_inlines_recursive(child, inlines, &current_style);
            }
        }
    }
}

#[cfg(test)]
#[path = "parser_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "parser_tests_inline.rs"]
mod tests_inline;

#[cfg(test)]
#[path = "parser_tests_marker.rs"]
mod tests_marker;

#[cfg(test)]
#[path = "parser_tests_html_table.rs"]
mod tests_html_table;