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
use std::fmt::Write;

use scraper::{Html, Selector};

use crate::dom;

use super::ExtractorResult;
use super::comments::{CommentData, build_comment, build_comment_tree, build_content_html};

/// Detect whether this page is a Hacker News page.
#[must_use]
pub fn is_hackernews(html: &Html, url: Option<&str>) -> bool {
    if url.is_some_and(|u| u.contains("news.ycombinator.com")) {
        return true;
    }
    Selector::parse(".fatitem")
        .ok()
        .is_some_and(|sel| html.select(&sel).next().is_some())
}

/// Extract content from a Hacker News page.
///
/// When `include_replies` is false, comments are omitted.
/// Falls back to the HN Firebase API when HTML extraction yields
/// fewer than 10 words (e.g., JS-rendered or empty pages).
#[must_use]
pub fn extract_hackernews(
    html: &Html,
    url: Option<&str>,
    include_replies: bool,
) -> Option<ExtractorResult> {
    if !is_hackernews(html, url) {
        return None;
    }

    let fatitem_ids = dom::select_ids(html, ".fatitem");
    let has_fatitem = fatitem_ids.first().copied().is_some();

    let result = if has_fatitem {
        let is_comment_page = detect_comment_page(html);
        if is_comment_page {
            extract_comment_page(html, url, include_replies)
        } else {
            extract_story_page(html, url, include_replies)
        }
    } else {
        None
    };

    match result {
        Some(ref r) if dom::count_words_html(&r.content) >= 10 => result,
        _ => try_api_fetch(url, include_replies).or(result),
    }
}

fn detect_comment_page(html: &Html) -> bool {
    let fatitem_ids = dom::select_ids(html, ".fatitem");
    let Some(&fatitem) = fatitem_ids.first() else {
        return false;
    };
    let has_onstory = !dom::select_within(html, fatitem, ".onstory").is_empty();
    let has_titleline = !dom::select_within(html, fatitem, ".titleline").is_empty();
    has_onstory && !has_titleline
}

// --- Story page extraction ---

fn extract_story_page(
    html: &Html,
    _url: Option<&str>,
    include_replies: bool,
) -> Option<ExtractorResult> {
    let fatitem_ids = dom::select_ids(html, ".fatitem");
    let fatitem = fatitem_ids.first().copied()?;

    let title = extract_story_title(html, fatitem);
    let author = extract_author(html, fatitem);
    let post_content = extract_story_content(html, fatitem);
    let comments = if include_replies {
        extract_comments(html)
    } else {
        String::new()
    };
    let content = build_content_html("hackernews", &post_content, &comments);

    let published = extract_date(html, fatitem);

    Some(ExtractorResult {
        content,
        title: Some(title),
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("Hacker News".to_string()),
        published: if published.is_empty() {
            None
        } else {
            Some(published)
        },
        image: None,
        description: None,
    })
}

fn extract_story_title(html: &Html, fatitem: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, fatitem, ".titleline");
    ids.first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default()
}

fn extract_author(html: &Html, container: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, container, ".hnuser");
    ids.first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default()
}

fn extract_date(html: &Html, container: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, container, ".age");
    ids.first()
        .and_then(|&id| dom::get_attr(html, id, "title"))
        .and_then(|dt| dt.split('T').next().map(String::from))
        .unwrap_or_default()
}

fn extract_story_content(html: &Html, fatitem: ego_tree::NodeId) -> String {
    use std::fmt::Write;
    let mut content = String::new();

    // Story link
    let link_ids = dom::select_within(html, fatitem, ".titleline a");
    if let Some(&link_id) = link_ids.first()
        && let Some(href) = dom::get_attr(html, link_id, "href")
    {
        let escaped = html_attr_escape(&href);
        let _ = write!(
            content,
            "<p><a href=\"{escaped}\" target=\"_blank\">{escaped}</a></p>"
        );
    }

    // Self-text (Ask HN, Show HN)
    let text_ids = dom::select_within(html, fatitem, ".toptext");
    if let Some(&text_id) = text_ids.first() {
        let text_html = dom::inner_html(html, text_id);
        let _ = write!(
            content,
            "<div class=\"post-text\">{}</div>",
            text_html.trim()
        );
    }

    content
}

// --- Comment page extraction ---

fn extract_comment_page(
    html: &Html,
    _url: Option<&str>,
    include_replies: bool,
) -> Option<ExtractorResult> {
    let fatitem_ids = dom::select_ids(html, ".fatitem");
    let fatitem = fatitem_ids.first().copied()?;

    let main_comment = extract_main_comment(html, fatitem)?;
    let author = main_comment.author.clone();
    let title = build_comment_title(&main_comment);
    let post_content = build_comment(&main_comment);
    let comments = if include_replies {
        extract_comments(html)
    } else {
        String::new()
    };
    let content = build_content_html("hackernews", &post_content, &comments);

    Some(ExtractorResult {
        content,
        title: Some(title),
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("Hacker News".to_string()),
        published: None,
        image: None,
        description: None,
    })
}

fn extract_main_comment(html: &Html, fatitem: ego_tree::NodeId) -> Option<CommentData> {
    let athing_ids = dom::select_within(html, fatitem, "tr.athing");
    let athing = athing_ids.first().copied()?;

    let author = extract_author(html, athing);
    let date = extract_date(html, athing);

    let commtext_ids = dom::select_within(html, athing, ".commtext");
    let content = commtext_ids
        .first()
        .map(|&id| dom::inner_html(html, id).trim().to_string())
        .unwrap_or_default();

    if content.is_empty() {
        return None;
    }

    Some(CommentData {
        author,
        date,
        content,
        depth: 0,
        score: None,
        url: None,
    })
}

fn build_comment_title(comment: &CommentData) -> String {
    let text = dom::strip_html_tags(&comment.content);
    let trimmed = text.trim();
    let preview = match trimmed.char_indices().nth(50) {
        Some((i, _)) => format!("{}...", &trimmed[..i]),
        None => trimmed.to_string(),
    };
    format!("Comment by {}: {preview}", comment.author)
}

fn html_attr_escape(s: &str) -> String {
    dom::html_attr_escape(s)
}

// --- Comment extraction (shared) ---

fn extract_comments(html: &Html) -> String {
    let comment_ids = dom::select_ids(html, "tr.comtr");
    if comment_ids.is_empty() {
        return String::new();
    }

    let mut comments = Vec::new();
    let mut processed = std::collections::HashSet::new();

    for &cid in &comment_ids {
        let Some(id_attr) = dom::get_attr(html, cid, "id") else {
            continue;
        };
        if !processed.insert(id_attr.clone()) {
            continue;
        }

        let depth = extract_indent_depth(html, cid);
        let author = extract_comment_author(html, cid);
        let date = extract_comment_date(html, cid);
        let score = extract_comment_score(html, cid);

        let commtext_ids = dom::select_within(html, cid, ".commtext");
        let body = commtext_ids
            .first()
            .map(|&id| dom::inner_html(html, id).trim().to_string())
            .unwrap_or_default();

        if body.is_empty() {
            continue;
        }

        let comment_url = format!("https://news.ycombinator.com/item?id={id_attr}");

        comments.push(CommentData {
            author,
            date,
            content: body,
            depth,
            score: if score.is_empty() { None } else { Some(score) },
            url: Some(comment_url),
        });
    }

    if comments.is_empty() {
        return String::new();
    }
    build_comment_tree(&comments)
}

fn extract_indent_depth(html: &Html, comment_id: ego_tree::NodeId) -> usize {
    let img_ids = dom::select_within(html, comment_id, ".ind img");
    img_ids
        .first()
        .and_then(|&id| dom::get_attr(html, id, "width"))
        .and_then(|w| w.parse::<usize>().ok())
        .map_or(0, |w| w / 40)
}

fn extract_comment_author(html: &Html, comment_id: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, comment_id, ".hnuser");
    ids.first().map_or_else(
        || "[deleted]".to_string(),
        |&id| dom::text_content(html, id).trim().to_string(),
    )
}

fn extract_comment_date(html: &Html, comment_id: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, comment_id, ".age");
    ids.first()
        .and_then(|&id| dom::get_attr(html, id, "title"))
        .and_then(|dt| dt.split('T').next().map(String::from))
        .unwrap_or_default()
}

fn extract_comment_score(html: &Html, comment_id: ego_tree::NodeId) -> String {
    let ids = dom::select_within(html, comment_id, ".score");
    ids.first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default()
}

// --- API fallback ---

const HN_API_BASE: &str = "https://hacker-news.firebaseio.com/v0/item";
const HN_MAX_COMMENT_DEPTH: usize = 3;
const HN_MAX_COMMENTS: usize = 20;

/// Extract the item ID from a Hacker News URL.
fn parse_hn_item_id(url: &str) -> Option<&str> {
    // news.ycombinator.com/item?id=12345
    let query = url.split("id=").nth(1)?;
    let id = query.split('&').next()?;
    if id.is_empty() || !id.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    Some(id)
}

/// Fetch content from the HN Firebase API.
fn try_api_fetch(url: Option<&str>, include_replies: bool) -> Option<ExtractorResult> {
    let id = parse_hn_item_id(url?)?;
    let json = fetch_hn_json(id)?;

    let item_type = json
        .get("type")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("");

    match item_type {
        "story" | "job" | "poll" => Some(build_story_from_api(&json, include_replies)),
        "comment" => build_comment_from_api(&json, include_replies),
        _ => None,
    }
}

/// Build an `ExtractorResult` for a story item from the API.
fn build_story_from_api(json: &serde_json::Value, include_replies: bool) -> ExtractorResult {
    let title = hn_json_str(json, "title");
    let author = hn_json_str(json, "by");
    let published = format_unix_timestamp(json);

    let mut post_html = String::new();
    if let Some(link) = json.get("url").and_then(serde_json::Value::as_str) {
        let escaped = dom::html_attr_escape(link);
        let _ = write!(
            post_html,
            "<p><a href=\"{escaped}\" target=\"_blank\">{escaped}</a></p>"
        );
    }
    if let Some(text) = json.get("text").and_then(serde_json::Value::as_str) {
        let _ = write!(post_html, "<div class=\"post-text\">{text}</div>");
    }

    let comments_html = if include_replies {
        let mut count = 0;
        fetch_comment_kids(json, 0, &mut count)
    } else {
        String::new()
    };

    let content = build_content_html("hackernews", &post_html, &comments_html);

    ExtractorResult {
        content,
        title: if title.is_empty() { None } else { Some(title) },
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("Hacker News".to_string()),
        published: if published.is_empty() {
            None
        } else {
            Some(published)
        },
        image: None,
        description: None,
    }
}

/// Build an `ExtractorResult` for a single comment from the API.
fn build_comment_from_api(
    json: &serde_json::Value,
    include_replies: bool,
) -> Option<ExtractorResult> {
    let text = json.get("text").and_then(serde_json::Value::as_str)?;
    if text.trim().is_empty() {
        return None;
    }
    let author = hn_json_str(json, "by");

    let plain = dom::strip_html_tags(text);
    let trimmed = plain.trim();
    let preview = match trimmed.char_indices().nth(50) {
        Some((i, _)) => format!("{}...", &trimmed[..i]),
        None => trimmed.to_string(),
    };
    let title = format!("Comment by {author}: {preview}");

    let main_comment = CommentData {
        author: author.clone(),
        date: format_unix_timestamp(json),
        content: text.to_string(),
        depth: 0,
        score: None,
        url: None,
    };
    let post_html = build_comment(&main_comment);

    let comments_html = if include_replies {
        let mut count = 0;
        fetch_comment_kids(json, 0, &mut count)
    } else {
        String::new()
    };

    let content = build_content_html("hackernews", &post_html, &comments_html);

    Some(ExtractorResult {
        content,
        title: Some(title),
        author: if author.is_empty() {
            None
        } else {
            Some(author)
        },
        site: Some("Hacker News".to_string()),
        published: None,
        image: None,
        description: None,
    })
}

/// Recursively fetch child comments from the HN API into a flat
/// list, then build a comment tree.
fn fetch_comment_kids(parent: &serde_json::Value, depth: usize, count: &mut usize) -> String {
    let mut comments = Vec::new();
    collect_kids_flat(parent, depth, count, &mut comments);
    if comments.is_empty() {
        return String::new();
    }
    build_comment_tree(&comments)
}

/// Collect nested comments into a flat Vec with depth info.
fn collect_kids_flat(
    parent: &serde_json::Value,
    depth: usize,
    count: &mut usize,
    out: &mut Vec<CommentData>,
) {
    if depth >= HN_MAX_COMMENT_DEPTH || *count >= HN_MAX_COMMENTS {
        return;
    }

    let Some(kids) = parent.get("kids").and_then(serde_json::Value::as_array) else {
        return;
    };

    for kid_val in kids {
        if *count >= HN_MAX_COMMENTS {
            break;
        }
        let Some(kid_id) = kid_val.as_u64() else {
            continue;
        };
        let id_str = kid_id.to_string();
        let Some(child_json) = fetch_hn_json(&id_str) else {
            continue;
        };
        if child_json
            .get("deleted")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false)
        {
            continue;
        }

        let text = child_json
            .get("text")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("");
        if text.trim().is_empty() {
            continue;
        }

        *count += 1;
        out.push(CommentData {
            author: hn_json_str(&child_json, "by"),
            date: format_unix_timestamp(&child_json),
            content: text.to_string(),
            depth,
            score: None,
            url: Some(format!("https://news.ycombinator.com/item?id={kid_id}")),
        });

        collect_kids_flat(&child_json, depth + 1, count, out);
    }
}

/// Fetch a single item from the HN Firebase API.
fn fetch_hn_json(id: &str) -> Option<serde_json::Value> {
    let url = format!("{HN_API_BASE}/{id}.json");
    let body = crate::http::get(&url)?;
    serde_json::from_str(&body).ok()
}

fn hn_json_str(json: &serde_json::Value, key: &str) -> String {
    json.get(key)
        .and_then(serde_json::Value::as_str)
        .unwrap_or("")
        .to_string()
}

/// Format a unix timestamp field into a `YYYY-MM-DD` date string.
fn format_unix_timestamp(json: &serde_json::Value) -> String {
    let Some(ts) = json.get("time").and_then(serde_json::Value::as_i64) else {
        return String::new();
    };
    // Manual conversion from unix timestamp to YYYY-MM-DD
    // Using days-since-epoch arithmetic to avoid pulling in chrono
    let secs_per_day: i64 = 86400;
    let days = ts / secs_per_day;
    // Algorithm from Howard Hinnant's civil_from_days
    let z = days + 719_468;
    let era = (if z >= 0 { z } else { z - 146_096 }) / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    format!("{y:04}-{m:02}-{d:02}")
}

#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    fn load_fixture(name: &str) -> String {
        let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR"));
        std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("fixture not found at {path}: {e}"))
    }

    #[test]
    fn parse_hn_item_id_valid() {
        assert_eq!(
            parse_hn_item_id("https://news.ycombinator.com/item?id=12345678"),
            Some("12345678")
        );
    }

    #[test]
    fn parse_hn_item_id_with_extra_params() {
        assert_eq!(
            parse_hn_item_id("https://news.ycombinator.com/item?id=999&p=2"),
            Some("999")
        );
    }

    #[test]
    fn parse_hn_item_id_invalid() {
        assert!(parse_hn_item_id("https://news.ycombinator.com/").is_none());
        assert!(parse_hn_item_id("https://example.com").is_none());
    }

    #[test]
    fn format_timestamp_known_date() {
        // 2024-01-15 00:00:00 UTC = 1705276800
        let json = serde_json::json!({"time": 1_705_276_800});
        assert_eq!(format_unix_timestamp(&json), "2024-01-15");
    }

    #[test]
    fn format_timestamp_missing() {
        let json = serde_json::json!({});
        assert_eq!(format_unix_timestamp(&json), "");
    }

    #[test]
    fn api_fetch_live_story() {
        // Item 1 is the first HN post
        let url = "https://news.ycombinator.com/item?id=1";
        let result = try_api_fetch(Some(url), false);
        if let Some(r) = result {
            assert!(r.title.is_some());
            assert_eq!(r.site.as_deref(), Some("Hacker News"));
        }
        // Don't fail if network is unavailable
    }

    #[test]
    fn extract_hn_comment_page() {
        let html_str = load_fixture("general--news.ycombinator.com-item-id=12345678.html");
        let url = Some("https://news.ycombinator.com/item?id=12345678");
        let html = Html::parse_document(&html_str);

        assert!(is_hackernews(&html, url));
        let result = extract_hackernews(&html, url, true).unwrap();

        assert!(result.title.unwrap().contains("testuser"));
        assert_eq!(result.author.as_deref(), Some("testuser"));
        assert!(result.content.contains("main comment text"));
    }

    #[test]
    fn build_story_from_api_canned_json() {
        let json = serde_json::json!({
            "type": "story",
            "title": "Show HN: A Rust parser",
            "by": "rustfan",
            "url": "https://example.com/parser",
            "text": "I built a parser in Rust. It's fast.",
            "time": 1_705_276_800_i64,
            "kids": []
        });

        let result = build_story_from_api(&json, false);
        assert_eq!(result.title.as_deref(), Some("Show HN: A Rust parser"));
        assert_eq!(result.author.as_deref(), Some("rustfan"));
        assert_eq!(result.published.as_deref(), Some("2024-01-15"));
        assert_eq!(result.site.as_deref(), Some("Hacker News"));
        assert!(result.content.contains("example.com/parser"));
        assert!(result.content.contains("parser in Rust"));
    }

    #[test]
    fn build_story_from_api_no_text() {
        let json = serde_json::json!({
            "type": "story",
            "title": "Link-only story",
            "by": "poster",
            "url": "https://example.com/article",
            "time": 1_705_276_800_i64
        });

        let result = build_story_from_api(&json, false);
        assert_eq!(result.title.as_deref(), Some("Link-only story"));
        assert!(result.content.contains("example.com/article"));
    }

    #[test]
    fn extract_hn_story_with_comments() {
        let html_str = load_fixture("comments--news.ycombinator.com-item-id=12345678.html");
        let url = Some("https://news.ycombinator.com/item?id=12345678");
        let html = Html::parse_document(&html_str);

        assert!(is_hackernews(&html, url));
        let result = extract_hackernews(&html, url, true).unwrap();

        assert_eq!(result.title.as_deref(), Some("A Sample Article"));
        assert_eq!(result.author.as_deref(), Some("author_one"));
        assert_eq!(result.site.as_deref(), Some("Hacker News"));
        assert!(result.content.contains("example.com/article"));
        assert!(result.content.contains("Comments"));
        assert!(result.content.contains("commenter_one"));
        assert!(result.content.contains("distributed systems"));
    }
}