tomesole 0.1.1

A terminal client (TUI and CLI) for searching and downloading from Library Genesis
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
//! A deliberately small HTML scanner.
//!
//! We only ever need to do four things to a Libgen page: find one table by its
//! `id`, split it into rows and cells, pull `href` attributes out of a cell,
//! and flatten a cell to plain text. A full CSS-selector engine and HTML5
//! tree-builder would be a large dependency for that.
//!
//! This is a scanner, not a parser: it never builds a DOM, and it makes no
//! attempt to implement HTML5 error recovery. It is written to tolerate the
//! specific sloppiness Libgen actually emits — unclosed rows, void elements,
//! and stray quotes inside attribute lists (`<span class="badge""> `) — and to
//! degrade to "no match" rather than misbehave on anything else.

/// Elements that never have a closing tag, so depth tracking must not wait for
/// one.
const VOID_ELEMENTS: [&str; 14] = [
    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source",
    "track", "wbr",
];

/// Elements whose content is raw text and may contain `<`, so we skip them
/// wholesale rather than trying to tokenise inside.
const RAW_TEXT_ELEMENTS: [&str; 2] = ["script", "style"];

#[derive(Debug, Clone, PartialEq, Eq)]
struct Tag<'a> {
    name: String,
    /// The attribute section, i.e. everything between the name and the `>`.
    attrs: &'a str,
    closing: bool,
    self_closing: bool,
    /// Byte offset of the opening `<`.
    start: usize,
    /// Byte offset just past the closing `>`.
    end: usize,
}

impl Tag<'_> {
    /// True when this tag opens an element that will need a matching close.
    fn opens_scope(&self) -> bool {
        !self.closing && !self.self_closing && !VOID_ELEMENTS.contains(&self.name.as_str())
    }
}

/// Find the next tag at or after `from`, skipping comments, doctypes, and the
/// contents of raw-text elements.
fn next_tag(html: &str, from: usize) -> Option<Tag<'_>> {
    let bytes = html.as_bytes();
    let mut i = from;

    while i < bytes.len() {
        // Find the next '<' on a char boundary.
        let lt = html[i..].find('<')? + i;
        let rest = &html[lt..];

        if rest.starts_with("<!--") {
            i = html[lt..].find("-->")? + lt + 3;
            continue;
        }
        if rest.starts_with("<!") || rest.starts_with("<?") {
            i = html[lt..].find('>')? + lt + 1;
            continue;
        }

        let after_lt = lt + 1;
        let closing = html[after_lt..].starts_with('/');
        let name_start = if closing { after_lt + 1 } else { after_lt };

        // A tag name must start with an ASCII letter; otherwise this '<' is
        // just text (e.g. "a < b").
        let first = html[name_start..].chars().next();
        if !matches!(first, Some(c) if c.is_ascii_alphabetic()) {
            i = lt + 1;
            continue;
        }

        let name_end = html[name_start..]
            .find(|c: char| !(c.is_ascii_alphanumeric() || c == '-' || c == ':'))
            .map(|off| name_start + off)
            .unwrap_or(html.len());
        let name = html[name_start..name_end].to_ascii_lowercase();

        // Walk to the '>' that ends this tag, honouring quoted attribute
        // values so a '>' inside one does not end the tag early. Libgen puts
        // markup like `title="... <br> ..."` in attributes, so this matters.
        //
        // A quote only *opens* a value when it directly follows `=`. Libgen
        // also emits doubled quotes (`class="badge""`); treating that stray
        // quote as opening a new value would swallow the rest of the document.
        let mut j = name_end;
        let mut quote: Option<u8> = None;
        let mut prev_significant = b' ';
        let mut gt = None;
        while j < bytes.len() {
            let b = bytes[j];
            if let Some(q) = quote {
                if b == q {
                    quote = None;
                    prev_significant = b;
                }
                j += 1;
                continue;
            }
            match b {
                b'"' | b'\'' if prev_significant == b'=' => {
                    quote = Some(b);
                    prev_significant = b;
                }
                b'>' => {
                    gt = Some(j);
                    break;
                }
                _ => {
                    if !b.is_ascii_whitespace() {
                        prev_significant = b;
                    }
                }
            }
            j += 1;
        }
        let gt = gt?;

        let attrs = &html[name_end..gt];
        let self_closing = attrs.trim_end().ends_with('/');

        return Some(Tag {
            name,
            attrs,
            closing,
            self_closing,
            start: lt,
            end: gt + 1,
        });
    }
    None
}

/// Advance past a raw-text element's content, returning the offset of its
/// closing tag.
fn skip_raw_text(html: &str, name: &str, content_start: usize) -> usize {
    let needle = format!("</{name}");
    match html[content_start..].to_ascii_lowercase().find(&needle) {
        Some(off) => content_start + off,
        None => html.len(),
    }
}

/// Given the offset just past an element's opening tag, return the byte range
/// of its inner HTML by walking to the matching close tag.
///
/// When no matching close exists (Libgen omits `</tr>` in places), the element
/// is treated as running until the next sibling of the same name, or to the end
/// of the slice.
fn inner_range(html: &str, name: &str, content_start: usize) -> (usize, usize) {
    let mut depth = 1usize;
    let mut cursor = content_start;
    let mut first_sibling_open: Option<usize> = None;

    while let Some(tag) = next_tag(html, cursor) {
        if RAW_TEXT_ELEMENTS.contains(&tag.name.as_str()) && tag.opens_scope() {
            cursor = skip_raw_text(html, &tag.name, tag.end);
            continue;
        }

        if tag.name == name {
            if tag.closing {
                depth -= 1;
                if depth == 0 {
                    return (content_start, tag.start);
                }
            } else if tag.opens_scope() {
                if depth == 1 && first_sibling_open.is_none() {
                    first_sibling_open = Some(tag.start);
                }
                depth += 1;
            }
        }
        cursor = tag.end;
    }

    // Unclosed. Prefer stopping at the next sibling so a run of unclosed
    // `<tr>`s still splits into separate rows.
    (content_start, first_sibling_open.unwrap_or(html.len()))
}

/// Return the inner HTML of the first `<tag>` whose `attr` equals `value`.
pub fn find_by_attr<'a>(html: &'a str, tag: &str, attr: &str, value: &str) -> Option<&'a str> {
    let mut cursor = 0usize;
    while let Some(t) = next_tag(html, cursor) {
        if !t.closing
            && t.name == tag
            && let Some(found) = get_attr(t.attrs, attr)
            && found.eq_ignore_ascii_case(value)
        {
            let (s, e) = inner_range(html, tag, t.end);
            return Some(&html[s..e]);
        }
        cursor = t.end;
    }
    None
}

/// Return the inner HTML of each top-level `<tag>` in `html`.
///
/// Nested elements of the same name are part of their parent's slice and are
/// not returned separately.
pub fn children<'a>(html: &'a str, tag: &str) -> Vec<&'a str> {
    let mut out = Vec::new();
    let mut cursor = 0usize;
    while let Some(t) = next_tag(html, cursor) {
        if RAW_TEXT_ELEMENTS.contains(&t.name.as_str()) && t.opens_scope() {
            cursor = skip_raw_text(html, &t.name, t.end);
            continue;
        }
        if !t.closing && t.name == tag {
            let (s, e) = inner_range(html, tag, t.end);
            out.push(&html[s..e]);
            // Continue after the element we just consumed.
            cursor = e.max(t.end);
            continue;
        }
        cursor = t.end;
    }
    out
}

/// Read one attribute's value out of a tag's attribute section.
///
/// Tolerates unquoted values and the stray doubled quotes Libgen emits.
pub fn get_attr(attrs: &str, want: &str) -> Option<String> {
    let lower = attrs.to_ascii_lowercase();
    let want_lower = want.to_ascii_lowercase();
    let mut search_from = 0usize;

    while let Some(pos) = lower[search_from..].find(&want_lower) {
        let at = search_from + pos;
        search_from = at + want_lower.len();

        // Must be preceded by whitespace or start-of-attrs, so `href` does not
        // match inside `data-href`.
        let preceded_ok = at == 0
            || attrs[..at]
                .chars()
                .next_back()
                .map(|c| c.is_whitespace())
                .unwrap_or(false);
        if !preceded_ok {
            continue;
        }

        let after = &attrs[at + want_lower.len()..];
        let after_trimmed = after.trim_start();
        if !after_trimmed.starts_with('=') {
            continue;
        }
        let value_part = after_trimmed[1..].trim_start();
        let value = if let Some(rest) = value_part.strip_prefix('"') {
            rest.split('"').next().unwrap_or("")
        } else if let Some(rest) = value_part.strip_prefix('\'') {
            rest.split('\'').next().unwrap_or("")
        } else {
            value_part
                .split(|c: char| c.is_whitespace() || c == '>')
                .next()
                .unwrap_or("")
        };
        return Some(decode_entities(value));
    }
    None
}

/// Collect the `href` of every `<a>` in `html`, in document order.
pub fn hrefs(html: &str) -> Vec<String> {
    anchors(html).into_iter().map(|(href, _)| href).collect()
}

/// Collect the `src` of every `<img>` in `html`, in document order.
///
/// `<img>` has no closing tag, so it cannot go through [`anchors`].
pub fn img_srcs(html: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut cursor = 0usize;
    while let Some(t) = next_tag(html, cursor) {
        if !t.closing
            && t.name == "img"
            && let Some(src) = get_attr(t.attrs, "src")
            && !src.trim().is_empty()
        {
            out.push(src);
        }
        cursor = t.end;
    }
    out
}

/// Collect `(href, inner_html)` for every `<a>` in `html`, in document order.
///
/// Needed because which link a cell means is decided by where it points, not
/// by its position: a Libgen title cell leads with a series link when the book
/// belongs to a series, and the title itself comes second.
pub fn anchors(html: &str) -> Vec<(String, String)> {
    let mut out = Vec::new();
    let mut cursor = 0usize;
    while let Some(t) = next_tag(html, cursor) {
        if !t.closing
            && t.name == "a"
            && let Some(href) = get_attr(t.attrs, "href")
        {
            let (s, e) = inner_range(html, "a", t.end);
            out.push((href, html[s..e].to_string()));
            cursor = e.max(t.end);
            continue;
        }
        cursor = t.end;
    }
    out
}

/// Flatten markup to readable plain text: tags dropped, entities decoded,
/// whitespace collapsed.
pub fn text(html: &str) -> String {
    let mut buf = String::with_capacity(html.len() / 2);
    let mut cursor = 0usize;

    loop {
        match next_tag(html, cursor) {
            Some(t) => {
                buf.push_str(&html[cursor..t.start]);
                // Block-ish separators should not glue words together.
                if matches!(t.name.as_str(), "br" | "td" | "tr" | "p" | "div" | "li") {
                    buf.push(' ');
                }
                if RAW_TEXT_ELEMENTS.contains(&t.name.as_str()) && t.opens_scope() {
                    cursor = skip_raw_text(html, &t.name, t.end);
                } else {
                    cursor = t.end;
                }
            }
            None => {
                buf.push_str(&html[cursor..]);
                break;
            }
        }
    }

    collapse_whitespace(&decode_entities(&buf))
}

fn collapse_whitespace(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut last_was_space = false;
    for c in s.chars() {
        // Non-breaking space counts as whitespace for display purposes.
        if c.is_whitespace() || c == '\u{a0}' {
            if !last_was_space {
                out.push(' ');
            }
            last_was_space = true;
        } else {
            out.push(c);
            last_was_space = false;
        }
    }
    out.trim().to_string()
}

/// Decode the HTML entities that actually turn up in Libgen pages.
pub fn decode_entities(s: &str) -> String {
    if !s.contains('&') {
        return s.to_string();
    }
    let mut out = String::with_capacity(s.len());
    let bytes = s.as_bytes();
    let mut i = 0usize;

    while i < s.len() {
        if bytes[i] != b'&' {
            // Copy one whole char.
            let ch = s[i..].chars().next().expect("valid boundary");
            out.push(ch);
            i += ch.len_utf8();
            continue;
        }

        // Entities are short; a missing ';' is common in the wild so we also
        // accept a bare run of alphanumerics.
        let tail = &s[i + 1..];
        let end = tail
            .find(|c: char| !(c.is_ascii_alphanumeric() || c == '#'))
            .unwrap_or(tail.len());
        let (body, consumed) = if tail[end..].starts_with(';') {
            (&tail[..end], end + 2) // include '&' and ';'
        } else {
            (&tail[..end], end + 1)
        };

        match decode_one(body) {
            Some(decoded) => {
                out.push_str(&decoded);
                i += consumed;
            }
            None => {
                out.push('&');
                i += 1;
            }
        }
    }
    out
}

fn decode_one(body: &str) -> Option<String> {
    if body.is_empty() {
        return None;
    }
    if let Some(num) = body.strip_prefix('#') {
        let code = if let Some(hex) = num.strip_prefix(['x', 'X']) {
            u32::from_str_radix(hex, 16).ok()?
        } else {
            num.parse::<u32>().ok()?
        };
        return char::from_u32(code).map(|c| c.to_string());
    }
    let named = match body.to_ascii_lowercase().as_str() {
        "amp" => "&",
        "lt" => "<",
        "gt" => ">",
        "quot" => "\"",
        "apos" => "'",
        "nbsp" => "\u{a0}",
        "mdash" => "",
        "ndash" => "",
        "hellip" => "",
        "laquo" => "«",
        "raquo" => "»",
        "rsquo" => "",
        "lsquo" => "",
        "ldquo" => "",
        "rdquo" => "",
        _ => return None,
    };
    Some(named.to_string())
}

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

    const FIXTURE: &str = include_str!("../tests/fixtures/li_search.html");

    #[test]
    fn finds_the_right_table_among_several() {
        let table = find_by_attr(FIXTURE, "table", "id", "tablelibgen")
            .expect("fixture contains the results table");
        assert!(!table.contains("decoy table"));
        let rows = children(table, "tr");
        // One header row plus four data rows.
        assert_eq!(rows.len(), 5);
    }

    #[test]
    fn splits_rows_into_nine_cells() {
        let table = find_by_attr(FIXTURE, "table", "id", "tablelibgen").unwrap();
        let rows = children(table, "tr");
        for row in &rows[1..] {
            let cells = children(row, "td");
            assert_eq!(cells.len(), 9, "each data row has nine columns");
        }
    }

    #[test]
    fn extracts_md5_from_the_mirrors_cell() {
        let table = find_by_attr(FIXTURE, "table", "id", "tablelibgen").unwrap();
        let row = children(table, "tr")[1];
        let cells = children(row, "td");
        let links = hrefs(cells[8]);
        assert!(
            links.iter().any(|h| h.contains("ads.php?md5=")),
            "mirrors cell links to ads.php, got {links:?}"
        );
    }

    #[test]
    fn cell_text_is_flattened_and_decoded() {
        let table = find_by_attr(FIXTURE, "table", "id", "tablelibgen").unwrap();
        let row = children(table, "tr")[1];
        let cells = children(row, "td");
        // Year column.
        assert_eq!(text(cells[3]), "2019");
        // Extension column.
        assert_eq!(text(cells[7]), "doc");
        // Language column.
        assert_eq!(text(cells[4]), "English");
    }

    #[test]
    fn tolerates_stray_quotes_in_attributes() {
        // Lifted from a real Libgen row: note the doubled quote.
        let bad = r#"<span class="badge badge-secondary"">l 3359259</span>"#;
        assert_eq!(text(bad), "l 3359259");
    }

    #[test]
    fn attribute_lookup_is_not_fooled_by_prefixes() {
        let attrs = r#" data-href="/decoy" href="/real" "#;
        assert_eq!(get_attr(attrs, "href").as_deref(), Some("/real"));
    }

    #[test]
    fn ignores_greater_than_inside_attribute_values() {
        let html = r#"<div title="a > b"><td>inner</td></div>"#;
        let cells = children(html, "td");
        assert_eq!(cells.len(), 1);
        assert_eq!(text(cells[0]), "inner");
    }

    #[test]
    fn skips_script_contents() {
        let html = r#"<div><script>var x = "<td>fake</td>";</script><td>real</td></div>"#;
        let inner = children(html, "div");
        let cells = children(inner[0], "td");
        assert_eq!(cells.len(), 1);
        assert_eq!(text(cells[0]), "real");
    }

    #[test]
    fn handles_unclosed_rows() {
        let html = "<table><tr><td>one</td><tr><td>two</td></table>";
        let table = children(html, "table")[0];
        let rows = children(table, "tr");
        assert_eq!(rows.len(), 2);
        assert_eq!(text(rows[0]), "one");
        assert_eq!(text(rows[1]), "two");
    }

    #[test]
    fn nested_tables_do_not_split_into_extra_rows() {
        let html =
            "<table id=\"x\"><tr><td><table><tr><td>inner</td></tr></table></td></tr></table>";
        let outer = find_by_attr(html, "table", "id", "x").unwrap();
        assert_eq!(children(outer, "tr").len(), 1);
    }

    #[test]
    fn decodes_entities() {
        assert_eq!(decode_entities("a &amp; b"), "a & b");
        assert_eq!(decode_entities("&lt;tag&gt;"), "<tag>");
        assert_eq!(decode_entities("&#8597;"), "");
        assert_eq!(decode_entities("&#x41;"), "A");
        assert_eq!(decode_entities("caf&eacute;"), "caf&eacute;"); // unknown, left alone
        assert_eq!(decode_entities("100% &"), "100% &");
    }

    #[test]
    fn text_does_not_glue_words_across_tags() {
        assert_eq!(text("<td>a</td><td>b</td>"), "a b");
        assert_eq!(text("one<br>two"), "one two");
    }

    #[test]
    fn missing_table_returns_none() {
        assert!(find_by_attr("<html><body>nope</body></html>", "table", "id", "x").is_none());
    }

    #[test]
    fn malformed_input_does_not_panic() {
        for junk in [
            "<",
            "<<<>>>",
            "<table id=",
            "<table id=\"x\">unclosed",
            "a < b > c",
            "<!-- unterminated",
            "<td><td><td>",
            "",
        ] {
            let _ = find_by_attr(junk, "table", "id", "x");
            let _ = children(junk, "tr");
            let _ = text(junk);
            let _ = hrefs(junk);
        }
    }
}