readabilityrs 0.1.4

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
use regex::Regex;
use scraper::{Html, Selector};
use std::sync::LazyLock;

use super::languages::{is_known_language, normalize_language};

static LANGUAGE_CLASS_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)^language-(.+)$").unwrap());
static LANG_CLASS_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^lang-(.+)$").unwrap());
static HIGHLIGHT_SOURCE_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?i)^highlight-source-(.+)$").unwrap());
static BRUSH_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)brush:\s*(\w+)").unwrap());
static LINE_NUMBER_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?m)^\s*\d+[\s|]").unwrap());
static MULTI_NEWLINE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\n{3,}").unwrap());

/// Standardize all code blocks in the HTML to canonical `<pre><code class="language-x">` form.
///
/// Single-pass: parse HTML once, collect all replacements, apply them.
pub fn standardize_code_blocks(html: &str) -> String {
    let doc = Html::parse_fragment(html);
    let mut output = html.to_string();

    // Collect (original_html, replacement) pairs from a single parse
    let mut replacements: Vec<(String, String)> = Vec::new();

    // 1. rehype-pretty-code figures
    if let Ok(sel) = Selector::parse("figure[data-rehype-pretty-code-figure]") {
        for el in doc.select(&sel) {
            if let Some(canonical) = standardize_rehype_figure(&el) {
                replacements.push((el.html(), canonical));
            }
        }
    }

    // 2. GitHub-style highlight divs
    if let Ok(sel) = Selector::parse("div.highlight") {
        for el in doc.select(&sel) {
            let class_attr = el.value().attr("class").unwrap_or("");
            if let Some(lang) = extract_github_language(class_attr) {
                if let Some(code) = extract_pre_text(&el) {
                    let cleaned = clean_code_content(&code);
                    replacements.push((el.html(), format_canonical_code_block(&lang, &cleaned)));
                }
            }
        }
    }

    // 3. Line-number tables
    if let Ok(sel) = Selector::parse("table.highlight-table, table.rouge-table, table.code-listing")
    {
        for el in doc.select(&sel) {
            if let Some((lang, code)) = extract_table_code(&el) {
                let cleaned = clean_code_content(&code);
                replacements.push((el.html(), format_canonical_code_block(&lang, &cleaned)));
            }
        }
    }

    // 4. Shiki blocks
    if let Ok(sel) = Selector::parse("pre.shiki") {
        for el in doc.select(&sel) {
            let lang = detect_language_from_element(&el);
            if let Some(code) = extract_shiki_text(&el) {
                let cleaned = clean_code_content(&code);
                replacements.push((el.html(), format_canonical_code_block(&lang, &cleaned)));
            }
        }
    }

    // 5. Standard pre>code blocks that need normalization
    if let Ok(sel) = Selector::parse("pre") {
        for pre in doc.select(&sel) {
            let pre_html = pre.html();
            if pre_html.contains("data-lang=") {
                continue;
            }
            // Skip if already captured by a parent selector above
            if replacements
                .iter()
                .any(|(orig, _)| orig.contains(&pre_html))
            {
                continue;
            }
            let lang = detect_language_from_pre(&pre);
            if let Some(text) = extract_code_text_from_pre(&pre) {
                let cleaned = clean_code_content(&text);
                replacements.push((pre_html, format_canonical_code_block(&lang, &cleaned)));
            }
        }
    }

    // Apply all replacements
    for (original, canonical) in &replacements {
        output = output.replacen(original, canonical, 1);
    }

    output
}

fn detect_language_from_pre(pre: &scraper::ElementRef) -> String {
    // Check data-lang / data-language on pre
    if let Some(lang) = pre
        .value()
        .attr("data-lang")
        .or(pre.value().attr("data-language"))
    {
        return normalize_language(lang);
    }

    // Check classes on pre
    if let Some(lang) = detect_language_from_classes(pre.value().attr("class").unwrap_or("")) {
        return lang;
    }

    // Check child <code> element
    if let Ok(code_sel) = Selector::parse("code") {
        if let Some(code_el) = pre.select(&code_sel).next() {
            if let Some(lang) = code_el
                .value()
                .attr("data-lang")
                .or(code_el.value().attr("data-language"))
            {
                return normalize_language(lang);
            }
            if let Some(lang) =
                detect_language_from_classes(code_el.value().attr("class").unwrap_or(""))
            {
                return lang;
            }
        }
    }

    String::new()
}

fn detect_language_from_element(el: &scraper::ElementRef) -> String {
    if let Some(lang) = el
        .value()
        .attr("data-lang")
        .or(el.value().attr("data-language"))
    {
        return normalize_language(lang);
    }
    if let Some(lang) = detect_language_from_classes(el.value().attr("class").unwrap_or("")) {
        return lang;
    }

    // Check child code element
    if let Ok(code_sel) = Selector::parse("code") {
        if let Some(code_el) = el.select(&code_sel).next() {
            if let Some(lang) = code_el
                .value()
                .attr("data-lang")
                .or(code_el.value().attr("data-language"))
            {
                return normalize_language(lang);
            }
            if let Some(lang) =
                detect_language_from_classes(code_el.value().attr("class").unwrap_or(""))
            {
                return lang;
            }
        }
    }

    String::new()
}

/// Extract language from CSS classes using priority rules.
fn detect_language_from_classes(classes: &str) -> Option<String> {
    for class in classes.split_whitespace() {
        // language-*
        if let Some(caps) = LANGUAGE_CLASS_RE.captures(class) {
            return Some(normalize_language(&caps[1]));
        }
        // lang-*
        if let Some(caps) = LANG_CLASS_RE.captures(class) {
            return Some(normalize_language(&caps[1]));
        }
        // highlight-source-* (GitHub)
        if let Some(caps) = HIGHLIGHT_SOURCE_RE.captures(class) {
            return Some(normalize_language(&caps[1]));
        }
    }

    // brush: * (WordPress)
    if let Some(caps) = BRUSH_RE.captures(classes) {
        return Some(normalize_language(&caps[1]));
    }

    // Bare known language name
    for class in classes.split_whitespace() {
        if is_known_language(class) {
            return Some(normalize_language(class));
        }
    }

    None
}

fn extract_github_language(class_attr: &str) -> Option<String> {
    for class in class_attr.split_whitespace() {
        if let Some(caps) = HIGHLIGHT_SOURCE_RE.captures(class) {
            return Some(normalize_language(&caps[1]));
        }
    }
    // Fallback: any highlight class with a known language
    detect_language_from_classes(class_attr)
}

fn extract_pre_text(el: &scraper::ElementRef) -> Option<String> {
    let sel = Selector::parse("pre").ok()?;
    let pre = el.select(&sel).next()?;
    Some(pre.text().collect::<String>())
}

fn extract_shiki_text(el: &scraper::ElementRef) -> Option<String> {
    // Shiki uses <span class="line"> inside <code>
    let code_sel = Selector::parse("code").ok()?;
    if let Some(code) = el.select(&code_sel).next() {
        let line_sel = Selector::parse("span.line").ok()?;
        let lines: Vec<String> = code
            .select(&line_sel)
            .map(|span| span.text().collect::<String>())
            .collect();
        if !lines.is_empty() {
            return Some(lines.join("\n"));
        }
        // Fallback to full text
        return Some(code.text().collect::<String>());
    }
    Some(el.text().collect::<String>())
}

fn extract_table_code(el: &scraper::ElementRef) -> Option<(String, String)> {
    // Code is typically in td.code or the second td
    let td_sel = Selector::parse("td").ok()?;
    let tds: Vec<_> = el.select(&td_sel).collect();

    // Try to find the code cell (usually second, or one with class "code")
    for td in &tds {
        let class = td.value().attr("class").unwrap_or("");
        if class.contains("code") || class.contains("rouge-code") {
            let code_text = td.text().collect::<String>();
            let lang = detect_language_from_element(el);
            return Some((lang, code_text));
        }
    }

    // Fallback: use last td
    if tds.len() >= 2 {
        let code_text = tds.last()?.text().collect::<String>();
        let lang = detect_language_from_element(el);
        return Some((lang, code_text));
    }

    None
}

fn extract_code_text_from_pre(pre: &scraper::ElementRef) -> Option<String> {
    // Prefer <code> child text
    if let Ok(code_sel) = Selector::parse("code") {
        if let Some(code) = pre.select(&code_sel).next() {
            // Handle Verso/Lean <code class="hl block">
            let line_sel = Selector::parse("span.line").ok();
            if let Some(ref ls) = line_sel {
                let lines: Vec<String> = code
                    .select(ls)
                    .map(|s| s.text().collect::<String>())
                    .collect();
                if !lines.is_empty() {
                    return Some(lines.join("\n"));
                }
            }
            return Some(code.text().collect::<String>());
        }
    }
    Some(pre.text().collect::<String>())
}

fn standardize_rehype_figure(el: &scraper::ElementRef) -> Option<String> {
    let pre_sel = Selector::parse("pre").ok()?;
    let pre = el.select(&pre_sel).next()?;
    let lang = detect_language_from_pre(&pre);
    let code_text = extract_code_text_from_pre(&pre)?;
    let cleaned = clean_code_content(&code_text);
    Some(format_canonical_code_block(&lang, &cleaned))
}

/// Clean code content: tabs→spaces, strip line numbers, collapse newlines, normalize nbsp.
fn clean_code_content(code: &str) -> String {
    let mut s = code.replace('\t', "    ");
    s = s.replace('\u{00a0}', " "); // non-breaking space

    // Strip leading line numbers (e.g., "  1 |", " 12\t")
    let lines: Vec<&str> = s.lines().collect();
    let has_line_numbers = lines.len() > 2
        && lines
            .iter()
            .filter(|l| !l.trim().is_empty())
            .take(5)
            .all(|l| LINE_NUMBER_RE.is_match(l));
    if has_line_numbers {
        s = lines
            .iter()
            .map(|l| LINE_NUMBER_RE.replace(l, "").to_string())
            .collect::<Vec<_>>()
            .join("\n");
    }

    // Collapse 3+ newlines to 2
    s = MULTI_NEWLINE_RE.replace_all(&s, "\n\n").to_string();

    s.trim().to_string()
}

/// Escape HTML special characters in code text so it can be safely
/// embedded inside `<code>` elements without breaking the HTML structure.
///
/// Quotes are deliberately left alone: inside `<pre><code>` a quote is ordinary
/// source text, and escaping it would corrupt the snippet.
fn html_escape_code(s: &str) -> String {
    super::escaping::escape_html_preserving_entities(s, false)
}

/// Format a canonical code block.
fn format_canonical_code_block(lang: &str, code: &str) -> String {
    let escaped = html_escape_code(code);
    if lang.is_empty() {
        format!("<pre><code>{}</code></pre>", escaped)
    } else {
        format!(
            "<pre><code class=\"language-{}\" data-lang=\"{}\">{}</code></pre>",
            lang, lang, escaped
        )
    }
}

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

    #[test]
    fn test_prism_code_block() {
        let html = r#"<pre class="language-python"><code class="language-python">print("hello")</code></pre>"#;
        let result = standardize_code_blocks(html);
        assert!(result.contains("data-lang=\"python\""));
        assert!(result.contains("print(\"hello\")"));
    }

    #[test]
    fn test_brush_wordpress() {
        let html = r#"<pre class="brush: ruby"><code>puts "hi"</code></pre>"#;
        let result = standardize_code_blocks(html);
        assert!(result.contains("data-lang=\"ruby\""));
    }

    #[test]
    fn test_language_detection_bare() {
        assert_eq!(
            detect_language_from_classes("python"),
            Some("python".into())
        );
        assert_eq!(
            detect_language_from_classes("language-js"),
            Some("javascript".into())
        );
        assert_eq!(
            detect_language_from_classes("lang-ts"),
            Some("typescript".into())
        );
        assert_eq!(
            detect_language_from_classes("highlight-source-go"),
            Some("go".into())
        );
    }

    #[test]
    fn test_clean_code_content_tabs() {
        let code = "fn main() {\n\tprintln!(\"hi\");\n}";
        let cleaned = clean_code_content(code);
        assert!(cleaned.contains("    println!"));
    }

    #[test]
    fn test_clean_code_content_nbsp() {
        let code = "let\u{00a0}x = 1;";
        let cleaned = clean_code_content(code);
        assert_eq!(cleaned, "let x = 1;");
    }

    #[test]
    fn test_html_escape_code_preserves_existing_entity() {
        assert_eq!(
            html_escape_code("if (a &amp;&amp; b)"),
            "if (a &amp;&amp; b)"
        );
    }

    #[test]
    fn test_html_escape_code_escapes_bare_ampersands_and_angles() {
        assert_eq!(
            html_escape_code(r#"if (a && b) { s = "x < y"; }"#),
            r#"if (a &amp;&amp; b) { s = "x &lt; y"; }"#
        );
    }

    #[test]
    fn test_html_escape_code_leaves_quotes_alone() {
        let escaped = html_escape_code(r#"let s = "quoted";"#);
        assert!(!escaped.contains("&quot;"));
        assert_eq!(escaped, r#"let s = "quoted";"#);
    }

    #[test]
    fn test_html_escape_code_multibyte_utf8_does_not_panic() {
        assert_eq!(
            html_escape_code("let s = \"\u{4f60}\u{597d}\"; // a & b"),
            "let s = \"\u{4f60}\u{597d}\"; // a &amp; b"
        );
    }
}