readabilityrs 0.1.3

A Rust port of Mozilla's Readability library for extracting article content from web pages
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
//! Main Readability struct and parse implementation.
//!
//! This module contains the primary [`Readability`] struct which orchestrates
//! the entire article extraction pipeline.
//!
//! ## Example
//!
//! ```rust,no_run
//! use readabilityrs::{Readability, ReadabilityOptions};
//!
//! let html = std::fs::read_to_string("article.html").unwrap();
//! let url = "https://example.com/article";
//!
//! let readability = Readability::new(&html, Some(url), None)?;
//!
//! if let Some(article) = readability.parse() {
//!     println!("Title: {:?}", article.title);
//!     println!("Author: {:?}", article.byline);
//!     println!("Content length: {} chars", article.length);
//!
//!     // Save to file
//!     if let Some(content) = article.content {
//!         std::fs::write("output.html", content)?;
//!     }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::{
    article::Article,
    cleaner,
    content_extractor::grab_article,
    dom_utils,
    error::{ReadabilityError, Result},
    metadata::{get_article_metadata, get_json_ld, Metadata},
    options::ReadabilityOptions,
    utils,
};
use scraper::{ElementRef, Html, Selector};

/// The main Readability parser.
///
/// This struct is the primary interface for extracting article content from HTML documents.
/// It implements Mozilla's Readability algorithm, which powers Firefox's Reader View.
///
/// ## Lifecycle
///
/// The typical usage pattern starts by constructing a `Readability` instance with
/// [`Readability::new()`], then calling [`parse()`](Readability::parse) to extract the content.
/// The result is an [`Article`] containing the extracted content and metadata.
///
/// ## Features
///
/// - Intelligent content identification
/// - Metadata extraction (title, author, date, etc.)
/// - JSON-LD structured data parsing
/// - Multiple retry strategies for difficult pages
/// - Configurable thresholds and behavior
///
/// ## Example
///
/// ```rust,no_run
/// use readabilityrs::{Readability, ReadabilityOptions};
///
/// let html = r#"
///     <html>
///     <head><title>Article Title</title></head>
///     <body>
///         <article>
///             <h1>Article Title</h1>
///             <p>First paragraph of content...</p>
///             <p>Second paragraph of content...</p>
///         </article>
///     </body>
///     </html>
/// "#;
///
/// let readability = Readability::new(html, None, None)?;
/// let article = readability.parse();
///
/// if let Some(article) = article {
///     println!("Success! Extracted {} characters", article.length);
/// } else {
///     println!("Could not extract article content");
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// ## With Custom Options
///
/// ```rust,no_run
/// use readabilityrs::{Readability, ReadabilityOptions};
///
/// let html = "<html>...</html>";
///
/// let options = ReadabilityOptions::builder()
///     .char_threshold(300)
///     .debug(true)
///     .build();
///
/// let readability = Readability::new(html, None, Some(options))?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Readability {
    /// The HTML document being parsed (raw, for metadata extraction)
    document: Html,

    /// Original HTML string (stored for preprocessing before content extraction)
    html: String,

    /// Base URL for resolving relative links
    base_url: Option<String>,

    /// Configuration options
    options: ReadabilityOptions,

    /// Extracted metadata
    metadata: Metadata,
}

impl Readability {
    /// Create a new Readability instance
    ///
    /// # Arguments
    /// * `html` - The HTML content to parse
    /// * `url` - Optional base URL for resolving relative links
    /// * `options` - Optional configuration options
    ///
    /// # Returns
    /// Result containing the Readability instance or an error
    pub fn new(html: &str, url: Option<&str>, options: Option<ReadabilityOptions>) -> Result<Self> {
        // Parse raw HTML for metadata extraction
        // Preprocessing happens later in parse() before content extraction
        let document = Html::parse_document(html);

        // Validate base URL if provided
        let base_url = url
            .map(|u| {
                url::Url::parse(u)
                    .map(|_| u.to_string())
                    .map_err(|_| ReadabilityError::InvalidUrl(u.to_string()))
            })
            .transpose()?;

        let options = options.unwrap_or_default();

        Ok(Self {
            document,
            html: html.to_string(),
            base_url,
            options,
            metadata: Metadata::default(),
        })
    }

    /// Parse the document and extract article content
    ///
    /// # Returns
    /// `Option<Article>` - Some(article) if successful, None if no article found
    pub fn parse(mut self) -> Option<Article> {
        let json_ld = if !self.options.disable_json_ld {
            get_json_ld(&self.document)
        } else {
            Metadata::default()
        };

        self.metadata = get_article_metadata(&self.document, json_ld);

        let preprocessed_html = cleaner::prep_document(&self.html);
        let preprocessed_doc = Html::parse_document(&preprocessed_html);

        match grab_article(&preprocessed_doc, &self.options) {
            Ok(Some(content_html)) => {
                let cleaned_wrapper_html =
                    cleaner::clean_article_content_light(&content_html, self.base_url.as_deref())
                        .unwrap_or_else(|_| content_html.clone());

                let mut prepped_html = crate::post_processor::prep_article(
                    &cleaned_wrapper_html,
                    self.options.clean_styles,
                    self.options.clean_whitespace,
                );

                // Remove title from content if the option is enabled
                if self.options.remove_title_from_content {
                    if let Some(ref title) = self.metadata.title {
                        prepped_html =
                            crate::post_processor::remove_title_from_content(&prepped_html, title);
                    }
                }
                let cleaned_html =
                    match cleaner::clean_article_content(&prepped_html, self.base_url.as_deref()) {
                        Ok(html) => html,
                        Err(e) => {
                            if self.options.debug {
                                eprintln!("Error cleaning content: {e}");
                            }
                            prepped_html
                        }
                    };

                let text_content = self.get_text_content(&cleaned_html);
                let length = text_content.len();

                // Generate excerpt from content if not in metadata
                // Try first paragraph of extracted content, then fall back to text
                let excerpt = self.metadata.excerpt.clone().or_else(|| {
                    self.generate_excerpt_from_html(&cleaned_html)
                        .or_else(|| self.generate_excerpt_from_text(&text_content))
                });

                // Extract text direction from document
                let dir = crate::dom_utils::get_article_direction(&self.document);

                // Optionally produce markdown output
                let markdown_content = if self.options.output_markdown {
                    let md_opts = self
                        .options
                        .markdown_options
                        .as_ref()
                        .cloned()
                        .unwrap_or_default();
                    let standardized = crate::elements::standardize_all(
                        &cleaned_html,
                        self.metadata.title.as_deref(),
                    );
                    Some(crate::markdown::html_to_markdown(&standardized, &md_opts))
                } else {
                    None
                };

                Some(Article {
                    title: self.metadata.title,
                    content: Some(cleaned_html),
                    raw_content: Some(content_html),
                    text_content: Some(text_content),
                    length,
                    excerpt,
                    image: self.metadata.image,
                    byline: self.metadata.byline,
                    dir,
                    site_name: self.metadata.site_name,
                    lang: self.metadata.lang,
                    published_time: self.metadata.published_time,
                    markdown_content,
                })
            }
            Ok(None) => None,
            Err(e) => {
                if self.options.debug {
                    eprintln!("Error grabbing article: {e}");
                }
                None
            }
        }
    }

    /// Extract plain text from HTML content
    fn get_text_content(&self, html: &str) -> String {
        let doc = Html::parse_fragment(html);
        doc.root_element().text().collect::<String>()
    }

    /// Generate an excerpt from the first paragraph of article HTML
    ///
    /// Extracts text from the first <p> tag found in the article content.
    /// This matches Mozilla's Readability.js behavior.
    ///
    /// # Arguments
    /// * `html` - The article HTML content
    ///
    /// # Returns
    /// Option<String> - Text from first paragraph, or None if no suitable paragraph found
    fn generate_excerpt_from_html(&self, html: &str) -> Option<String> {
        let doc = Html::parse_fragment(html);
        let p_selector = Selector::parse("p").ok()?;

        for p in doc.select(&p_selector) {
            let text = p.text().collect::<String>();
            let trimmed = text.trim();

            if trimmed.len() < 25 {
                continue;
            }

            if utils::looks_like_bracket_menu(trimmed) {
                continue;
            }

            let class_attr = p.value().attr("class").unwrap_or("");
            let id_attr = p.value().attr("id").unwrap_or("");
            let class_lower = class_attr.to_lowercase();
            let id_lower = id_attr.to_lowercase();

            if Self::paragraph_is_excerpt_noise(&p, trimmed, &class_lower, &id_lower) {
                continue;
            }

            let looks_like_byline = utils::looks_like_byline(trimmed)
                || class_lower.contains("byline")
                || class_lower.contains("author")
                || id_lower.contains("byline")
                || id_lower.contains("author");
            if looks_like_byline {
                continue;
            }

            return Some(trimmed.to_string());
        }

        None
    }

    fn paragraph_is_excerpt_noise(
        element: &ElementRef,
        text: &str,
        class_lower: &str,
        id_lower: &str,
    ) -> bool {
        const CLASS_KEYWORDS: [&str; 8] = [
            "hatnote",
            "shortdescription",
            "metadata",
            "navbox",
            "dablink",
            "noprint",
            "mwe-math-element",
            "mw-empty-elt",
        ];

        if CLASS_KEYWORDS
            .iter()
            .any(|kw| class_lower.contains(kw) || id_lower.contains(kw))
        {
            return true;
        }

        if element
            .value()
            .attr("role")
            .map(|role| role.eq_ignore_ascii_case("note"))
            .unwrap_or(false)
        {
            return true;
        }

        let trimmed_lower = text.to_lowercase();
        const TEXT_PREFIXES: [&str; 5] = [
            "see also",
            "coordinates",
            "navigation menu",
            "external links",
            "further reading",
        ];
        if TEXT_PREFIXES
            .iter()
            .any(|prefix| trimmed_lower.starts_with(prefix))
        {
            return true;
        }

        let link_density = dom_utils::get_link_density(*element);
        link_density > 0.8
    }

    /// Generate an excerpt from the article text content
    ///
    /// Takes the first paragraph or first ~200 characters of the article text
    /// and uses it as an excerpt. This matches Mozilla's Readability.js behavior.
    ///
    /// # Arguments
    /// * `text` - The article text content
    ///
    /// # Returns
    /// Option<String> - Generated excerpt, or None if text is too short
    fn generate_excerpt_from_text(&self, text: &str) -> Option<String> {
        let cleaned = text.trim();

        if cleaned.is_empty() {
            return None;
        }

        // Try to find the first substantial paragraph (at least 80 chars)
        // Split by double newlines to find paragraphs
        for paragraph in cleaned.split("\n\n") {
            let para_trimmed = paragraph.trim();
            if para_trimmed.len() < 80 {
                continue;
            }

            if utils::looks_like_bracket_menu(para_trimmed) {
                continue;
            }

            return Some(self.truncate_text(para_trimmed, 300));
        }

        if utils::looks_like_bracket_menu(cleaned) {
            return None;
        }

        // Take first ~300 chars if substantial enough
        if cleaned.len() > 40 {
            Some(self.truncate_text(cleaned, 300))
        } else {
            None
        }
    }

    /// Truncate text to a maximum length, trying to break at word boundary
    ///
    /// # Arguments
    /// * `text` - Text to truncate
    /// * `max_len` - Maximum length in characters
    ///
    /// # Returns
    /// String - Truncated text
    fn truncate_text(&self, text: &str, max_len: usize) -> String {
        let char_count = text.chars().count();
        if char_count <= max_len {
            return text.to_string();
        }

        let truncated: String = text.chars().take(max_len).collect();
        if let Some(last_space_pos) = truncated.rfind(char::is_whitespace) {
            truncated[..last_space_pos].trim().to_string()
        } else {
            truncated.trim().to_string()
        }
    }

    /// Log a debug message (if debug mode is enabled)
    #[allow(dead_code)]
    fn log(&self, message: &str) {
        if self.options.debug {
            eprintln!("Reader: (Readability) {message}");
        }
    }
}

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

    #[test]
    fn test_new_readability() {
        let html = r#"<html><body><p>Test</p></body></html>"#;
        let result = Readability::new(html, None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_invalid_url() {
        let html = r#"<html><body><p>Test</p></body></html>"#;
        let result = Readability::new(html, Some("not a url"), None);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_simple() {
        let html = r#"
            <html>
                <body>
                    <article>
                        <h1>Test Article</h1>
                        <p>This is a test article with some content.</p>
                    </article>
                </body>
            </html>
        "#;

        let readability = Readability::new(html, None, None).unwrap();
        let _article = readability.parse();
        // For now, just test that it doesn't panic
        // Full functionality will be tested once implementation is complete
    }

    #[test]
    fn excerpt_skips_hatnote_paragraphs() {
        let html = r#"
        <p class="hatnote" role="note">See also: Something else entirely.</p>
        <p>This is the first real paragraph with sufficient length to act as an excerpt. It should be returned.</p>
        "#;
        let reader = Readability::new(html, None, None).unwrap();
        let excerpt = reader.generate_excerpt_from_html(html);
        assert_eq!(
            excerpt,
            Some(
                "This is the first real paragraph with sufficient length to act as an excerpt. It should be returned."
                    .to_string()
            )
        );
    }
}