decruft 0.1.3

Extract clean, readable 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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use ego_tree::NodeId;
use markup5ever::{QualName, ns};
use scraper::{Html, Node};

use crate::dom;

/// Highlighter class patterns that capture a language name.
/// Each pattern is tried against individual CSS class names.
const HIGHLIGHTER_PREFIXES: &[&str] = &[
    "language-",
    "lang-",
    "syntax-",
    "highlight-",
    "code-",
    "cm-s-", // CodeMirror theme prefix (skip, not a language)
];

/// Suffixes that indicate a language class.
const HIGHLIGHTER_SUFFIXES: &[&str] = &["-code", "-snippet"];

/// Class patterns to strip as code-block line numbers.
const LINE_NUMBER_CLASSES: &[&str] = &[
    "linenumber",
    "line-number",
    "line-numbers",
    "lineno",
    "linenos",
    "rouge-gutter",
    "hljs-ln-numbers",
    "hljs-ln-line",
    "lnt",
    "react-syntax-highlighter-line-number",
    "code-line-number",
    "gutter",
];

/// Classes on elements that are copy/run buttons to remove.
const BUTTON_CLASSES: &[&str] = &[
    "copy-button",
    "copy-code-button",
    "copybutton",
    "code-copy",
    "btn-copy",
    "copy",
];

/// Data attributes that mark copy buttons.
const BUTTON_DATA_ATTRS: &[&str] = &["data-copy", "data-clipboard-text"];

/// Known programming language names and aliases.
const LANGUAGES: &[&str] = &[
    "actionscript",
    "ada",
    "agda",
    "apache",
    "applescript",
    "arduino",
    "asm",
    "assembly",
    "astro",
    "awk",
    "bash",
    "basic",
    "batch",
    "brainfuck",
    "bsl",
    "c",
    "ceylon",
    "clojure",
    "cmake",
    "cobol",
    "coffeescript",
    "cpp",
    "crystal",
    "csharp",
    "css",
    "csv",
    "cuda",
    "d",
    "dart",
    "delphi",
    "diff",
    "django",
    "dockerfile",
    "dotnet",
    "eiffel",
    "elixir",
    "elm",
    "emacs",
    "erb",
    "erlang",
    "excel",
    "fish",
    "fortran",
    "fsharp",
    "gdscript",
    "gherkin",
    "glsl",
    "go",
    "graphql",
    "groovy",
    "haml",
    "handlebars",
    "haskell",
    "haxe",
    "hlsl",
    "html",
    "http",
    "ini",
    "io",
    "java",
    "javascript",
    "jinja",
    "json",
    "jsonnet",
    "jsx",
    "julia",
    "jupyter",
    "kotlin",
    "latex",
    "lean",
    "less",
    "liquid",
    "lisp",
    "livescript",
    "llvm",
    "lua",
    "makefile",
    "markdown",
    "markup",
    "mathematica",
    "matlab",
    "mdx",
    "mermaid",
    "mips",
    "moonscript",
    "nginx",
    "nim",
    "nix",
    "nushell",
    "objc",
    "objective-c",
    "ocaml",
    "openscad",
    "pascal",
    "perl",
    "php",
    "plaintext",
    "plsql",
    "postcss",
    "powershell",
    "prisma",
    "processing",
    "prolog",
    "protobuf",
    "puppet",
    "purescript",
    "python",
    "r",
    "racket",
    "razor",
    "regex",
    "rescript",
    "rest",
    "ruby",
    "rust",
    "sass",
    "scala",
    "scheme",
    "scss",
    "sh",
    "shell",
    "smalltalk",
    "smarty",
    "solidity",
    "sparql",
    "sql",
    "stan",
    "stylus",
    "svelte",
    "svg",
    "swift",
    "tcl",
    "terraform",
    "tex",
    "text",
    "toml",
    "tsx",
    "twig",
    "typescript",
    "v",
    "vala",
    "vb",
    "vbnet",
    "verilog",
    "vhdl",
    "vim",
    "vue",
    "wasm",
    "wolfram",
    "xml",
    "xquery",
    "xslt",
    "yaml",
    "zig",
    "zsh",
    // Common aliases
    "js",
    "ts",
    "rb",
    "py",
    "rs",
    "cs",
    "hs",
    "yml",
    "md",
];

/// Standardize code blocks within the content subtree.
///
/// Normalizes various syntax highlighter markup into clean
/// `<pre><code data-lang="...">` elements.
pub fn standardize_code_blocks(html: &mut Html, main_content: NodeId) {
    remove_line_numbers(html, main_content);
    remove_copy_buttons(html, main_content);
    normalize_pre_code_blocks(html, main_content);
}

/// Remove line number elements from code blocks.
fn remove_line_numbers(html: &mut Html, main_content: NodeId) {
    let mut to_remove = Vec::new();

    let descendants = dom::all_descendant_elements(html, main_content);
    for node_id in descendants {
        if should_remove_as_line_number(html, node_id) {
            to_remove.push(node_id);
        }
    }

    for id in to_remove {
        dom::remove_node(html, id);
    }
}

/// Check if an element should be removed as a line number.
fn should_remove_as_line_number(html: &Html, node_id: NodeId) -> bool {
    let Some(node_ref) = html.tree.get(node_id) else {
        return false;
    };
    let Node::Element(el) = node_ref.value() else {
        return false;
    };

    let classes = el.attr("class").unwrap_or("");
    for class in classes.split_whitespace() {
        let lower = class.to_ascii_lowercase();
        if LINE_NUMBER_CLASSES.iter().any(|&c| c == lower) {
            return true;
        }
    }

    // <td> cells used for line numbers in table-based highlighters
    let tag = el.name.local.as_ref();
    if tag == "td" && classes.contains("hljs-ln-numbers") {
        return true;
    }

    false
}

/// Remove copy/clipboard buttons from code blocks.
fn remove_copy_buttons(html: &mut Html, main_content: NodeId) {
    let mut to_remove = Vec::new();

    let descendants = dom::all_descendant_elements(html, main_content);
    for node_id in descendants {
        if is_copy_button(html, node_id) {
            to_remove.push(node_id);
        }
    }

    for id in to_remove {
        dom::remove_node(html, id);
    }
}

/// Check if an element is a copy button.
fn is_copy_button(html: &Html, node_id: NodeId) -> bool {
    let Some(node_ref) = html.tree.get(node_id) else {
        return false;
    };
    let Node::Element(el) = node_ref.value() else {
        return false;
    };

    // Check data attributes
    for attr_name in BUTTON_DATA_ATTRS {
        if el.attr(attr_name).is_some() {
            return true;
        }
    }

    // Check class names
    let classes = el.attr("class").unwrap_or("");
    for class in classes.split_whitespace() {
        let lower = class.to_ascii_lowercase();
        if BUTTON_CLASSES.iter().any(|&c| c == lower) {
            return true;
        }
    }

    false
}

/// Find all `<pre>` elements, detect language, unwrap inner spans,
/// and normalize to `<pre><code data-lang="...">`.
fn normalize_pre_code_blocks(html: &mut Html, main_content: NodeId) {
    let pre_ids = dom::descendant_elements_by_tag(html, main_content, "pre");

    for pre_id in pre_ids {
        normalize_single_pre(html, pre_id);
    }
}

/// Normalize a single `<pre>` element into `<pre><code>` form.
fn normalize_single_pre(html: &mut Html, pre_id: NodeId) {
    let language = detect_language(html, pre_id);
    let code_text = extract_code_text(html, pre_id);

    // Remove all existing children of <pre>
    let child_ids: Vec<NodeId> = {
        let Some(node_ref) = html.tree.get(pre_id) else {
            return;
        };
        node_ref.children().map(|c| c.id()).collect()
    };
    for child_id in child_ids {
        dom::remove_node(html, child_id);
    }

    // Create <code> element with data-lang
    let code_el = build_code_element(&language);
    let code_id = html.tree.orphan(Node::Element(code_el)).id();

    // Create text node with the code content
    let text_node = Node::Text(scraper::node::Text {
        text: code_text.into(),
    });
    let text_id = html.tree.orphan(text_node).id();

    // Append text into <code>, then <code> into <pre>
    {
        let Some(mut code_mut) = html.tree.get_mut(code_id) else {
            return;
        };
        code_mut.append_id(text_id);
    }
    {
        let Some(mut pre_mut) = html.tree.get_mut(pre_id) else {
            return;
        };
        pre_mut.append_id(code_id);
    }

    // Strip all attributes from <pre> itself
    let Some(mut pre_mut) = html.tree.get_mut(pre_id) else {
        return;
    };
    let Node::Element(el) = pre_mut.value() else {
        return;
    };
    el.attrs.clear();
}

/// Build a `<code>` element, optionally with `data-lang` attribute.
fn build_code_element(language: &str) -> scraper::node::Element {
    let name = QualName::new(None, ns!(html), markup5ever::local_name!("code"));
    let mut attributes = Vec::new();

    if !language.is_empty() {
        attributes.push(markup5ever::Attribute {
            name: QualName::new(None, ns!(), markup5ever::LocalName::from("data-lang")),
            value: language.into(),
        });
    }

    scraper::node::Element::new(name, attributes)
}

/// Detect the programming language from a `<pre>` and its children.
fn detect_language(html: &Html, pre_id: NodeId) -> String {
    // Check <pre> itself
    if let Some(lang) = detect_language_from_node(html, pre_id) {
        return lang;
    }

    // Check child <code> elements
    let code_children = dom::descendant_elements_by_tag(html, pre_id, "code");
    for code_id in code_children {
        if let Some(lang) = detect_language_from_node(html, code_id) {
            return lang;
        }
    }

    // Check parent elements (wrapper divs with language classes)
    let mut current = pre_id;
    for _ in 0..3 {
        let Some(parent_id) = dom::parent_element(html, current) else {
            break;
        };
        if let Some(lang) = detect_language_from_node(html, parent_id) {
            return lang;
        }
        current = parent_id;
    }

    String::new()
}

/// Try to detect language from a single element's attributes.
fn detect_language_from_node(html: &Html, node_id: NodeId) -> Option<String> {
    let node_ref = html.tree.get(node_id)?;
    let Node::Element(el) = node_ref.value() else {
        return None;
    };

    // Check data-language / data-lang attributes (Shiki, rehype)
    for attr_name in &["data-language", "data-lang", "language"] {
        if let Some(val) = el.attr(attr_name) {
            let lower = val.to_ascii_lowercase();
            if is_known_language(&lower) {
                return Some(lower);
            }
        }
    }

    // Check class names
    let classes = el.attr("class").unwrap_or("");
    detect_language_from_classes(classes)
}

/// Extract language from CSS class names using highlighter patterns.
fn detect_language_from_classes(classes: &str) -> Option<String> {
    for class in classes.split_whitespace() {
        let lower = class.to_ascii_lowercase();

        // Check prefix patterns: language-X, lang-X, etc.
        for prefix in HIGHLIGHTER_PREFIXES {
            if let Some(lang) = lower.strip_prefix(prefix)
                && is_known_language(lang)
            {
                return Some(lang.to_string());
            }
        }

        // Check suffix patterns: X-code, X-snippet
        for suffix in HIGHLIGHTER_SUFFIXES {
            if let Some(lang) = lower.strip_suffix(suffix)
                && is_known_language(lang)
            {
                return Some(lang.to_string());
            }
        }

        // Bare language name as class
        if is_known_language(&lower) {
            return Some(lower);
        }
    }
    None
}

/// Check if a string is a known programming language.
fn is_known_language(name: &str) -> bool {
    LANGUAGES.contains(&name)
}

/// Extract plain text content from a code block, stripping all
/// markup but preserving whitespace structure.
fn extract_code_text(html: &Html, node_id: NodeId) -> String {
    let mut buf = String::new();
    collect_code_text(html, node_id, &mut buf);

    // Clean up: normalize tabs, NBSP, trim
    let cleaned = buf.replace('\t', "    ").replace('\u{00a0}', " ");

    // Trim leading/trailing whitespace, normalize multiple blank lines
    let trimmed = cleaned.trim();
    collapse_blank_lines(trimmed)
}

/// Recursively collect text from a node, skipping line number
/// and button elements.
fn collect_code_text(html: &Html, node_id: NodeId, buf: &mut String) {
    let Some(node_ref) = html.tree.get(node_id) else {
        return;
    };
    match node_ref.value() {
        Node::Text(t) => buf.push_str(t),
        Node::Element(el) => {
            let tag = el.name.local.as_ref();

            // Handle <br> as newline
            if tag == "br" {
                buf.push('\n');
                return;
            }

            // Skip buttons and styles
            if tag == "button" || tag == "style" {
                return;
            }

            for child in node_ref.children() {
                collect_code_text(html, child.id(), buf);
            }
        }
        _ => {}
    }
}

/// Collapse runs of 3+ newlines into 2 newlines.
fn collapse_blank_lines(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut newline_count = 0u32;

    for ch in s.chars() {
        if ch == '\n' {
            newline_count += 1;
            if newline_count <= 2 {
                result.push(ch);
            }
        } else {
            newline_count = 0;
            result.push(ch);
        }
    }
    result
}

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

    fn parse_and_standardize(html_str: &str) -> String {
        let mut doc = Html::parse_document(html_str);
        let root = doc.tree.root().id();
        standardize_code_blocks(&mut doc, root);
        dom::outer_html(&doc, root)
    }

    #[test]
    fn basic_pre_code_passthrough() {
        let html = r"<html><body><pre><code>fn main() {}</code></pre></body></html>";
        let result = parse_and_standardize(html);
        assert!(result.contains("<pre><code>fn main() {}</code></pre>"));
    }

    #[test]
    fn detects_language_from_class() {
        let html = r#"<html><body>
            <pre><code class="language-rust">fn main() {}</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="rust""#));
        assert!(result.contains("fn main() {}"));
    }

    #[test]
    fn detects_prismjs_lang_prefix() {
        let html = r#"<html><body>
            <pre><code class="lang-python">print("hi")</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="python""#));
    }

    #[test]
    fn detects_highlightjs_hljs_class() {
        let html = r#"<html><body>
            <pre><code class="hljs language-javascript">let x = 1;</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="javascript""#));
    }

    #[test]
    fn detects_data_language_attribute() {
        let html = r#"<html><body>
            <pre data-language="go"><code>fmt.Println("hi")</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="go""#));
    }

    #[test]
    fn strips_syntax_highlighting_spans() {
        let html = r#"<html><body>
            <pre><code class="language-rust">
                <span class="keyword">fn</span> <span class="function">main</span>() {}
            </code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="rust""#));
        assert!(!result.contains("<span"));
        assert!(result.contains("fn"));
        assert!(result.contains("main"));
    }

    #[test]
    fn removes_line_numbers() {
        let html = r#"<html><body>
            <pre><code class="language-python">
                <span class="lineno">1</span>print("hello")
                <span class="lineno">2</span>print("world")
            </code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(!result.contains("lineno"));
        assert!(result.contains("print"));
    }

    #[test]
    fn removes_copy_buttons() {
        let html = r#"<html><body>
            <pre>
                <button class="copy-button">Copy</button>
                <code class="language-rust">let x = 1;</code>
            </pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(!result.contains("copy-button"));
        assert!(!result.contains("Copy"));
        assert!(result.contains("let x = 1;"));
    }

    #[test]
    fn removes_data_copy_buttons() {
        let html = r#"<html><body>
            <pre>
                <button data-copy="true">Copy</button>
                <code>x = 1</code>
            </pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(!result.contains("button"));
        assert!(result.contains("x = 1"));
    }

    #[test]
    fn detects_language_from_parent_div() {
        let html = r#"<html><body>
            <div class="language-typescript">
                <pre><code>const x: number = 1;</code></pre>
            </div>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="typescript""#));
    }

    #[test]
    fn handles_bare_language_class() {
        let html = r#"<html><body>
            <pre><code class="python">x = 1</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(result.contains(r#"data-lang="python""#));
    }

    #[test]
    fn no_language_when_unknown() {
        let html = r#"<html><body>
            <pre><code class="someRandomClass">stuff</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(!result.contains("data-lang"));
        assert!(result.contains("stuff"));
    }

    #[test]
    fn normalizes_tabs_and_nbsp() {
        let html = "<html><body><pre><code>x\tfoo\u{00a0}bar</code></pre></body></html>";
        let result = parse_and_standardize(html);
        // Tabs become 4 spaces, NBSP becomes regular space
        assert!(
            result.contains("x    foo bar"),
            "Expected normalized whitespace in: {result:?}"
        );
    }

    #[test]
    fn removes_rouge_gutter() {
        let html = r#"<html><body>
            <pre><code class="language-ruby">
                <td class="rouge-gutter">1</td>
                <td>puts "hi"</td>
            </code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        assert!(!result.contains("rouge-gutter"));
    }

    #[test]
    fn handles_pre_without_code() {
        let html = r"<html><body>
            <pre>plain preformatted text</pre>
        </body></html>";
        let result = parse_and_standardize(html);
        assert!(result.contains("<pre><code>plain preformatted text</code></pre>"));
    }

    #[test]
    fn preserves_br_as_newlines() {
        let html = r"<html><body>
            <pre><code>line1<br>line2<br>line3</code></pre>
        </body></html>";
        let result = parse_and_standardize(html);
        assert!(result.contains("line1\nline2\nline3"));
    }

    #[test]
    fn collapses_excess_blank_lines() {
        let input = "a\n\n\n\n\nb";
        let result = collapse_blank_lines(input);
        assert_eq!(result, "a\n\nb");
    }

    #[test]
    fn is_known_language_works() {
        assert!(is_known_language("rust"));
        assert!(is_known_language("javascript"));
        assert!(is_known_language("typescript"));
        assert!(is_known_language("python"));
        assert!(is_known_language("go"));
        assert!(is_known_language("nix"));
        assert!(!is_known_language("notareallang"));
    }

    #[test]
    fn detects_code_suffix_pattern() {
        assert_eq!(
            detect_language_from_classes("javascript-code"),
            Some("javascript".to_string())
        );
    }

    #[test]
    fn detects_snippet_suffix_pattern() {
        assert_eq!(
            detect_language_from_classes("python-snippet"),
            Some("python".to_string())
        );
    }

    #[test]
    fn pre_attributes_are_stripped() {
        let html = r#"<html><body>
            <pre class="highlight" data-lang="rust"><code>let x = 1;</code></pre>
        </body></html>"#;
        let result = parse_and_standardize(html);
        // <pre> should have no attributes; data-lang goes on <code>
        assert!(result.contains(r#"<pre><code data-lang="rust">"#));
    }
}