jw-hwp-core 0.1.1

Read-only parser for Hancom HWP 5.0 (binary CFB) and HWPX (OWPML) documents
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
//! Parses `Contents/sectionN.xml` into paragraphs + tables.
//!
//! The XML structure is recursive: a table cell contains `<hp:subList>` with
//! its own `<hp:p>` children. We use a stack-based walker that pauses the
//! current paragraph while descending into a cell's sub-list, then resumes.

use crate::error::Error;
use crate::error::Warning;
use crate::model::{FootnoteBody, ImageRef, ParagraphDetail, Section};
use crate::structure::{make_preview, NodeKind, StructureNode};
use crate::table::{Cell, Table};
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Reader;

pub struct SectionOut {
    pub section: Section,
    pub warnings: Vec<Warning>,
}

#[derive(Default)]
struct ParaBuilder {
    para_shape_id: u32,
    text: String,
    runs: Vec<(u32, u32)>,
    /// Current open run's char_shape_id, if inside a <hp:run>.
    current_run_char_id: Option<u32>,
    /// Did this paragraph contain a table?
    contains_table: bool,
    /// Are we inside an open <hp:t> so Text events should be captured?
    in_t_element: bool,
    /// Footnote/endnote bodies attached to this paragraph.
    footnotes: Vec<FootnoteBody>,
    /// Equation script attached to this paragraph (if any).
    equation: Option<String>,
    /// Image (binaryItemIDRef) references on this paragraph.
    image_refs: Vec<ImageRef>,
}

struct TableBuilder {
    rows: u16,
    cols: u16,
    /// cells[row][col] = Option<Cell>
    cells: Vec<Vec<Option<Cell>>>,
    // state for the currently-open cell
    cur_cell_col: u16,
    cur_cell_row: u16,
    cur_cell_col_span: u16,
    cur_cell_row_span: u16,
    cur_cell_paragraphs: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParaContext {
    TopLevel,
    Cell,
    Note,
}

/// Collects inner-note paragraphs while we are inside a `<hp:footNote>` or
/// `<hp:endNote>`. Pushed on open, popped on close; on close, the accumulated
/// text is attached to the nearest top-level `ParaBuilder`.
struct NoteFrame {
    kind: String,
    paragraphs: Vec<String>,
}

pub fn parse(bytes: &[u8], section_index: usize) -> Result<SectionOut, Error> {
    let mut reader = Reader::from_reader(bytes);
    reader.config_mut().trim_text(false); // preserve whitespace inside <hp:t>

    let mut paragraphs: Vec<String> = Vec::new();
    let mut paragraph_details: Vec<ParagraphDetail> = Vec::new();
    let mut structure: Vec<StructureNode> = Vec::new();
    let mut tables: Vec<Table> = Vec::new();
    let warnings: Vec<Warning> = Vec::new();

    // Stack of paragraphs.
    let mut para_stack: Vec<ParaBuilder> = Vec::new();
    let mut table_stack: Vec<TableBuilder> = Vec::new();
    let mut context_stack: Vec<ParaContext> = Vec::new();
    let mut note_stack: Vec<NoteFrame> = Vec::new();
    // Depth tracker for the "eqImage"/"equation" script element so Event::Text
    // accumulates into `cur_equation_script` while inside it.
    let mut in_equation_script: bool = false;
    let mut cur_equation_script: String = String::new();

    loop {
        let ev = reader
            .read_event()
            .map_err(|e| Error::Container(format!("section xml: {e}")))?;

        match ev {
            Event::Start(e) => handle_start(
                &e,
                &mut para_stack,
                &mut context_stack,
                &mut table_stack,
                &mut note_stack,
                &mut in_equation_script,
                &mut cur_equation_script,
            ),
            Event::Empty(e) => handle_empty(&e, &mut para_stack, &mut table_stack),
            Event::Text(t) => {
                let s = t
                    .unescape()
                    .map_err(|e| Error::Container(format!("text: {e}")))?
                    .into_owned();
                if in_equation_script {
                    cur_equation_script.push_str(&s);
                } else if let Some(pb) = para_stack.last_mut() {
                    if pb.in_t_element {
                        pb.text.push_str(&s);
                    }
                }
            }
            Event::End(e) => handle_end(
                &e,
                &mut para_stack,
                &mut context_stack,
                &mut table_stack,
                &mut note_stack,
                &mut in_equation_script,
                &mut cur_equation_script,
                &mut paragraphs,
                &mut paragraph_details,
                &mut structure,
                &mut tables,
                section_index,
            ),
            Event::Eof => break,
            _ => {}
        }
    }

    Ok(SectionOut {
        section: Section {
            index: section_index,
            paragraphs,
            paragraph_details,
            structure,
            tables,
        },
        warnings,
    })
}

// Extend ParaBuilder with the in_t_element flag.
impl ParaBuilder {
    fn new(para_shape_id: u32) -> Self {
        Self {
            para_shape_id,
            text: String::new(),
            runs: Vec::new(),
            current_run_char_id: None,
            contains_table: false,
            in_t_element: false,
            footnotes: Vec::new(),
            equation: None,
            image_refs: Vec::new(),
        }
    }
}

/// Returns a mutable reference to the nearest TopLevel paragraph builder,
/// walking the stack from the top down. Returns None if none is open.
fn top_level_para<'a>(
    para_stack: &'a mut [ParaBuilder],
    context_stack: &[ParaContext],
) -> Option<&'a mut ParaBuilder> {
    let n = para_stack.len().min(context_stack.len());
    for i in (0..n).rev() {
        if context_stack[i] == ParaContext::TopLevel {
            return Some(&mut para_stack[i]);
        }
    }
    None
}

// Supplement struct with the flag by redefining above? Rust doesn't allow
// reopening. We'll just add field to the struct definition.
//
// (For clarity, re-declare ParaBuilder above with `in_t_element`.)

#[allow(clippy::too_many_arguments)]
fn handle_start(
    e: &BytesStart,
    para_stack: &mut Vec<ParaBuilder>,
    context_stack: &mut Vec<ParaContext>,
    table_stack: &mut Vec<TableBuilder>,
    note_stack: &mut Vec<NoteFrame>,
    in_equation_script: &mut bool,
    _cur_equation_script: &mut String,
) {
    let local_owned = e.name().local_name().as_ref().to_vec();
    let local = local_owned.as_slice();
    match local {
        b"p" => {
            let id = get_attr_u32(e, b"paraPrIDRef").unwrap_or(0);
            let ctx = if !note_stack.is_empty() {
                ParaContext::Note
            } else if !table_stack.is_empty() {
                ParaContext::Cell
            } else {
                ParaContext::TopLevel
            };
            para_stack.push(ParaBuilder::new(id));
            context_stack.push(ctx);
        }
        b"run" => {
            if let Some(pb) = para_stack.last_mut() {
                let char_id = get_attr_u32(e, b"charPrIDRef").unwrap_or(0);
                pb.current_run_char_id = Some(char_id);
                let start = pb.text.chars().count() as u32;
                pb.runs.push((start, char_id));
            }
        }
        b"t" => {
            if let Some(pb) = para_stack.last_mut() {
                pb.in_t_element = true;
            }
        }
        b"tbl" => {
            let rows = get_attr_u32(e, b"rowCnt").unwrap_or(0) as u16;
            let cols = get_attr_u32(e, b"colCnt").unwrap_or(0) as u16;
            let cells = vec![vec![None; cols as usize]; rows as usize];
            table_stack.push(TableBuilder {
                rows,
                cols,
                cells,
                cur_cell_col: 0,
                cur_cell_row: 0,
                cur_cell_col_span: 1,
                cur_cell_row_span: 1,
                cur_cell_paragraphs: Vec::new(),
            });
            if let Some(pb) = para_stack.last_mut() {
                pb.contains_table = true;
            }
        }
        b"tc" => {
            if let Some(tb) = table_stack.last_mut() {
                tb.cur_cell_paragraphs.clear();
                tb.cur_cell_col_span = 1;
                tb.cur_cell_row_span = 1;
            }
        }
        b"footNote" => {
            note_stack.push(NoteFrame {
                kind: "footnote".into(),
                paragraphs: Vec::new(),
            });
        }
        b"endNote" => {
            note_stack.push(NoteFrame {
                kind: "endnote".into(),
                paragraphs: Vec::new(),
            });
        }
        b"equation" => {
            // Prefer the `script` attribute; else collect text inside
            // <hp:script>…</hp:script> children via `in_equation_script`.
            if let Some(s) = get_attr(e, b"script") {
                if let Some(pb) = top_level_para(para_stack, context_stack) {
                    pb.equation = Some(s);
                }
            }
        }
        b"script" => {
            *in_equation_script = true;
        }
        b"pic" => {
            if let Some(id_str) = get_attr(e, b"binaryItemIDRef") {
                let bin_id = digit_prefix(&id_str).unwrap_or(0);
                if let Some(pb) = top_level_para(para_stack, context_stack) {
                    pb.image_refs.push(ImageRef { bin_id });
                }
            }
        }
        _ => {}
    }
}

/// Extract leading digits from a string (after skipping non-digits). Returns
/// None if no digits are present.
fn digit_prefix(s: &str) -> Option<u16> {
    let trimmed: String = s
        .chars()
        .skip_while(|c| !c.is_ascii_digit())
        .take_while(|c| c.is_ascii_digit())
        .collect();
    if trimmed.is_empty() {
        return None;
    }
    trimmed.parse::<u16>().ok()
}

fn handle_empty(e: &BytesStart, para_stack: &mut [ParaBuilder], table_stack: &mut [TableBuilder]) {
    let local_owned = e.name().local_name().as_ref().to_vec();
    let local = local_owned.as_slice();
    match local {
        b"cellAddr" => {
            if let Some(tb) = table_stack.last_mut() {
                tb.cur_cell_col = get_attr_u32(e, b"colAddr").unwrap_or(0) as u16;
                tb.cur_cell_row = get_attr_u32(e, b"rowAddr").unwrap_or(0) as u16;
            }
        }
        b"cellSpan" => {
            if let Some(tb) = table_stack.last_mut() {
                tb.cur_cell_col_span = get_attr_u32(e, b"colSpan").unwrap_or(1) as u16;
                tb.cur_cell_row_span = get_attr_u32(e, b"rowSpan").unwrap_or(1) as u16;
            }
        }
        b"pic" => {
            if let Some(id_str) = get_attr(e, b"binaryItemIDRef") {
                let bin_id = digit_prefix(&id_str).unwrap_or(0);
                // For self-closing <hp:pic>, attach to the innermost open
                // paragraph builder.
                if let Some(pb) = para_stack.last_mut() {
                    pb.image_refs.push(ImageRef { bin_id });
                }
            }
        }
        b"equation" => {
            if let Some(s) = get_attr(e, b"script") {
                if let Some(pb) = para_stack.last_mut() {
                    pb.equation = Some(s);
                }
            }
        }
        _ => {}
    }
}

#[allow(clippy::too_many_arguments)]
fn handle_end(
    e: &BytesEnd,
    para_stack: &mut Vec<ParaBuilder>,
    context_stack: &mut Vec<ParaContext>,
    table_stack: &mut Vec<TableBuilder>,
    note_stack: &mut Vec<NoteFrame>,
    in_equation_script: &mut bool,
    cur_equation_script: &mut String,
    paragraphs: &mut Vec<String>,
    paragraph_details: &mut Vec<ParagraphDetail>,
    structure: &mut Vec<StructureNode>,
    tables: &mut Vec<Table>,
    section_index: usize,
) {
    let local_owned = e.name().local_name().as_ref().to_vec();
    let local = local_owned.as_slice();
    match local {
        b"t" => {
            if let Some(pb) = para_stack.last_mut() {
                pb.in_t_element = false;
            }
        }
        b"run" => {
            if let Some(pb) = para_stack.last_mut() {
                pb.current_run_char_id = None;
            }
        }
        b"script" => {
            *in_equation_script = false;
            if !cur_equation_script.is_empty() {
                if let Some(pb) = top_level_para(para_stack, context_stack) {
                    if pb.equation.is_none() {
                        pb.equation = Some(std::mem::take(cur_equation_script));
                    } else {
                        cur_equation_script.clear();
                    }
                } else {
                    cur_equation_script.clear();
                }
            }
        }
        b"footNote" | b"endNote" => {
            if let Some(frame) = note_stack.pop() {
                let text = frame.paragraphs.join("\n");
                if let Some(pb) = top_level_para(para_stack, context_stack) {
                    pb.footnotes.push(FootnoteBody {
                        kind: frame.kind,
                        text,
                    });
                }
            }
        }
        b"p" => {
            if let Some(pb) = para_stack.pop() {
                let ctx = context_stack.pop().unwrap_or(ParaContext::TopLevel);
                match ctx {
                    ParaContext::TopLevel => {
                        let text = pb.text.clone();
                        let kind = if pb.contains_table {
                            NodeKind::Table
                        } else {
                            NodeKind::Paragraph
                        };
                        let preview = make_preview(&text);
                        let id = format!("{}:{}", section_index, paragraphs.len());
                        paragraphs.push(text.clone());
                        paragraph_details.push(ParagraphDetail {
                            text,
                            para_shape_id: pb.para_shape_id,
                            runs: pb.runs,
                            footnotes: pb.footnotes,
                            equation: pb.equation,
                            image_refs: pb.image_refs,
                        });
                        structure.push(StructureNode {
                            id,
                            kind,
                            preview,
                            ctrl_id: None,
                        });
                    }
                    ParaContext::Cell => {
                        if let Some(tb) = table_stack.last_mut() {
                            tb.cur_cell_paragraphs.push(pb.text);
                        }
                    }
                    ParaContext::Note => {
                        if let Some(frame) = note_stack.last_mut() {
                            frame.paragraphs.push(pb.text);
                        }
                    }
                }
            }
        }
        b"tc" => {
            if let Some(tb) = table_stack.last_mut() {
                let row = tb.cur_cell_row as usize;
                let col = tb.cur_cell_col as usize;
                let paragraphs = std::mem::take(&mut tb.cur_cell_paragraphs);
                let text = paragraphs.join("\n");
                let cell = Cell {
                    col: tb.cur_cell_col,
                    row: tb.cur_cell_row,
                    col_span: tb.cur_cell_col_span,
                    row_span: tb.cur_cell_row_span,
                    text,
                    paragraphs,
                };
                if row < tb.cells.len() && col < tb.cells[row].len() {
                    tb.cells[row][col] = Some(cell);
                }
            }
        }
        b"tbl" => {
            if let Some(tb) = table_stack.pop() {
                let id = format!("{}:{}", section_index, paragraphs.len());
                tables.push(Table {
                    id,
                    rows: tb.rows,
                    cols: tb.cols,
                    caption: None,
                    cells: tb.cells,
                });
            }
        }
        _ => {}
    }
}

fn get_attr(e: &BytesStart, key: &[u8]) -> Option<String> {
    for a in e.attributes().flatten() {
        if a.key.as_ref() == key || a.key.local_name().as_ref() == key {
            if let Ok(v) = a.unescape_value() {
                return Some(v.into_owned());
            }
        }
    }
    None
}

fn get_attr_u32(e: &BytesStart, key: &[u8]) -> Option<u32> {
    get_attr(e, key).and_then(|s| s.parse::<u32>().ok())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn captures_footnote_in_section_xml() {
        let xml = r#"<?xml version="1.0"?>
<hs:sec xmlns:hp="http://www.hancom.co.kr/hwpml/2011/paragraph"
        xmlns:hs="http://www.hancom.co.kr/hwpml/2011/section">
  <hp:p paraPrIDRef="0">
    <hp:run charPrIDRef="0"><hp:t>main text</hp:t></hp:run>
    <hp:footNote>
      <hp:subList>
        <hp:p paraPrIDRef="1"><hp:run charPrIDRef="0"><hp:t>footnote body</hp:t></hp:run></hp:p>
      </hp:subList>
    </hp:footNote>
  </hp:p>
</hs:sec>"#;
        let out = parse(xml.as_bytes(), 0).expect("parse");
        let details = &out.section.paragraph_details;
        assert_eq!(details.len(), 1);
        assert_eq!(out.section.paragraphs[0], "main text");
        assert_eq!(details[0].footnotes.len(), 1);
        assert_eq!(details[0].footnotes[0].kind, "footnote");
        assert_eq!(details[0].footnotes[0].text, "footnote body");
    }

    #[test]
    fn captures_equation_script_attribute() {
        let xml = r#"<?xml version="1.0"?>
<hs:sec xmlns:hp="http://www.hancom.co.kr/hwpml/2011/paragraph"
        xmlns:hs="http://www.hancom.co.kr/hwpml/2011/section">
  <hp:p paraPrIDRef="0">
    <hp:run charPrIDRef="0">
      <hp:equation script="x=1"/>
      <hp:t>hello</hp:t>
    </hp:run>
  </hp:p>
</hs:sec>"#;
        let out = parse(xml.as_bytes(), 0).expect("parse");
        let d = &out.section.paragraph_details[0];
        assert_eq!(d.equation.as_deref(), Some("x=1"));
        assert_eq!(d.text, "hello");
    }

    #[test]
    fn captures_pic_bin_id() {
        let xml = r#"<?xml version="1.0"?>
<hs:sec xmlns:hp="http://www.hancom.co.kr/hwpml/2011/paragraph"
        xmlns:hs="http://www.hancom.co.kr/hwpml/2011/section">
  <hp:p paraPrIDRef="0">
    <hp:run charPrIDRef="0">
      <hp:pic binaryItemIDRef="image3"/>
      <hp:t>caption</hp:t>
    </hp:run>
  </hp:p>
</hs:sec>"#;
        let out = parse(xml.as_bytes(), 0).expect("parse");
        let d = &out.section.paragraph_details[0];
        assert_eq!(d.image_refs.len(), 1);
        assert_eq!(d.image_refs[0].bin_id, 3);
        assert_eq!(d.text, "caption");
    }
}