xberg 1.1.2

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
//! RTF control-word dispatch used by [`super::text_extract::extract_text_from_rtf`].

use super::{FormattingTracker, ParagraphMeta};
use crate::extractors::rtf::images::{RtfImage, extract_pict_image};
use crate::extractors::rtf::tables::TableState;
use crate::types::Table;

/// Bundled mutable parser state threaded through control-word handling.
///
/// Groups the many independent pieces of state a single RTF control word may
/// read or update into one parameter, replacing a long positional argument
/// list (`handle_control_word` previously took 27). `chars`, `control_word`,
/// and `param` stay as direct arguments since every handler needs them
/// positionally rather than as shared state. ~keep
pub(super) struct ControlWordCtx<'a> {
    pub(super) result: &'a mut String,
    pub(super) table_state: &'a mut Option<TableState>,
    pub(super) tables: &'a mut Vec<Table>,
    pub(super) images: &'a mut Vec<RtfImage>,
    pub(super) ensure_table: &'a dyn Fn(&mut Option<TableState>),
    pub(super) finalize_table: &'a dyn Fn(&mut Option<TableState>, &mut Vec<Table>),
    pub(super) plain: bool,
    pub(super) group_has_text: &'a mut [bool],
    pub(super) cur_heading_level: &'a mut u8,
    pub(super) cur_list_level: &'a mut Option<u8>,
    pub(super) cur_list_id: &'a mut Option<u16>,
    pub(super) cur_ordered: &'a mut bool,
    pub(super) para_metas: &'a mut Vec<ParagraphMeta>,
    pub(super) para_meta_emitted: &'a mut bool,
    pub(super) uc_stack: &'a mut Vec<u8>,
    pub(super) ansi_codepage_stack: &'a mut [u32],
    pub(super) footnote_count: &'a mut usize,
    pub(super) pending_boundary_space: &'a mut bool,
    pub(super) hidden_stack: &'a mut Vec<bool>,
    pub(super) fmt_tracker: &'a mut FormattingTracker,
    pub(super) font_id_stack: &'a mut [Option<u16>],
    pub(super) default_font_id: &'a mut Option<u16>,
}

/// Dispatch an RTF control word to its handler.
///
/// Split into three category dispatchers below -- scope/state, text
/// emission, and table/formatting -- purely to keep each match's arm count
/// (and this function's own length) within the repository's limits. Control
/// words are disjoint string literals, so which group checks first cannot
/// change which arm ultimately handles a given word. ~keep
pub(super) fn handle_control_word(
    control_word: &str,
    param: Option<i32>,
    chars: &mut std::iter::Peekable<std::str::Chars>,
    ctx: &mut ControlWordCtx,
) {
    if handle_scope_control_word(control_word, param, ctx) {
        return;
    }
    if handle_text_emission_control_word(control_word, param, chars, ctx) {
        return;
    }
    handle_table_or_formatting_control_word(control_word, param, ctx);
}

/// Handle font/codepage/paragraph-scope control words. Returns `false` for
/// any word it does not recognize, leaving it for the next dispatcher.
fn handle_scope_control_word(control_word: &str, param: Option<i32>, ctx: &mut ControlWordCtx) -> bool {
    match control_word {
        "f" => {
            if let Some(val) = param
                && let Some(font_id) = ctx.font_id_stack.last_mut()
            {
                *font_id = Some(val.max(0) as u16);
            }
        }
        "deff" => {
            if let Some(val) = param {
                *ctx.default_font_id = Some(val.max(0) as u16);
            }
        }
        "v" => {
            let hidden = param.unwrap_or(1) != 0;
            if let Some(h) = ctx.hidden_stack.last_mut() {
                *h = hidden;
            }
        }
        "pard" => handle_pard_control_word(ctx),
        "outlinelevel" => {
            if let Some(level) = param {
                *ctx.cur_heading_level = (level as u8) + 1;
            }
        }
        "ilvl" => {
            *ctx.cur_list_level = Some(param.unwrap_or(0) as u8);
        }
        "ls" => {
            *ctx.cur_list_id = Some(param.unwrap_or(0) as u16);
        }
        "uc" => {
            if let Some(val) = param
                && let Some(uc) = ctx.uc_stack.last_mut()
            {
                *uc = val.max(0) as u8;
            }
        }
        "ansicpg" => {
            if let Some(val) = param
                && val > 0
                && let Some(codepage) = ctx.ansi_codepage_stack.last_mut()
            {
                *codepage = val as u32;
            }
        }
        _ => return false,
    }
    true
}

/// Handle control words that emit text into the output (Unicode escapes,
/// images, paragraph/line/tab breaks, fixed-text literals). Returns `false`
/// for any word it does not recognize, leaving it for the next dispatcher.
fn handle_text_emission_control_word(
    control_word: &str,
    param: Option<i32>,
    chars: &mut std::iter::Peekable<std::str::Chars>,
    ctx: &mut ControlWordCtx,
) -> bool {
    match control_word {
        "u" => handle_u_control_word(param, chars, ctx),
        "chftn" => {
            *ctx.footnote_count += 1;
            let marker = format!("[^{}]", *ctx.footnote_count);
            if let Some(state) = ctx.table_state.as_mut()
                && state.in_row
            {
                state.current_cell.push_str(&marker);
            } else {
                ctx.result.push_str(&marker);
                if let Some(flag) = ctx.group_has_text.last_mut() {
                    *flag = true;
                }
            }
        }
        "pict" => handle_pict_control_word(chars, ctx),
        "par" | "line" => handle_par_or_line(ctx),
        "tab" => {
            if let Some(state) = ctx.table_state.as_mut()
                && state.in_row
            {
                state.current_cell.push('\t');
            } else {
                ctx.result.push('\t');
                if let Some(flag) = ctx.group_has_text.last_mut() {
                    *flag = true;
                }
            }
        }
        "bullet" | "lquote" | "rquote" | "ldblquote" | "rdblquote" | "endash" | "emdash" => {
            if let Some(c) = literal_char_for_control_word(control_word) {
                push_literal_char(ctx, c);
            }
        }
        _ => return false,
    }
    true
}

/// Handle table-structure and character-formatting control words. Any word
/// unrecognized here (or by the two dispatchers above) is a no-op, matching
/// the original single match's `_ => {}` fallback.
fn handle_table_or_formatting_control_word(control_word: &str, param: Option<i32>, ctx: &mut ControlWordCtx) {
    match control_word {
        "trowd" => {
            (ctx.ensure_table)(ctx.table_state);
            if let Some(state) = ctx.table_state.as_mut() {
                state.start_row();
            }
        }
        "cell" => {
            if let Some(state) = ctx.table_state.as_mut()
                && state.in_row
            {
                state.push_cell();
            }
        }
        "row" => handle_row_control_word(ctx),
        "intbl" => {
            (ctx.ensure_table)(ctx.table_state);
            if let Some(state) = ctx.table_state.as_mut()
                && !state.in_row
            {
                state.start_row();
            }
        }
        "b" => {
            ctx.fmt_tracker.update_bold(ctx.result.len(), param.unwrap_or(1) != 0);
        }
        "i" => {
            ctx.fmt_tracker.update_italic(ctx.result.len(), param.unwrap_or(1) != 0);
        }
        "ul" => {
            ctx.fmt_tracker
                .update_underline(ctx.result.len(), param.unwrap_or(1) != 0);
        }
        "ulnone" => {
            ctx.fmt_tracker.update_underline(ctx.result.len(), false);
        }
        "strike" => {
            ctx.fmt_tracker
                .update_strikethrough(ctx.result.len(), param.unwrap_or(1) != 0);
        }
        "cf" => {
            ctx.fmt_tracker
                .update_color(ctx.result.len(), param.unwrap_or(0) as u16);
        }
        "plain" => {
            if let Some(h) = ctx.hidden_stack.last_mut() {
                *h = false;
            }
            ctx.fmt_tracker.reset_all(ctx.result.len());
        }
        _ => {}
    }
}

/// Map a fixed-text control word to the literal character it emits.
///
/// Only called for the seven words already matched by the caller's combined
/// arm, so `None` cannot actually occur there -- returning `Option` rather
/// than panicking keeps this helper safe to call on its own. ~keep
fn literal_char_for_control_word(word: &str) -> Option<char> {
    Some(match word {
        "bullet" => '\u{2022}',
        "lquote" => '\u{2018}',
        "rquote" => '\u{2019}',
        "ldblquote" => '\u{201C}',
        "rdblquote" => '\u{201D}',
        "endash" => '\u{2013}',
        "emdash" => '\u{2014}',
        _ => return None,
    })
}

/// Push a single literal character produced by a fixed-text control word
/// (`\bullet`, `\lquote`, etc.), matching the char-append + group-has-text
/// bookkeeping every such control word performs. ~keep
fn push_literal_char(ctx: &mut ControlWordCtx, ch: char) {
    ctx.result.push(ch);
    if let Some(flag) = ctx.group_has_text.last_mut() {
        *flag = true;
    }
}

/// Handle `\pard`: close the current paragraph and reset per-paragraph state.
fn handle_pard_control_word(ctx: &mut ControlWordCtx) {
    let in_table_row = ctx.table_state.as_ref().is_some_and(|s| s.in_row);
    if !in_table_row && !ctx.result.is_empty() && !ctx.result.ends_with('\n') && !*ctx.para_meta_emitted {
        ctx.para_metas.push(ParagraphMeta {
            heading_level: *ctx.cur_heading_level,
            list_level: *ctx.cur_list_level,
            list_id: *ctx.cur_list_id,
            is_table: false,
            ordered: *ctx.cur_ordered,
        });
        ctx.result.push('\n');
        ctx.result.push('\n');
        if let Some(flag) = ctx.group_has_text.last_mut() {
            *flag = true;
        }
    }
    *ctx.para_meta_emitted = false;
    *ctx.cur_heading_level = 0;
    *ctx.cur_list_level = None;
    *ctx.cur_list_id = None;
    *ctx.cur_ordered = false;
}

/// Handle `\uN`: emit the Unicode fallback character and skip the following
/// `\ucN`-controlled number of fallback characters (or `\'hh` escapes).
fn handle_u_control_word(
    param: Option<i32>,
    chars: &mut std::iter::Peekable<std::str::Chars>,
    ctx: &mut ControlWordCtx,
) {
    let Some(code_num) = param else { return };
    let code_u = if code_num < 0 {
        (code_num + 65536) as u32
    } else {
        code_num as u32
    };
    if let Some(c) = char::from_u32(code_u) {
        if let Some(state) = ctx.table_state.as_mut()
            && state.in_row
        {
            state.current_cell.push(c);
        } else {
            if *ctx.pending_boundary_space
                && !ctx.result.is_empty()
                && !ctx.result.ends_with(' ')
                && !ctx.result.ends_with('\n')
            {
                ctx.result.push(' ');
            }
            *ctx.pending_boundary_space = false;
            ctx.result.push(c);
            if let Some(flag) = ctx.group_has_text.last_mut() {
                *flag = true;
            }
        }
    }
    let uc_count = ctx.uc_stack.last().copied().unwrap_or(1);
    let mut skipped = 0u8;
    while skipped < uc_count {
        let Some(&next) = chars.peek() else { break };
        if next == '{' || next == '}' {
            break;
        }
        if next != '\\' {
            chars.next();
            skipped += 1;
            continue;
        }
        chars.next();
        let Some(&apos) = chars.peek() else { break };
        if apos != '\'' {
            break;
        }
        chars.next();
        chars.next();
        chars.next();
        skipped += 1;
    }
}

/// Handle `\pict`: extract image metadata/bytes and emit a markdown image
/// placeholder unless plain-text output was requested.
fn handle_pict_control_word(chars: &mut std::iter::Peekable<std::str::Chars>, ctx: &mut ControlWordCtx) {
    let (image_metadata, rtf_image) = extract_pict_image(chars);
    if let Some(img) = rtf_image {
        ctx.images.push(img);
    }
    if !image_metadata.is_empty() && !ctx.plain {
        let img_md = format!("![image]({image_metadata}) ");
        if let Some(state) = ctx.table_state.as_mut()
            && state.in_row
        {
            state.current_cell.push_str(&img_md);
        } else {
            if let Some(flag) = ctx.group_has_text.last_mut() {
                *flag = true;
            }
            ctx.result.push_str(&img_md);
        }
    }
}

/// Handle `\par`/`\line`: close the current cell/paragraph as appropriate.
fn handle_par_or_line(ctx: &mut ControlWordCtx) {
    *ctx.pending_boundary_space = false;
    let in_table_row = ctx.table_state.as_ref().is_some_and(|s| s.in_row);
    if in_table_row {
        if let Some(state) = ctx.table_state.as_mut()
            && !state.current_cell.is_empty()
            && !state.current_cell.ends_with(' ')
        {
            state.current_cell.push(' ');
        }
        return;
    }
    let still_in_table = ctx.table_state.as_ref().is_some_and(|s| s.expecting_next_row);
    if ctx.table_state.is_some() && !still_in_table {
        (ctx.finalize_table)(ctx.table_state, ctx.tables);
    }
    if !ctx.result.is_empty() && !ctx.result.ends_with('\n') {
        if !*ctx.para_meta_emitted {
            ctx.para_metas.push(ParagraphMeta {
                heading_level: *ctx.cur_heading_level,
                list_level: *ctx.cur_list_level,
                list_id: *ctx.cur_list_id,
                is_table: false,
                ordered: *ctx.cur_ordered,
            });
            *ctx.para_meta_emitted = true;
        }
        ctx.result.push('\n');
        ctx.result.push('\n');
    }
    if let Some(flag) = ctx.group_has_text.last_mut() {
        *flag = true;
    }
}

/// Handle `\row`: close the current table row and emit its text placeholder.
fn handle_row_control_word(ctx: &mut ControlWordCtx) {
    (ctx.ensure_table)(ctx.table_state);
    if let Some(state) = ctx.table_state.as_mut()
        && (state.in_row || !state.current_cell.is_empty())
    {
        state.push_row();
    }
    if !ctx.result.is_empty() && !ctx.result.ends_with('\n') {
        ctx.result.push('\n');
        ctx.result.push('\n');
    }
    ctx.result.push_str("[TABLE_ROW]");
    ctx.result.push('\n');
    ctx.result.push('\n');
    if let Some(flag) = ctx.group_has_text.last_mut() {
        *flag = true;
    }
    *ctx.para_meta_emitted = true;
    ctx.para_metas.push(ParagraphMeta {
        is_table: true,
        ..Default::default()
    });
}