ucp-translator-html 0.1.18

HTML to UCM document translator
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
//! HTML parser implementation.

use crate::error::{HtmlError, Result};
use scraper::{ElementRef, Html, Selector};
use ucm_core::{Block, BlockId, Content, Document, MediaSource};

/// Strategy for handling heading levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HeadingStrategy {
    /// Use actual heading levels from HTML (h1-h6)
    #[default]
    AsIs,
    /// Flatten all headings to a single level
    Flatten(usize),
    /// Infer hierarchy from DOM nesting depth
    InferFromNesting,
}

/// Configuration for HTML parsing
#[derive(Debug, Clone)]
pub struct HtmlParserConfig {
    /// Whether to preserve whitespace in text nodes
    pub preserve_whitespace: bool,
    /// Whether to extract images as media blocks
    pub extract_images: bool,
    /// Whether to extract links and store href in edges
    pub extract_links: bool,
    /// Strategy for handling heading levels
    pub heading_strategy: HeadingStrategy,
    /// Maximum nesting depth to process
    pub max_depth: usize,
    /// Maximum number of blocks to create
    pub max_blocks: usize,
    /// Minimum text length to create a block (filters noise)
    pub min_text_length: usize,
}

impl Default for HtmlParserConfig {
    fn default() -> Self {
        Self {
            preserve_whitespace: false,
            extract_images: true,
            extract_links: true,
            heading_strategy: HeadingStrategy::AsIs,
            max_depth: 50,
            max_blocks: 10000,
            min_text_length: 1,
        }
    }
}

/// HTML to UCM document parser
pub struct HtmlParser {
    config: HtmlParserConfig,
}

impl HtmlParser {
    /// Create a new parser with default configuration
    pub fn new() -> Self {
        Self {
            config: HtmlParserConfig::default(),
        }
    }

    /// Create a parser with custom configuration
    pub fn with_config(config: HtmlParserConfig) -> Self {
        Self { config }
    }

    /// Parse HTML string into a UCM Document
    pub fn parse(&self, html: &str) -> Result<Document> {
        let mut doc = Document::create();
        let root = doc.root;

        // Parse HTML
        let fragment = Html::parse_document(html);

        // Find body or use root element
        let body_selector = Selector::parse("body").unwrap();
        let body = fragment.select(&body_selector).next();

        if let Some(body_element) = body {
            self.process_children(&mut doc, &root, body_element, 0)?;
        } else {
            // No body tag, process entire document
            if let Some(root_element) = fragment.root_element().first_child() {
                if let Some(element) = ElementRef::wrap(root_element) {
                    self.process_children(&mut doc, &root, element, 0)?;
                }
            }
        }

        Ok(doc)
    }

    /// Process all children of an element
    fn process_children(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
        depth: usize,
    ) -> Result<()> {
        if depth > self.config.max_depth {
            return Err(HtmlError::ResourceLimit(format!(
                "Maximum nesting depth {} exceeded",
                self.config.max_depth
            )));
        }

        if doc.block_count() > self.config.max_blocks {
            return Err(HtmlError::ResourceLimit(format!(
                "Maximum block count {} exceeded",
                self.config.max_blocks
            )));
        }

        let mut current_heading_parent = *parent_id;
        let mut heading_stack: Vec<(usize, BlockId)> = vec![(0, *parent_id)];

        for child in element.children() {
            if let Some(child_element) = ElementRef::wrap(child) {
                let tag_name = child_element.value().name();

                // Handle headings specially for hierarchy
                if let Some(level) = self.parse_heading_level(tag_name) {
                    // Pop stack until we find a heading with lower level
                    while heading_stack.len() > 1
                        && heading_stack
                            .last()
                            .map(|(l, _)| *l >= level)
                            .unwrap_or(false)
                    {
                        heading_stack.pop();
                    }

                    let heading_parent = heading_stack
                        .last()
                        .map(|(_, id)| *id)
                        .unwrap_or(*parent_id);

                    let heading_id =
                        self.process_heading(doc, &heading_parent, child_element, level)?;

                    if let Some(id) = heading_id {
                        heading_stack.push((level, id));
                        current_heading_parent = id;
                    }
                } else {
                    // Non-heading elements go under current heading
                    self.process_element(doc, &current_heading_parent, child_element, depth + 1)?;
                }
            } else if let Some(text_node) = child.value().as_text() {
                let text = if self.config.preserve_whitespace {
                    text_node.to_string()
                } else {
                    text_node.trim().to_string()
                };

                if text.len() >= self.config.min_text_length {
                    let block = Block::new(Content::text(&text), Some("text"));
                    doc.add_block(block, &current_heading_parent)?;
                }
            }
        }

        Ok(())
    }

    /// Process a single HTML element
    fn process_element(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
        depth: usize,
    ) -> Result<Option<BlockId>> {
        if depth > self.config.max_depth {
            return Ok(None);
        }

        let tag_name = element.value().name();

        match tag_name {
            // Skip script, style, meta, etc.
            "script" | "style" | "meta" | "link" | "head" | "noscript" => Ok(None),

            // Headings (handled separately in process_children for hierarchy)
            "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => {
                let level = self.parse_heading_level(tag_name).unwrap_or(1);
                self.process_heading(doc, parent_id, element, level)
            }

            // Paragraphs
            "p" => self.process_paragraph(doc, parent_id, element),

            // Lists
            "ul" | "ol" => self.process_list(doc, parent_id, element),

            // Code blocks
            "pre" => self.process_code_block(doc, parent_id, element),
            "code" => {
                // Inline code - treat as text
                let code_text = element.text().collect::<String>();
                if !code_text.trim().is_empty() {
                    let formatted = format!("`{}`", code_text);
                    let block = Block::new(Content::text(&formatted), Some("code"));
                    Ok(Some(doc.add_block(block, parent_id)?))
                } else {
                    Ok(None)
                }
            }

            // Blockquotes
            "blockquote" => self.process_blockquote(doc, parent_id, element),

            // Images
            "img" => self.process_image(doc, parent_id, element),

            // Links
            "a" => self.process_link(doc, parent_id, element),

            // Tables
            "table" => self.process_table(doc, parent_id, element),

            // Container elements - process children
            "div" | "section" | "article" | "main" | "aside" | "nav" | "header" | "footer"
            | "span" | "figure" | "figcaption" => {
                self.process_children(doc, parent_id, element, depth)?;
                Ok(None)
            }

            // Line breaks
            "br" | "hr" => Ok(None),

            // Default: try to extract text content
            _ => {
                let text = self.extract_text_content(element);
                if !text.is_empty() && text.len() >= self.config.min_text_length {
                    let block = Block::new(Content::text(&text), Some("text"));
                    Ok(Some(doc.add_block(block, parent_id)?))
                } else {
                    // Process children for unknown container elements
                    self.process_children(doc, parent_id, element, depth)?;
                    Ok(None)
                }
            }
        }
    }

    /// Process a heading element
    fn process_heading(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
        level: usize,
    ) -> Result<Option<BlockId>> {
        let text = self.extract_text_content(element);
        if text.is_empty() {
            return Ok(None);
        }

        let adjusted_level = match self.config.heading_strategy {
            HeadingStrategy::AsIs => level,
            HeadingStrategy::Flatten(target) => target,
            HeadingStrategy::InferFromNesting => level, // Could be enhanced
        };

        let role = format!("heading{}", adjusted_level.clamp(1, 6));
        let block = Block::new(Content::text(&text), Some(&role));
        let block_id = doc.add_block(block, parent_id)?;

        Ok(Some(block_id))
    }

    /// Process a paragraph element
    fn process_paragraph(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let text = self.extract_formatted_text(element);
        if text.is_empty() || text.len() < self.config.min_text_length {
            return Ok(None);
        }

        let block = Block::new(Content::text(&text), Some("paragraph"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Process a list (ul/ol)
    fn process_list(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let li_selector = Selector::parse("li").unwrap();
        let items: Vec<String> = element
            .select(&li_selector)
            .map(|li| self.extract_formatted_text(li))
            .filter(|s| !s.is_empty())
            .collect();

        if items.is_empty() {
            return Ok(None);
        }

        let list_content = items.join("\n");
        let block = Block::new(Content::text(&list_content), Some("list"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Process a code block (pre/code)
    fn process_code_block(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let code_selector = Selector::parse("code").unwrap();
        let code_element = element.select(&code_selector).next().unwrap_or(element);

        let code_text = code_element.text().collect::<String>();
        if code_text.trim().is_empty() {
            return Ok(None);
        }

        // Try to extract language from class
        let language = code_element
            .value()
            .attr("class")
            .and_then(|class| {
                class
                    .split_whitespace()
                    .find(|c| c.starts_with("language-") || c.starts_with("lang-"))
                    .map(|c| {
                        c.trim_start_matches("language-")
                            .trim_start_matches("lang-")
                    })
            })
            .unwrap_or("text");

        let block = Block::new(Content::code(language, &code_text), Some("code"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Process a blockquote
    fn process_blockquote(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let text = self.extract_formatted_text(element);
        if text.is_empty() {
            return Ok(None);
        }

        let block = Block::new(Content::text(&text), Some("quote"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Process an image element
    fn process_image(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        if !self.config.extract_images {
            return Ok(None);
        }

        let src = element.value().attr("src").unwrap_or("");
        let alt = element.value().attr("alt").unwrap_or("");

        if src.is_empty() {
            return Ok(None);
        }

        // Create media content
        let media_source = if src.starts_with("data:") {
            // Base64 encoded image
            let base64_data = src.split(',').nth(1).unwrap_or("").to_string();
            MediaSource::Base64(base64_data)
        } else {
            MediaSource::Url(src.to_string())
        };

        let media = ucm_core::Media::image(media_source).with_alt(alt);
        let block = Block::new(Content::Media(media), Some("image"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Process a link element
    fn process_link(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let text = self.extract_text_content(element);
        let href = element.value().attr("href").unwrap_or("");

        if text.is_empty() {
            return Ok(None);
        }

        if self.config.extract_links && !href.is_empty() {
            // Create link in markdown format
            let link_text = format!("[{}]({})", text, href);
            let block = Block::new(Content::text(&link_text), Some("link"));
            Ok(Some(doc.add_block(block, parent_id)?))
        } else {
            // Just extract as text
            let block = Block::new(Content::text(&text), Some("text"));
            Ok(Some(doc.add_block(block, parent_id)?))
        }
    }

    /// Process a table element
    fn process_table(
        &self,
        doc: &mut Document,
        parent_id: &BlockId,
        element: ElementRef,
    ) -> Result<Option<BlockId>> {
        let row_selector = Selector::parse("tr").unwrap();
        let cell_selector = Selector::parse("td, th").unwrap();

        let rows: Vec<Vec<String>> = element
            .select(&row_selector)
            .map(|row| {
                row.select(&cell_selector)
                    .map(|cell| self.extract_text_content(cell))
                    .collect()
            })
            .filter(|row: &Vec<String>| !row.is_empty())
            .collect();

        if rows.is_empty() {
            return Ok(None);
        }

        let block = Block::new(Content::table(rows), Some("table"));
        Ok(Some(doc.add_block(block, parent_id)?))
    }

    /// Parse heading level from tag name
    fn parse_heading_level(&self, tag_name: &str) -> Option<usize> {
        match tag_name {
            "h1" => Some(1),
            "h2" => Some(2),
            "h3" => Some(3),
            "h4" => Some(4),
            "h5" => Some(5),
            "h6" => Some(6),
            _ => None,
        }
    }

    /// Extract plain text content from an element
    fn extract_text_content(&self, element: ElementRef) -> String {
        let text: String = element.text().collect();
        if self.config.preserve_whitespace {
            text
        } else {
            // Normalize whitespace
            text.split_whitespace().collect::<Vec<_>>().join(" ")
        }
    }

    /// Extract text with some formatting preserved (bold, italic, etc.)
    fn extract_formatted_text(&self, element: ElementRef) -> String {
        let mut result = String::new();

        for child in element.children() {
            if let Some(child_element) = ElementRef::wrap(child) {
                let tag_name = child_element.value().name();
                let child_text = self.extract_formatted_text(child_element);

                match tag_name {
                    "strong" | "b" => {
                        result.push_str("**");
                        result.push_str(&child_text);
                        result.push_str("**");
                    }
                    "em" | "i" => {
                        result.push('*');
                        result.push_str(&child_text);
                        result.push('*');
                    }
                    "code" => {
                        result.push('`');
                        result.push_str(&child_text);
                        result.push('`');
                    }
                    "a" if self.config.extract_links => {
                        let href = child_element.value().attr("href").unwrap_or("");
                        if !href.is_empty() {
                            result.push_str(&format!("[{}]({})", child_text, href));
                        } else {
                            result.push_str(&child_text);
                        }
                    }
                    "br" => {
                        result.push('\n');
                    }
                    _ => {
                        result.push_str(&child_text);
                    }
                }
            } else if let Some(text_node) = child.value().as_text() {
                let text = if self.config.preserve_whitespace {
                    text_node.to_string()
                } else {
                    text_node.split_whitespace().collect::<Vec<_>>().join(" ")
                };
                result.push_str(&text);
            }
        }

        result.trim().to_string()
    }
}

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

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

    #[test]
    fn test_heading_hierarchy() {
        let html = r#"<html><body>
            <h1>Main</h1>
            <p>Intro</p>
            <h2>Sub 1</h2>
            <p>Content 1</p>
            <h2>Sub 2</h2>
            <p>Content 2</p>
        </body></html>"#;

        let doc = HtmlParser::new().parse(html).unwrap();

        // Verify structure
        let root_children = doc.children(&doc.root);
        assert!(!root_children.is_empty());
    }

    #[test]
    fn test_code_language_extraction() {
        let html = r#"<pre><code class="language-rust">fn main() {}</code></pre>"#;
        let doc = HtmlParser::new().parse(html).unwrap();

        // Should have extracted the code block
        assert!(doc.block_count() >= 2);
    }

    #[test]
    fn test_max_depth_limit() {
        let config = HtmlParserConfig {
            max_depth: 2,
            ..Default::default()
        };
        let parser = HtmlParser::with_config(config);

        // Deeply nested HTML
        let html = "<div><div><div><div><div><p>Deep</p></div></div></div></div></div>";
        let result = parser.parse(html);

        // Should handle gracefully (either succeed with truncation or error)
        // The important thing is it doesn't stack overflow
        assert!(result.is_ok() || matches!(result, Err(HtmlError::ResourceLimit(_))));
    }

    #[test]
    fn test_heading_strategy_flatten() {
        let config = HtmlParserConfig {
            heading_strategy: HeadingStrategy::Flatten(3),
            ..Default::default()
        };
        let parser = HtmlParser::with_config(config);

        let html = "<h1>Title</h1><h2>Subtitle</h2>";
        let doc = parser.parse(html).unwrap();

        // All headings should be flattened to h3
        for block in doc.blocks.values() {
            if let Some(ref role) = block.metadata.semantic_role {
                if role.category.as_str().starts_with("heading") {
                    assert_eq!(role.category.as_str(), "heading3");
                }
            }
        }
    }
}