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

use scraper::Html;

use crate::dom;

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

/// Detect whether this page is a Lobste.rs story page.
#[must_use]
pub fn is_lobsters(url: Option<&str>) -> bool {
    url.is_some_and(|u| u.contains("lobste.rs/s/"))
}

/// Extract content from a Lobste.rs story page.
///
/// When `include_replies` is false, comments are omitted.
/// Falls back to the Lobste.rs JSON API when HTML extraction
/// yields fewer than 10 words.
#[must_use]
pub fn extract_lobsters(
    html: &Html,
    url: Option<&str>,
    include_replies: bool,
) -> Option<ExtractorResult> {
    if !is_lobsters(url) {
        return None;
    }

    let result = extract_from_html(html, include_replies);

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

fn extract_from_html(html: &Html, include_replies: bool) -> Option<ExtractorResult> {
    let title = extract_title(html);
    let story_body = extract_story_body(html);
    let story_link = extract_story_link(html);

    if story_body.is_empty() && story_link.is_empty() {
        return None;
    }

    let mut post_html = String::new();
    if !story_link.is_empty() {
        let escaped = dom::html_attr_escape(&story_link);
        let _ = write!(
            post_html,
            "<p><a href=\"{escaped}\" target=\"_blank\">\
             {escaped}</a></p>"
        );
    }
    if !story_body.is_empty() {
        let _ = write!(post_html, "<div class=\"post-text\">{story_body}</div>");
    }

    let comments_html = if include_replies {
        extract_comments(html)
    } else {
        String::new()
    };

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

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

fn extract_title(html: &Html) -> String {
    let ids = dom::select_ids(html, ".u-repost-of");
    ids.first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default()
}

fn extract_story_body(html: &Html) -> String {
    // Lobsters stories with text have a .description div
    let ids = dom::select_ids(html, ".story_text");
    ids.first()
        .map(|&id| dom::inner_html(html, id).trim().to_string())
        .unwrap_or_default()
}

fn extract_story_link(html: &Html) -> String {
    let ids = dom::select_ids(html, ".u-repost-of");
    ids.first()
        .and_then(|&id| dom::get_attr(html, id, "href"))
        .unwrap_or_default()
}

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

    let mut comments = Vec::new();
    for &cid in &comment_ids {
        let body = dom::inner_html(html, cid).trim().to_string();
        if body.is_empty() {
            continue;
        }

        // Walk up to the .comment container to get metadata
        let container = find_comment_container(html, cid);
        let (author, depth, score) = container.map_or_else(
            || (String::new(), 0, String::new()),
            |c| extract_comment_meta(html, c),
        );

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

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

/// Walk up from a `.comment_text` node to the nearest `.comment`
/// ancestor (via `li.comments_subtree`).
fn find_comment_container(html: &Html, node_id: ego_tree::NodeId) -> Option<ego_tree::NodeId> {
    let mut current = Some(node_id);
    while let Some(nid) = current {
        if dom::has_class(html, nid, "comment") {
            return Some(nid);
        }
        current = dom::parent_element(html, nid);
    }
    None
}

fn extract_comment_meta(html: &Html, container: ego_tree::NodeId) -> (String, usize, String) {
    let author_ids = dom::select_within(html, container, ".comment_author a");
    let author = author_ids
        .first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default();

    // Lobsters uses inline style for depth indentation;
    // fall back to 0 if we can't determine it
    let depth = 0;

    let score_ids = dom::select_within(html, container, ".comment_score");
    let score = score_ids
        .first()
        .map(|&id| dom::text_content(html, id).trim().to_string())
        .unwrap_or_default();

    (author, depth, score)
}

// --- API fallback ---

/// Parse the short ID from a Lobste.rs URL.
///
/// `https://lobste.rs/s/abc123/story_title` -> `abc123`
fn parse_short_id(url: &str) -> Option<&str> {
    let after_s = url.split("lobste.rs/s/").nth(1)?;
    let id = after_s.split('/').next()?;
    if id.is_empty() {
        return None;
    }
    Some(id)
}

/// Build the JSON API URL from the page URL.
fn api_url(url: &str) -> Option<String> {
    let id = parse_short_id(url)?;
    Some(format!("https://lobste.rs/s/{id}.json"))
}

fn try_api_fetch(url: Option<&str>, include_replies: bool) -> Option<ExtractorResult> {
    let u = url?;
    let api = api_url(u)?;
    let json = fetch_lobsters_json(&api)?;

    build_from_api(&json, include_replies)
}

fn build_from_api(json: &serde_json::Value, include_replies: bool) -> Option<ExtractorResult> {
    let title = lobsters_json_str(json, "title");
    let description = json
        .get("description")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("");
    let story_url = lobsters_json_str(json, "url");
    let submitter = json
        .get("submitter_user")
        .and_then(|u| u.get("username"))
        .and_then(serde_json::Value::as_str)
        .unwrap_or("")
        .to_string();

    let mut post_html = String::new();
    if !story_url.is_empty() {
        let escaped = dom::html_attr_escape(&story_url);
        let _ = write!(
            post_html,
            "<p><a href=\"{escaped}\" target=\"_blank\">\
             {escaped}</a></p>"
        );
    }
    if !description.trim().is_empty() {
        let _ = write!(post_html, "<div class=\"post-text\">{description}</div>");
    }

    if post_html.is_empty() {
        return None;
    }

    let comments_html = if include_replies {
        build_api_comments(json)
    } else {
        String::new()
    };

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

    Some(ExtractorResult {
        content,
        title: if title.is_empty() { None } else { Some(title) },
        author: if submitter.is_empty() {
            None
        } else {
            Some(submitter)
        },
        site: Some("Lobsters".to_string()),
        published: json
            .get("created_at")
            .and_then(serde_json::Value::as_str)
            .and_then(|d| d.split('T').next())
            .map(String::from),
        image: None,
        description: None,
    })
}

fn build_api_comments(json: &serde_json::Value) -> String {
    let Some(comments_arr) = json.get("comments").and_then(serde_json::Value::as_array) else {
        return String::new();
    };

    let mut comments = Vec::new();
    for c in comments_arr {
        let body = c
            .get("comment")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("");
        if body.trim().is_empty() {
            continue;
        }

        let author = c
            .get("commenting_user")
            .and_then(|u| u.get("username"))
            .and_then(serde_json::Value::as_str)
            .unwrap_or("")
            .to_string();

        let depth = c
            .get("depth")
            .and_then(serde_json::Value::as_u64)
            .and_then(|d| usize::try_from(d).ok())
            .unwrap_or(0);

        let score = c
            .get("score")
            .and_then(serde_json::Value::as_i64)
            .unwrap_or(0);

        comments.push(CommentData {
            author,
            date: String::new(),
            content: body.to_string(),
            depth,
            score: Some(format!("{score} points")),
            url: None,
        });
    }

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

fn fetch_lobsters_json(url: &str) -> Option<serde_json::Value> {
    let body = crate::http::get(url)?;
    serde_json::from_str(&body).ok()
}

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

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

    #[test]
    fn detect_lobsters_url() {
        assert!(is_lobsters(Some("https://lobste.rs/s/abc123/some_story")));
        assert!(is_lobsters(Some("https://lobste.rs/s/xyz")));
    }

    #[test]
    fn reject_non_lobsters_url() {
        assert!(!is_lobsters(Some("https://example.com")));
        assert!(!is_lobsters(Some("https://lobste.rs/")));
        assert!(!is_lobsters(None));
    }

    #[test]
    fn parse_short_id_valid() {
        assert_eq!(
            parse_short_id("https://lobste.rs/s/abc123/some_title"),
            Some("abc123")
        );
        assert_eq!(parse_short_id("https://lobste.rs/s/xyz"), Some("xyz"));
    }

    #[test]
    fn parse_short_id_invalid() {
        assert!(parse_short_id("https://example.com").is_none());
    }

    #[test]
    fn api_url_construction() {
        assert_eq!(
            api_url("https://lobste.rs/s/abc123/story"),
            Some("https://lobste.rs/s/abc123.json".to_string())
        );
    }

    #[test]
    fn extract_from_html_basic() {
        let html_str = r#"
        <html>
        <body>
        <a class="u-repost-of" href="https://example.com/article">
            A Cool Story
        </a>
        <div class="comment">
            <span class="comment_author">
                <a href="/u/alice">alice</a>
            </span>
            <div class="comment_text">
                <p>This is a great article.</p>
            </div>
        </div>
        </body>
        </html>
        "#;
        let html = Html::parse_document(html_str);
        let result = extract_from_html(&html, true).unwrap();

        assert_eq!(result.title.as_deref(), Some("A Cool Story"));
        assert!(result.content.contains("example.com/article"));
        assert!(result.content.contains("great article"));
    }

    #[test]
    fn build_from_api_basic() {
        let json = serde_json::json!({
            "title": "Test Story",
            "description": "<p>Story description</p>",
            "url": "https://example.com/test",
            "score": 42,
            "submitter_user": {"username": "alice"},
            "tags": ["rust"],
            "created_at": "2025-01-15T12:00:00Z",
            "comments": [
                {
                    "comment": "<p>Nice!</p>",
                    "commenting_user": {"username": "bob"},
                    "depth": 0,
                    "score": 5,
                    "created_at": "2025-01-15T13:00:00Z"
                },
                {
                    "comment": "<p>Agreed</p>",
                    "commenting_user": {"username": "carol"},
                    "depth": 1,
                    "score": 2,
                    "created_at": "2025-01-15T14:00:00Z"
                }
            ]
        });

        let result = build_from_api(&json, true).unwrap();
        assert_eq!(result.title.as_deref(), Some("Test Story"));
        assert_eq!(result.author.as_deref(), Some("alice"));
        assert_eq!(result.published.as_deref(), Some("2025-01-15"));
        assert!(result.content.contains("example.com/test"));
        assert!(result.content.contains("Nice!"));
        assert!(result.content.contains("bob"));
        assert!(result.content.contains("Agreed"));
    }

    #[test]
    fn api_fetch_live() {
        let url = "https://lobste.rs/s/abc123";
        let result = try_api_fetch(Some(url), false);
        // Don't assert success — just verify it doesn't crash
        drop(result);
    }
}