xberg 1.3.7

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! WordPerfect (.wpd) extractor.
//!
//! Extracts text content from WordPerfect documents by walking the ordered,
//! properly-nested [`WpdEvent`] stream produced by the vendored `libwpd`
//! shim in `xberg_libwpd`.

use crate::Result;
use crate::core::config::ExtractionConfig;
use crate::plugins::{InternalDocumentExtractor, Plugin};
use crate::types::Metadata;
use crate::types::document_structure::{AnnotationKind, ContentLayer, TextAnnotation};
use crate::types::internal::InternalDocument;
use crate::types::internal_builder::InternalDocumentBuilder;
use ahash::AHashMap;
use async_trait::async_trait;
use xberg_libwpd::{WpdDocument, WpdEvent, WpdMetadata};

#[cfg_attr(alef, alef(skip))]
/// Extractor for WordPerfect (.wpd) documents.
///
/// Supports WordPerfect 5.x/6.x (Windows) and Mac WordPerfect documents via
/// the vendored `libwpd`/`librevenge` shim in `xberg_libwpd`.
pub struct WordPerfectExtractor;

impl WordPerfectExtractor {
    pub(crate) fn new() -> Self {
        Self
    }
}

impl Default for WordPerfectExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl Plugin for WordPerfectExtractor {
    fn name(&self) -> &str {
        "wordperfect-extractor"
    }

    fn version(&self) -> String {
        env!("CARGO_PKG_VERSION").to_string()
    }

    fn initialize(&self) -> Result<()> {
        Ok(())
    }

    fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    fn description(&self) -> &str {
        "WordPerfect (.wpd) text extraction"
    }

    fn author(&self) -> &str {
        "Xberg Team"
    }
}

/// The kinds of inline spans tracked while walking the event stream. Carries
/// no payload beyond `Link`'s href; span matching on close compares variant
/// discriminant only (a document is expected to close spans in a way that
/// makes the href irrelevant for matching).
#[derive(Clone)]
enum SpanKind {
    Bold,
    Italic,
    Underline,
    Strikethrough,
    Superscript,
    Subscript,
    Link(String),
}

/// A single open inline span awaiting its matching `*End` event.
struct OpenSpan {
    start: u32,
    kind: SpanKind,
}

/// Accumulates plain text plus in-progress inline spans for one logical unit
/// of content (a paragraph, a table cell, or a diverted body such as a
/// header/footer/note/aside).
#[derive(Default)]
struct Ctx {
    text: String,
    spans: Vec<OpenSpan>,
    annotations: Vec<TextAnnotation>,
}

impl Ctx {
    fn append(&mut self, s: &str) {
        self.text.push_str(s);
    }

    fn open_span(&mut self, kind: SpanKind) {
        let start = self.text.len() as u32;
        self.spans.push(OpenSpan { start, kind });
    }

    /// Record an annotation for `open`, covering the range from where it opened
    /// to the current text end. Empty ranges are dropped.
    fn record_span(&mut self, open: OpenSpan) {
        let end = self.text.len() as u32;
        if end > open.start {
            self.annotations.push(TextAnnotation {
                start: open.start,
                end,
                kind: span_to_annotation(open.kind),
            });
        }
    }

    /// Close the innermost open span matching `kind`'s discriminant, recording
    /// an annotation over the byte range it covered (empty ranges are dropped).
    fn close_span(&mut self, kind: &SpanKind) {
        let target = std::mem::discriminant(kind);
        if let Some(pos) = self
            .spans
            .iter()
            .rposition(|s| std::mem::discriminant(&s.kind) == target)
        {
            let open = self.spans.remove(pos);
            self.record_span(open);
        }
    }

    /// Close every still-open span at the current text end, recording each as an
    /// annotation. Unlike a bare `spans.clear()`, this preserves the formatting
    /// of text accumulated so far when a boundary (paragraph flush, note anchor,
    /// cell/diversion close) arrives before a span's own `*End` event.
    fn close_open_spans(&mut self) {
        for open in std::mem::take(&mut self.spans) {
            self.record_span(open);
        }
    }

    /// Drain this context's text and annotations, merging adjacent same-kind
    /// annotations so consecutive runs of identical formatting don't produce
    /// back-to-back annotation fragments (mirrors the DOCX extractor). Any span
    /// still open at drain time is closed (recorded) rather than discarded.
    fn take(&mut self) -> (String, Vec<TextAnnotation>) {
        self.close_open_spans();
        let text = std::mem::take(&mut self.text);
        let mut annotations = std::mem::take(&mut self.annotations);
        merge_adjacent_annotations(&mut annotations);
        (text, annotations)
    }
}

/// Map an inline span to its `InternalDocument` annotation kind. Built directly
/// rather than via `types::builder`'s annotation helpers: those are gated behind
/// `office`/`email`/`xml`, none of which `wordperfect` implies.
fn span_to_annotation(kind: SpanKind) -> AnnotationKind {
    match kind {
        SpanKind::Bold => AnnotationKind::Bold,
        SpanKind::Italic => AnnotationKind::Italic,
        SpanKind::Underline => AnnotationKind::Underline,
        SpanKind::Strikethrough => AnnotationKind::Strikethrough,
        SpanKind::Superscript => AnnotationKind::Superscript,
        SpanKind::Subscript => AnnotationKind::Subscript,
        SpanKind::Link(href) => AnnotationKind::Link { url: href, title: None },
    }
}

/// Upper bound on a table cell's reported column span. libwpd only clamps the
/// span to a minimum of 1; an implausibly large value (from a corrupted
/// table-definition field) would otherwise drive an unbounded clone loop in
/// `end_cell`. No real table spans more columns than this.
const MAX_TABLE_COL_SPAN: u32 = 4096;

/// Why a diverted body (header/footer/note/aside) was opened, and what to do
/// with its accumulated text when the matching `*End` event closes it.
enum DiversionKind {
    Header,
    Footer,
    /// `key` is the footnote/endnote anchor key shared with the `FootnoteRef`
    /// already pushed at the anchor point.
    Note {
        key: String,
    },
    /// A footnote/endnote anchored inside a table cell or another diversion,
    /// where a standalone `FootnoteRef`/`FootnoteDefinition` element would land
    /// out of reading order (cells and diverted bodies are pushed to the builder
    /// as deferred single units). Its body is folded back inline into the
    /// surrounding text when it closes.
    InlineNote,
    /// `kind` is librevenge's aside kind, `"comment"` or `"box"`.
    Aside {
        kind: String,
    },
}

struct Diversion {
    kind: DiversionKind,
    ctx: Ctx,
}

/// Accumulates rows/cells for a table currently being walked. Column
/// placement is purely positional (sequential push per row), matching the
/// DOCX extractor's approach rather than trusting `CellStart.column`, which
/// libwpd may report as `-1`. `CoveredCell` positions (merged-away grid
/// slots from row/column spans) are filled in from the same column position
/// in the previous row when available, so a vertically merged cell's content
/// still appears in every row it visually covers.
#[derive(Default)]
struct TableBuilder {
    rows: Vec<Vec<String>>,
    current_row: Vec<String>,
    current_row_is_header: bool,
    header_row_indices: Vec<usize>,
    in_cell: bool,
    cell_ctx: Ctx,
    cell_col_span: u32,
}

impl TableBuilder {
    fn start_row(&mut self, header: bool) {
        self.current_row = Vec::new();
        self.current_row_is_header = header;
    }

    fn start_cell(&mut self, col_span: u32) {
        self.in_cell = true;
        self.cell_ctx = Ctx::default();
        self.cell_col_span = col_span.clamp(1, MAX_TABLE_COL_SPAN);
    }

    fn end_cell(&mut self) {
        let (text, annotations) = self.cell_ctx.take();
        // Bake inline formatting into the cell text as markdown (mirrors the
        // DOCX extractor, which renders cell runs to markdown) rather than
        // dropping it: `push_table_from_cells` takes flat strings.
        let mut rendered = annotations_to_markdown(&text, &annotations);
        for i in 0..self.cell_col_span {
            if i + 1 == self.cell_col_span {
                self.current_row.push(std::mem::take(&mut rendered));
            } else {
                self.current_row.push(rendered.clone());
            }
        }
        self.in_cell = false;
    }

    fn covered_cell(&mut self) {
        let col_idx = self.current_row.len();
        let filler = self
            .rows
            .last()
            .and_then(|r| r.get(col_idx))
            .cloned()
            .unwrap_or_default();
        self.current_row.push(filler);
    }

    fn end_row(&mut self) {
        let row = std::mem::take(&mut self.current_row);
        if self.current_row_is_header {
            self.header_row_indices.push(self.rows.len());
        }
        self.rows.push(row);
    }
}

/// Walks a `WpdDocument`'s event stream and pushes elements into an
/// `InternalDocumentBuilder`. See the module doc for the overall mapping.
struct WpdWalker<'a> {
    builder: &'a mut InternalDocumentBuilder,
    main: Ctx,
    pending_heading_level: Option<u8>,
    /// Ordered flag per currently open `push_list()` container, indexed by
    /// nesting depth - 1.
    list_stack: Vec<bool>,
    /// Set while inside a `ListItemStart..ListItemEnd` bracket, so a
    /// `ParagraphEnd` inside it flushes via `push_list_item` instead of
    /// `push_paragraph`.
    current_list_item_ordered: Option<bool>,
    table: Option<TableBuilder>,
    /// Nesting depth of tables opened inside a cell of `table`. WordPerfect
    /// permits cell-nested tables; rather than clobber the outer `TableBuilder`
    /// (losing it entirely), inner table structure is ignored and its text
    /// flows into the enclosing cell. `> 0` means we are inside such an inner
    /// table.
    table_depth: u32,
    diversions: Vec<Diversion>,
    /// Shared counter for footnote and endnote anchor keys.
    note_counter: u32,
}

impl<'a> WpdWalker<'a> {
    fn new(builder: &'a mut InternalDocumentBuilder) -> Self {
        Self {
            builder,
            main: Ctx::default(),
            pending_heading_level: None,
            list_stack: Vec::new(),
            current_list_item_ordered: None,
            table: None,
            table_depth: 0,
            diversions: Vec::new(),
            note_counter: 0,
        }
    }

    fn in_cell(&self) -> bool {
        self.table.as_ref().is_some_and(|t| t.in_cell)
    }

    /// The table currently accepting row/cell events, or `None` when no table is
    /// open or we are inside a nested inner table (`table_depth > 0`), whose
    /// structure is folded into the enclosing cell rather than built separately.
    fn active_table(&mut self) -> Option<&mut TableBuilder> {
        if self.table_depth == 0 {
            self.table.as_mut()
        } else {
            None
        }
    }

    /// The context inline text/spans currently route into: a table cell if
    /// one is open, else the innermost open diversion, else the main
    /// document-body paragraph buffer.
    fn active_ctx(&mut self) -> &mut Ctx {
        if self.in_cell() {
            &mut self.table.as_mut().expect("in_cell implies table is Some").cell_ctx
        } else if let Some(diversion) = self.diversions.last_mut() {
            &mut diversion.ctx
        } else {
            &mut self.main
        }
    }

    fn append(&mut self, s: &str) {
        self.active_ctx().append(s);
    }

    fn open_span(&mut self, kind: SpanKind) {
        self.active_ctx().open_span(kind);
    }

    fn close_span(&mut self, kind: &SpanKind) {
        self.active_ctx().close_span(kind);
    }

    /// Handle `ParagraphEnd`. Inside a table cell or a diversion, a paragraph
    /// break is just a line break within the accumulating text (cells and
    /// diverted bodies are pushed as a single unit when they close). At the
    /// top level it flushes the current paragraph/heading/list-item.
    fn on_paragraph_end(&mut self) {
        if self.in_cell() || !self.diversions.is_empty() {
            self.active_ctx().append("\n");
            return;
        }
        self.flush_paragraph();
    }

    fn flush_paragraph(&mut self) {
        let (text, annotations) = self.main.take();
        if text.trim().is_empty() {
            self.pending_heading_level = None;
            return;
        }
        if let Some(level) = self.pending_heading_level.take() {
            let idx = self.builder.push_heading(level, &text, None, None);
            if !annotations.is_empty() {
                self.builder.set_annotations(idx, annotations);
            }
        } else if let Some(ordered) = self.current_list_item_ordered {
            self.builder.push_list_item(&text, ordered, annotations, None, None);
        } else {
            self.builder.push_paragraph(&text, annotations, None, None);
        }
    }

    fn enter_list_item(&mut self, ordered: bool, level: u8) {
        let level = level.max(1) as usize;
        while self.list_stack.len() > level {
            self.builder.end_list();
            self.list_stack.pop();
        }
        if self.list_stack.len() < level {
            while self.list_stack.len() < level {
                self.builder.push_list(ordered);
                self.list_stack.push(ordered);
            }
        } else if self.list_stack.last() != Some(&ordered) {
            self.builder.end_list();
            self.list_stack.pop();
            self.builder.push_list(ordered);
            self.list_stack.push(ordered);
        }
        self.current_list_item_ordered = Some(ordered);
    }

    fn exit_list_item(&mut self) {
        // Always drain `main` (flush_paragraph's own empty-text guard suppresses
        // an empty push): a whitespace-only item that still carries a recorded
        // annotation must not leak its stale text/annotation into the next
        // element.
        self.flush_paragraph();
        self.current_list_item_ordered = None;
    }

    fn enter_note(&mut self, endnote: bool) {
        self.note_counter += 1;
        // A note anchored inside a table cell or another diversion can't become
        // a standalone `FootnoteRef`/`FootnoteDefinition` element without
        // landing out of reading order (cells/diverted bodies are pushed as
        // deferred units). Fold it inline instead: an inline marker here, the
        // body appended back into the surrounding text at `NoteEnd`.
        if !self.diversions.is_empty() || self.in_cell() {
            let marker = format!("[{}]", self.note_counter);
            self.append(&marker);
            self.diversions.push(Diversion {
                kind: DiversionKind::InlineNote,
                ctx: Ctx::default(),
            });
            return;
        }

        // Top level: flush the pre-anchor body so the footnote-ref marker
        // element lands at the right point in reading order, then divert
        // subsequent events into the note body until `NoteEnd`. Footnotes and
        // endnotes share the same InternalDocument mechanism
        // (`FootnoteRef`/`FootnoteDefinition`) since there is no separate
        // endnote element kind; they are distinguished only by the anchor key
        // prefix ("fn" vs "en"), mirroring how the DOCX extractor chains
        // `doc.footnotes` and `doc.endnotes`. Spans still open across the anchor
        // are snapshotted and reopened so formatting continues after the note.
        let open_kinds: Vec<SpanKind> = self.main.spans.iter().map(|s| s.kind.clone()).collect();
        self.flush_paragraph();
        for kind in open_kinds {
            self.main.open_span(kind);
        }
        let prefix = if endnote { "en" } else { "fn" };
        let key = format!("{prefix}{}", self.note_counter);
        let marker = self.note_counter.to_string();
        self.builder.push_footnote_ref(&marker, &key, None);
        self.diversions.push(Diversion {
            kind: DiversionKind::Note { key },
            ctx: Ctx::default(),
        });
    }

    fn enter_diversion(&mut self, kind: DiversionKind) {
        self.diversions.push(Diversion {
            kind,
            ctx: Ctx::default(),
        });
    }

    fn exit_diversion(&mut self) {
        let Some(mut diversion) = self.diversions.pop() else {
            return;
        };
        let (text, annotations) = diversion.ctx.take();
        match diversion.kind {
            DiversionKind::Header => {
                if !text.trim().is_empty() {
                    let idx = self.builder.push_paragraph(&text, annotations, None, None);
                    self.builder.set_layer(idx, ContentLayer::Header);
                }
            }
            DiversionKind::Footer => {
                if !text.trim().is_empty() {
                    let idx = self.builder.push_paragraph(&text, annotations, None, None);
                    self.builder.set_layer(idx, ContentLayer::Footer);
                }
            }
            DiversionKind::Aside { kind } => {
                if !text.trim().is_empty() {
                    let idx = self.builder.push_paragraph(&text, annotations, None, None);
                    let mut attrs = AHashMap::new();
                    attrs.insert("aside_kind".to_string(), kind);
                    self.builder.set_attributes(idx, attrs);
                }
            }
            DiversionKind::Note { key } => {
                self.builder.push_footnote_definition(&text, &key, None);
            }
            DiversionKind::InlineNote => {
                // Fold the note body back into the surrounding text (the parent
                // context, now active again after the pop above). Annotations
                // are dropped here — this is a rare nested-note fallback.
                let body = text.trim();
                if !body.is_empty() {
                    self.active_ctx().append(" ");
                    self.active_ctx().append(body);
                }
            }
        }
    }

    fn walk(&mut self, events: &[WpdEvent]) {
        for event in events {
            if self.handle_table_event(event) {
                continue;
            }
            match event {
                WpdEvent::Text(s) => self.append(s),
                WpdEvent::Tab => self.append("\t"),
                WpdEvent::Space => self.append(" "),
                WpdEvent::LineBreak => self.append("\n"),
                WpdEvent::Field(s) => self.append(s),
                WpdEvent::ParagraphEnd => self.on_paragraph_end(),
                WpdEvent::HeadingStart { level } => self.pending_heading_level = Some(*level),
                WpdEvent::BoldStart => self.open_span(SpanKind::Bold),
                WpdEvent::BoldEnd => self.close_span(&SpanKind::Bold),
                WpdEvent::ItalicStart => self.open_span(SpanKind::Italic),
                WpdEvent::ItalicEnd => self.close_span(&SpanKind::Italic),
                WpdEvent::UnderlineStart => self.open_span(SpanKind::Underline),
                WpdEvent::UnderlineEnd => self.close_span(&SpanKind::Underline),
                WpdEvent::StrikethroughStart => self.open_span(SpanKind::Strikethrough),
                WpdEvent::StrikethroughEnd => self.close_span(&SpanKind::Strikethrough),
                WpdEvent::SuperscriptStart => self.open_span(SpanKind::Superscript),
                WpdEvent::SuperscriptEnd => self.close_span(&SpanKind::Superscript),
                WpdEvent::SubscriptStart => self.open_span(SpanKind::Subscript),
                WpdEvent::SubscriptEnd => self.close_span(&SpanKind::Subscript),
                WpdEvent::LinkStart { href } => self.open_span(SpanKind::Link(href.clone())),
                WpdEvent::LinkEnd => self.close_span(&SpanKind::Link(String::new())),
                WpdEvent::ListItemStart { ordered, level, .. } => self.enter_list_item(*ordered, *level),
                WpdEvent::ListItemEnd => self.exit_list_item(),
                WpdEvent::HeaderStart => self.enter_diversion(DiversionKind::Header),
                WpdEvent::HeaderEnd => self.exit_diversion(),
                WpdEvent::FooterStart => self.enter_diversion(DiversionKind::Footer),
                WpdEvent::FooterEnd => self.exit_diversion(),
                WpdEvent::NoteStart { endnote } => self.enter_note(*endnote),
                WpdEvent::NoteEnd => self.exit_diversion(),
                WpdEvent::AsideStart { kind } => self.enter_diversion(DiversionKind::Aside { kind: kind.clone() }),
                WpdEvent::AsideEnd => self.exit_diversion(),
                WpdEvent::TableStart
                | WpdEvent::RowStart { .. }
                | WpdEvent::CellStart { .. }
                | WpdEvent::CoveredCell { .. }
                | WpdEvent::CellEnd
                | WpdEvent::RowEnd
                | WpdEvent::TableEnd => {
                    unreachable!("table events are intercepted by handle_table_event before this match")
                }
            }
        }

        self.flush_paragraph();
        while self.list_stack.pop().is_some() {
            self.builder.end_list();
        }
    }

    /// Handles the table-structure events (`TableStart`..`TableEnd`), returning `true` if
    /// `event` was one of them. Split out of [`Self::walk`] purely to shorten that method.
    fn handle_table_event(&mut self, event: &WpdEvent) -> bool {
        match event {
            WpdEvent::TableStart => {
                if self.table.is_some() {
                    // Table nested inside an open cell: keep the outer table,
                    // let this inner table's text fold into the enclosing cell.
                    self.table_depth += 1;
                } else {
                    self.table = Some(TableBuilder::default());
                }
            }
            WpdEvent::RowStart { header } => {
                let header = *header;
                if let Some(t) = self.active_table() {
                    t.start_row(header);
                }
            }
            WpdEvent::CellStart { col_span, .. } => {
                let col_span = *col_span;
                if let Some(t) = self.active_table() {
                    t.start_cell(col_span);
                }
            }
            WpdEvent::CoveredCell { .. } => {
                if let Some(t) = self.active_table() {
                    t.covered_cell();
                }
            }
            WpdEvent::CellEnd => {
                if let Some(t) = self.active_table() {
                    t.end_cell();
                }
            }
            WpdEvent::RowEnd => {
                if let Some(t) = self.active_table() {
                    t.end_row();
                }
            }
            WpdEvent::TableEnd => {
                if self.table_depth > 0 {
                    self.table_depth -= 1;
                } else {
                    self.end_table();
                }
            }
            _ => return false,
        }
        true
    }

    fn end_table(&mut self) {
        let Some(t) = self.table.take() else {
            return;
        };
        if t.rows.is_empty() {
            return;
        }
        let idx = self.builder.push_table_from_cells(&t.rows, None, None);
        if !t.header_row_indices.is_empty() {
            let header_rows = t
                .header_row_indices
                .iter()
                .map(|i| i.to_string())
                .collect::<Vec<_>>()
                .join(",");
            let mut attrs = AHashMap::new();
            attrs.insert("header_rows".to_string(), header_rows);
            self.builder.set_attributes(idx, attrs);
        }
    }
}

/// Merge adjacent or overlapping annotations of the same kind so consecutive
/// spans of identical formatting collapse into one annotation instead of
/// several back-to-back fragments. Mirrors the DOCX extractor's
/// `merge_adjacent_annotations`.
fn merge_adjacent_annotations(annotations: &mut Vec<TextAnnotation>) {
    if annotations.len() < 2 {
        return;
    }

    fn same_kind_for_merge(a: &AnnotationKind, b: &AnnotationKind) -> bool {
        match (a, b) {
            (AnnotationKind::Bold, AnnotationKind::Bold)
            | (AnnotationKind::Italic, AnnotationKind::Italic)
            | (AnnotationKind::Underline, AnnotationKind::Underline)
            | (AnnotationKind::Strikethrough, AnnotationKind::Strikethrough)
            | (AnnotationKind::Subscript, AnnotationKind::Subscript)
            | (AnnotationKind::Superscript, AnnotationKind::Superscript) => true,
            (AnnotationKind::Link { url: url_a, .. }, AnnotationKind::Link { url: url_b, .. }) => url_a == url_b,
            _ => false,
        }
    }

    let kind_key = |kind: &AnnotationKind| -> u8 {
        match kind {
            AnnotationKind::Bold => 0,
            AnnotationKind::Italic => 1,
            AnnotationKind::Underline => 2,
            AnnotationKind::Strikethrough => 3,
            AnnotationKind::Subscript => 4,
            AnnotationKind::Superscript => 5,
            AnnotationKind::Link { .. } => 6,
            _ => 255,
        }
    };

    annotations.sort_by(|a, b| kind_key(&a.kind).cmp(&kind_key(&b.kind)).then(a.start.cmp(&b.start)));

    let mut merged = Vec::with_capacity(annotations.len());
    let mut i = 0;
    while i < annotations.len() {
        let mut ann = annotations[i].clone();
        let mut j = i + 1;
        while j < annotations.len()
            && same_kind_for_merge(&annotations[j].kind, &ann.kind)
            && annotations[j].start <= ann.end
        {
            ann.end = ann.end.max(annotations[j].end);
            j += 1;
        }
        merged.push(ann);
        i = j;
    }

    *annotations = merged;
}

/// The markdown open/close markers for an annotation kind, if it has an inline
/// markdown representation. Superscript/subscript and any non-inline kinds
/// return `None` and are rendered as plain text (they have no portable markdown
/// form inside a table cell).
fn markdown_markers(kind: &AnnotationKind) -> Option<(String, String)> {
    match kind {
        AnnotationKind::Bold => Some(("**".to_string(), "**".to_string())),
        AnnotationKind::Italic => Some(("*".to_string(), "*".to_string())),
        AnnotationKind::Strikethrough => Some(("~~".to_string(), "~~".to_string())),
        AnnotationKind::Link { url, .. } => Some(("[".to_string(), format!("]({url})"))),
        _ => None,
    }
}

/// Render `text` with its inline annotations baked in as markdown, for contexts
/// (table cells) that store flat strings rather than a separate annotation list.
/// Annotation ranges use byte offsets that always fall on `char` boundaries
/// (spans open/close only at append boundaries). Half-open `[start, end)`
/// ranges: at each boundary, closings for ranges ending there are emitted before
/// openings for ranges starting there.
fn annotations_to_markdown(text: &str, annotations: &[TextAnnotation]) -> String {
    if annotations.is_empty() {
        return text.to_string();
    }
    let mut result = String::with_capacity(text.len() + annotations.len() * 4);
    let emit_boundary = |result: &mut String, pos: u32| {
        for a in annotations {
            if a.end == pos
                && let Some((_, close)) = markdown_markers(&a.kind)
            {
                result.push_str(&close);
            }
        }
        for a in annotations {
            if a.start == pos
                && let Some((open, _)) = markdown_markers(&a.kind)
            {
                result.push_str(&open);
            }
        }
    };
    let mut end_pos = 0u32;
    for (byte, ch) in text.char_indices() {
        emit_boundary(&mut result, byte as u32);
        result.push(ch);
        end_pos = byte as u32 + ch.len_utf8() as u32;
    }
    emit_boundary(&mut result, end_pos);
    result
}

fn apply_metadata(builder: &mut InternalDocumentBuilder, meta: &WpdMetadata) {
    builder.set_metadata(Metadata {
        title: meta.title.clone(),
        subject: meta.subject.clone(),
        authors: meta.author.clone().map(|a| vec![a]),
        keywords: meta.keywords.clone().map(|k| vec![k]),
        ..Metadata::default()
    });
}

fn build_wordperfect_internal_document(doc: &WpdDocument) -> InternalDocument {
    let mut builder = InternalDocumentBuilder::new("wordperfect");
    apply_metadata(&mut builder, &doc.metadata);
    WpdWalker::new(&mut builder).walk(&doc.events);
    builder.build()
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
impl InternalDocumentExtractor for WordPerfectExtractor {
    async fn extract_content(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        let limits = config.security_limits.clone().unwrap_or_default();
        if content.len() > limits.max_content_size {
            return Err(crate::XbergError::validation(format!(
                "WordPerfect file exceeds size limit ({} > {} bytes)",
                content.len(),
                limits.max_content_size
            )));
        }
        if config.cancel_token.as_ref().map(|t| t.is_cancelled()).unwrap_or(false) {
            return Err(crate::XbergError::Cancelled);
        }

        // libwpd is a synchronous, non-yielding native C++ parser: run it on a
        // blocking thread so the async runtime's `extraction_timeout_secs`
        // timeout and worker-pool concurrency are not defeated by a slow or
        // pathological document (mirrors `doc.rs`/`docx.rs`).
        let doc = {
            #[cfg(feature = "tokio-runtime")]
            {
                let content_owned = content.to_vec();
                let span = tracing::Span::current();
                tokio::task::spawn_blocking(move || {
                    let _guard = span.entered();
                    xberg_libwpd::extract_document(&content_owned)
                })
                .await
                .map_err(|e| crate::XbergError::parsing(format!("WordPerfect extraction task failed: {e}")))?
            }
            #[cfg(not(feature = "tokio-runtime"))]
            {
                xberg_libwpd::extract_document(content)
            }
        }
        .map_err(|e| crate::XbergError::parsing(format!("Failed to read WordPerfect document: {e}")))?;

        let mut internal_doc = build_wordperfect_internal_document(&doc);
        if internal_doc.elements.is_empty() {
            return Err(crate::XbergError::parsing(
                "WordPerfect document produced no extractable content".to_string(),
            ));
        }
        internal_doc.mime_type = mime_type.to_string();
        Ok(internal_doc)
    }

    fn supported_mime_types(&self) -> &[&str] {
        &["application/vnd.wordperfect", "application/wordperfect"]
    }

    fn priority(&self) -> i32 {
        50
    }
}

#[cfg(test)]
mod tests;