|
| 1 | +use crate::{DensityTree, DomExtractionError}; |
| 2 | +use scraper::{ElementRef, Html}; |
| 3 | + |
| 4 | +/// Extracts the main content from an HTML document as markdown using CETD analysis. |
| 5 | +/// |
| 6 | +/// This function identifies the highest density content node using the CETD algorithm |
| 7 | +/// and converts its HTML content to markdown format. |
| 8 | +/// |
| 9 | +/// # Arguments |
| 10 | +/// * `dtree` - A DensityTree that has been built and analyzed |
| 11 | +/// * `document` - The original HTML document for node reference |
| 12 | +/// |
| 13 | +/// # Returns |
| 14 | +/// A Result containing the extracted markdown content or an error |
| 15 | +#[cfg(feature = "markdown")] |
| 16 | +pub fn extract_content_as_markdown( |
| 17 | + dtree: &DensityTree, |
| 18 | + document: &Html, |
| 19 | +) -> Result<String, DomExtractionError> { |
| 20 | + // Get the node with maximum density sum |
| 21 | + let max_node = match dtree.get_max_density_sum_node() { |
| 22 | + Some(node) => node, |
| 23 | + None => return Ok(String::new()), // No content found |
| 24 | + }; |
| 25 | + |
| 26 | + // Get the NodeId from the density node |
| 27 | + let node_id = max_node.value().node_id; |
| 28 | + |
| 29 | + // Get the scraper node from the document |
| 30 | + let scraper_node = document |
| 31 | + .tree |
| 32 | + .get(node_id) |
| 33 | + .ok_or(DomExtractionError::NodeAccessError(node_id))?; |
| 34 | + |
| 35 | + // Find the nearest parent element that can be wrapped as ElementRef |
| 36 | + let mut current_node = scraper_node; |
| 37 | + let element_ref = loop { |
| 38 | + if let Some(element) = ElementRef::wrap(current_node) { |
| 39 | + break element; |
| 40 | + } |
| 41 | + |
| 42 | + // Move to parent if current node is not an element |
| 43 | + if let Some(parent) = current_node.parent() { |
| 44 | + current_node = parent; |
| 45 | + } else { |
| 46 | + return Err(DomExtractionError::NodeAccessError(node_id)); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + // Extract the HTML content |
| 51 | + let html_content = element_ref.inner_html(); |
| 52 | + |
| 53 | + // Convert HTML to markdown using htmd |
| 54 | + let converter = htmd::HtmlToMarkdown::builder() |
| 55 | + .skip_tags(vec!["script", "style"]) |
| 56 | + .build(); |
| 57 | + |
| 58 | + converter |
| 59 | + .convert(&html_content) |
| 60 | + .map_err(|_| DomExtractionError::NodeAccessError(node_id)) |
| 61 | + .map(|md| md.trim().to_string()) |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + use crate::DensityTree; |
| 68 | + |
| 69 | + #[test] |
| 70 | + #[cfg(feature = "markdown")] |
| 71 | + fn test_extract_content_as_markdown() { |
| 72 | + let html = r#" |
| 73 | + <html> |
| 74 | + <body> |
| 75 | + <div class="header">Navigation</div> |
| 76 | + <article> |
| 77 | + <h1>Main Article</h1> |
| 78 | + <p>This is the main content with lots of text that should have high density.</p> |
| 79 | + <p>Another paragraph with substantial content for density analysis.</p> |
| 80 | + </article> |
| 81 | + <div class="sidebar">Sidebar content</div> |
| 82 | + </body> |
| 83 | + </html> |
| 84 | + "#; |
| 85 | + |
| 86 | + let document = Html::parse_document(html); |
| 87 | + let mut dtree = DensityTree::from_document(&document).unwrap(); |
| 88 | + dtree.calculate_density_sum().unwrap(); |
| 89 | + |
| 90 | + let markdown = extract_content_as_markdown(&dtree, &document).unwrap(); |
| 91 | + |
| 92 | + // Debug: print what we actually got |
| 93 | + println!("Generated markdown: '{}'", markdown); |
| 94 | + |
| 95 | + // Should contain the main content |
| 96 | + assert!(!markdown.is_empty(), "Markdown should not be empty"); |
| 97 | + // Relaxed assertions for debugging |
| 98 | + assert!(markdown.contains("Main Article")); |
| 99 | + assert!(markdown.contains("main content")); |
| 100 | + } |
| 101 | +} |
0 commit comments