halldyll-parser 0.1.0

HTML/CSS parsing and content extraction for halldyll scraper
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
//! CSS Selector utilities for halldyll-parser
//!
//! This module provides:
//! - Pre-compiled, cached CSS selectors
//! - Safe selector parsing with error handling
//! - Common selector patterns for web scraping

use lazy_static::lazy_static;
use scraper::Selector;
use std::collections::HashMap;
use std::sync::RwLock;

use crate::types::{ParserError, ParserResult};

// ============================================================================
// CACHED SELECTORS (PRE-COMPILED)
// ============================================================================

/// Pre-compiled selectors for common elements
pub struct CachedSelectors {
    // Metadata selectors
    pub title: Selector,
    pub meta: Selector,
    pub link: Selector,
    pub base: Selector,
    pub html: Selector,
    
    // Content selectors
    pub body: Selector,
    pub article: Selector,
    pub main: Selector,
    pub main_role: Selector,
    
    // Heading selectors
    pub h1: Selector,
    pub h2: Selector,
    pub h3: Selector,
    pub h4: Selector,
    pub h5: Selector,
    pub h6: Selector,
    
    // Text content selectors
    pub p: Selector,
    pub blockquote: Selector,
    pub pre: Selector,
    pub pre_code: Selector,
    pub code: Selector,
    
    // List selectors
    pub ul: Selector,
    pub ol: Selector,
    pub li: Selector,
    pub dl: Selector,
    pub dt: Selector,
    pub dd: Selector,
    
    // Table selectors
    pub table: Selector,
    pub thead: Selector,
    pub tbody: Selector,
    pub tfoot: Selector,
    pub tr: Selector,
    pub th: Selector,
    pub td: Selector,
    pub caption: Selector,
    
    // Link and image selectors
    pub a: Selector,
    pub img: Selector,
    pub picture: Selector,
    pub source: Selector,
    pub figure: Selector,
    pub figcaption: Selector,
    
    // Script/style (for removal)
    pub script: Selector,
    pub style: Selector,
    pub noscript: Selector,
    
    // Structural
    pub nav: Selector,
    pub header: Selector,
    pub footer: Selector,
    pub aside: Selector,
    
    // Structured data
    pub json_ld: Selector,
    pub microdata: Selector,
}

impl CachedSelectors {
    /// Create all cached selectors
    fn new() -> Self {
        Self {
            // Metadata
            title: Selector::parse("title").unwrap(),
            meta: Selector::parse("meta").unwrap(),
            link: Selector::parse("link").unwrap(),
            base: Selector::parse("base").unwrap(),
            html: Selector::parse("html").unwrap(),
            
            // Content
            body: Selector::parse("body").unwrap(),
            article: Selector::parse("article").unwrap(),
            main: Selector::parse("main").unwrap(),
            main_role: Selector::parse("[role=main]").unwrap(),
            
            // Headings
            h1: Selector::parse("h1").unwrap(),
            h2: Selector::parse("h2").unwrap(),
            h3: Selector::parse("h3").unwrap(),
            h4: Selector::parse("h4").unwrap(),
            h5: Selector::parse("h5").unwrap(),
            h6: Selector::parse("h6").unwrap(),
            
            // Text
            p: Selector::parse("p").unwrap(),
            blockquote: Selector::parse("blockquote").unwrap(),
            pre: Selector::parse("pre").unwrap(),
            pre_code: Selector::parse("pre code").unwrap(),
            code: Selector::parse("code").unwrap(),
            
            // Lists
            ul: Selector::parse("ul").unwrap(),
            ol: Selector::parse("ol").unwrap(),
            li: Selector::parse("li").unwrap(),
            dl: Selector::parse("dl").unwrap(),
            dt: Selector::parse("dt").unwrap(),
            dd: Selector::parse("dd").unwrap(),
            
            // Tables
            table: Selector::parse("table").unwrap(),
            thead: Selector::parse("thead").unwrap(),
            tbody: Selector::parse("tbody").unwrap(),
            tfoot: Selector::parse("tfoot").unwrap(),
            tr: Selector::parse("tr").unwrap(),
            th: Selector::parse("th").unwrap(),
            td: Selector::parse("td").unwrap(),
            caption: Selector::parse("caption").unwrap(),
            
            // Links and images
            a: Selector::parse("a").unwrap(),
            img: Selector::parse("img").unwrap(),
            picture: Selector::parse("picture").unwrap(),
            source: Selector::parse("source").unwrap(),
            figure: Selector::parse("figure").unwrap(),
            figcaption: Selector::parse("figcaption").unwrap(),
            
            // Script/style
            script: Selector::parse("script").unwrap(),
            style: Selector::parse("style").unwrap(),
            noscript: Selector::parse("noscript").unwrap(),
            
            // Structural
            nav: Selector::parse("nav").unwrap(),
            header: Selector::parse("header").unwrap(),
            footer: Selector::parse("footer").unwrap(),
            aside: Selector::parse("aside").unwrap(),
            
            // Structured data
            json_ld: Selector::parse("script[type='application/ld+json']").unwrap(),
            microdata: Selector::parse("[itemscope]").unwrap(),
        }
    }
}

// Global cached selectors instance
lazy_static! {
    pub static ref SELECTORS: CachedSelectors = CachedSelectors::new();
}

// ============================================================================
// DYNAMIC SELECTOR CACHE
// ============================================================================

// Cache for dynamically created selectors
lazy_static! {
    static ref SELECTOR_CACHE: RwLock<HashMap<String, Selector>> = 
        RwLock::new(HashMap::new());
}

/// Get or create a selector from the cache
pub fn get_or_create_selector(selector_str: &str) -> ParserResult<Selector> {
    // Check read cache first
    {
        let cache = SELECTOR_CACHE.read().unwrap();
        if let Some(sel) = cache.get(selector_str) {
            return Ok(sel.clone());
        }
    }
    
    // Parse and cache
    let selector = parse_selector(selector_str)?;
    
    // Store in cache
    {
        let mut cache = SELECTOR_CACHE.write().unwrap();
        cache.insert(selector_str.to_string(), selector.clone());
    }
    
    Ok(selector)
}

/// Parse a CSS selector with proper error handling
pub fn parse_selector(selector_str: &str) -> ParserResult<Selector> {
    Selector::parse(selector_str)
        .map_err(|_| ParserError::SelectorError(selector_str.to_string()))
}

/// Try to parse a selector, returning None on failure
pub fn try_parse_selector(selector_str: &str) -> Option<Selector> {
    Selector::parse(selector_str).ok()
}

// ============================================================================
// SELECTOR UTILITIES
// ============================================================================

/// Build a selector for heading level
pub fn heading_selector(level: u8) -> &'static Selector {
    match level {
        1 => &SELECTORS.h1,
        2 => &SELECTORS.h2,
        3 => &SELECTORS.h3,
        4 => &SELECTORS.h4,
        5 => &SELECTORS.h5,
        6 => &SELECTORS.h6,
        _ => &SELECTORS.h1,
    }
}

/// Common content area selectors
pub const CONTENT_SELECTORS: &[&str] = &[
    "article",
    "main",
    "[role=main]",
    ".content",
    ".post-content",
    ".entry-content",
    ".article-content",
    ".post-body",
    ".article-body",
    "#content",
    "#main-content",
];

/// Selectors for elements to remove (boilerplate)
pub const BOILERPLATE_SELECTORS: &[&str] = &[
    "script",
    "style",
    "noscript",
    "iframe",
    "object",
    "embed",
    "nav",
    "header:not(article header)",
    "footer:not(article footer)",
    "aside",
    ".sidebar",
    ".navigation",
    ".nav",
    ".menu",
    ".advertisement",
    ".ad",
    ".ads",
    ".social-share",
    ".social-buttons",
    ".comments",
    ".comment-form",
    ".related-posts",
    ".recommended",
    "[role=navigation]",
    "[role=banner]",
    "[role=contentinfo]",
    "[role=complementary]",
    "[aria-hidden=true]",
];

/// Selectors for inline elements that should preserve text
pub const INLINE_ELEMENTS: &[&str] = &[
    "a", "span", "em", "strong", "b", "i", "u", "s", 
    "mark", "small", "sub", "sup", "code", "kbd", "samp", "var",
    "abbr", "cite", "dfn", "time", "q",
];

/// Selectors for block elements that should add line breaks
pub const BLOCK_ELEMENTS: &[&str] = &[
    "p", "div", "h1", "h2", "h3", "h4", "h5", "h6",
    "blockquote", "pre", "ul", "ol", "li", "dl", "dt", "dd",
    "table", "tr", "th", "td", "article", "section", "aside",
    "header", "footer", "nav", "main", "figure", "figcaption",
    "address", "hr", "br",
];

// ============================================================================
// SELECTOR BUILDERS
// ============================================================================

/// Build an attribute selector
pub fn attr_selector(element: &str, attr: &str, value: &str) -> String {
    format!("{}[{}='{}']", element, attr, value)
}

/// Build an attribute contains selector
pub fn attr_contains_selector(element: &str, attr: &str, value: &str) -> String {
    format!("{}[{}*='{}']", element, attr, value)
}

/// Build an attribute starts-with selector
pub fn attr_starts_with_selector(element: &str, attr: &str, value: &str) -> String {
    format!("{}[{}^='{}']", element, attr, value)
}

/// Build a class selector
pub fn class_selector(element: &str, class: &str) -> String {
    format!("{}.{}", element, class)
}

/// Build an ID selector
pub fn id_selector(element: &str, id: &str) -> String {
    format!("{}#{}", element, id)
}

/// Build a descendant selector
pub fn descendant_selector(ancestor: &str, descendant: &str) -> String {
    format!("{} {}", ancestor, descendant)
}

/// Build a child selector
pub fn child_selector(parent: &str, child: &str) -> String {
    format!("{} > {}", parent, child)
}

/// Build a multiple selector (OR)
pub fn multi_selector(selectors: &[&str]) -> String {
    selectors.join(", ")
}

// ============================================================================
// META TAG SELECTORS
// ============================================================================

/// Create selector for meta tag by name
pub fn meta_name_selector(name: &str) -> String {
    format!("meta[name='{}']", name)
}

/// Create selector for meta tag by property (OG/Twitter)
pub fn meta_property_selector(property: &str) -> String {
    format!("meta[property='{}']", property)
}

/// Create selector for link by rel
pub fn link_rel_selector(rel: &str) -> String {
    format!("link[rel='{}']", rel)
}

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_cached_selectors_exist() {
        // Just accessing should not panic
        let _ = &SELECTORS.title;
        let _ = &SELECTORS.body;
        let _ = &SELECTORS.h1;
    }

    #[test]
    fn test_cached_selectors_work() {
        let html = Html::parse_document("<html><body><h1>Test</h1></body></html>");
        let h1 = html.select(&SELECTORS.h1).next();
        assert!(h1.is_some());
    }

    #[test]
    fn test_parse_selector_success() {
        let sel = parse_selector("div.class").unwrap();
        let html = Html::parse_document("<div class='class'>Test</div>");
        assert!(html.select(&sel).next().is_some());
    }

    #[test]
    fn test_parse_selector_failure() {
        let result = parse_selector("div[[[invalid");
        assert!(result.is_err());
        if let Err(ParserError::SelectorError(s)) = result {
            assert!(s.contains("invalid"));
        }
    }

    #[test]
    fn test_try_parse_selector() {
        assert!(try_parse_selector("div").is_some());
        assert!(try_parse_selector("div[[[").is_none());
    }

    #[test]
    fn test_get_or_create_selector() {
        // First call - creates
        let sel1 = get_or_create_selector("div.test-class").unwrap();
        // Second call - from cache
        let sel2 = get_or_create_selector("div.test-class").unwrap();
        
        // Both should work identically
        let html = Html::parse_document("<div class='test-class'>Hello</div>");
        assert!(html.select(&sel1).next().is_some());
        assert!(html.select(&sel2).next().is_some());
    }

    #[test]
    fn test_heading_selector() {
        assert!(std::ptr::eq(heading_selector(1), &SELECTORS.h1));
        assert!(std::ptr::eq(heading_selector(2), &SELECTORS.h2));
        assert!(std::ptr::eq(heading_selector(6), &SELECTORS.h6));
        assert!(std::ptr::eq(heading_selector(99), &SELECTORS.h1)); // Invalid = h1
    }

    #[test]
    fn test_attr_selector() {
        let sel = attr_selector("input", "type", "text");
        assert_eq!(sel, "input[type='text']");
        
        let selector = parse_selector(&sel).unwrap();
        let html = Html::parse_document("<input type='text'>");
        assert!(html.select(&selector).next().is_some());
    }

    #[test]
    fn test_attr_contains_selector() {
        let sel = attr_contains_selector("a", "href", "example");
        assert_eq!(sel, "a[href*='example']");
    }

    #[test]
    fn test_attr_starts_with_selector() {
        let sel = attr_starts_with_selector("a", "href", "https");
        assert_eq!(sel, "a[href^='https']");
    }

    #[test]
    fn test_class_selector() {
        let sel = class_selector("div", "container");
        assert_eq!(sel, "div.container");
    }

    #[test]
    fn test_id_selector() {
        let sel = id_selector("div", "main");
        assert_eq!(sel, "div#main");
    }

    #[test]
    fn test_descendant_selector() {
        let sel = descendant_selector("article", "p");
        assert_eq!(sel, "article p");
    }

    #[test]
    fn test_child_selector() {
        let sel = child_selector("ul", "li");
        assert_eq!(sel, "ul > li");
    }

    #[test]
    fn test_multi_selector() {
        let sel = multi_selector(&["h1", "h2", "h3"]);
        assert_eq!(sel, "h1, h2, h3");
    }

    #[test]
    fn test_meta_name_selector() {
        let sel = meta_name_selector("description");
        assert_eq!(sel, "meta[name='description']");
        
        let selector = parse_selector(&sel).unwrap();
        let html = Html::parse_document("<meta name='description' content='Test'>");
        assert!(html.select(&selector).next().is_some());
    }

    #[test]
    fn test_meta_property_selector() {
        let sel = meta_property_selector("og:title");
        assert_eq!(sel, "meta[property='og:title']");
    }

    #[test]
    fn test_link_rel_selector() {
        let sel = link_rel_selector("canonical");
        assert_eq!(sel, "link[rel='canonical']");
    }

    #[test]
    fn test_boilerplate_selectors_valid() {
        // Ensure all boilerplate selectors are valid CSS
        for sel_str in BOILERPLATE_SELECTORS {
            assert!(
                try_parse_selector(sel_str).is_some(),
                "Invalid selector: {}", sel_str
            );
        }
    }

    #[test]
    fn test_content_selectors_valid() {
        for sel_str in CONTENT_SELECTORS {
            assert!(
                try_parse_selector(sel_str).is_some(),
                "Invalid selector: {}", sel_str
            );
        }
    }
}