meta_oxide 0.1.1

Universal metadata extraction library supporting 13 formats (HTML Meta, Open Graph, Twitter Cards, JSON-LD, Microdata, Microformats, RDFa, Dublin Core, Web App Manifest, oEmbed, rel-links, Images, SEO) with 7 language bindings
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
//! RDFa (Resource Description Framework in Attributes) extractor
//!
//! Extracts semantic metadata embedded in HTML using RDFa attributes.
//! RDFa is a W3C standard with 62% desktop adoption.

use crate::errors::Result;
use crate::extractors::common::{html_utils, url_utils};
use crate::types::rdfa::{RdfaItem, RdfaValue};
use scraper::{ElementRef, Html};
use std::collections::HashMap;

#[cfg(test)]
mod tests;

/// Prefix context for expanding CURIEs (Compact URIs)
#[derive(Debug, Clone)]
struct PrefixContext {
    prefixes: HashMap<String, String>,
}

impl PrefixContext {
    /// Create a new prefix context with default prefixes
    fn new() -> Self {
        let mut prefixes = HashMap::new();

        // Common default prefixes
        prefixes.insert("schema".to_string(), "https://schema.org/".to_string());
        prefixes.insert("foaf".to_string(), "http://xmlns.com/foaf/0.1/".to_string());
        prefixes.insert("dc".to_string(), "http://purl.org/dc/terms/".to_string());
        prefixes.insert("og".to_string(), "http://ogp.me/ns#".to_string());
        prefixes.insert("xsd".to_string(), "http://www.w3.org/2001/XMLSchema#".to_string());

        Self { prefixes }
    }

    /// Parse and add prefixes from a prefix attribute value
    /// Format: "prefix1: namespace1 prefix2: namespace2"
    fn parse_prefix_attr(&mut self, prefix_attr: &str) {
        for pair in prefix_attr.split_whitespace() {
            if let Some((prefix, namespace)) = pair.split_once(':') {
                let prefix = prefix.trim();
                let namespace = namespace.trim();
                if !prefix.is_empty() && !namespace.is_empty() {
                    self.prefixes.insert(prefix.to_string(), namespace.to_string());
                }
            }
        }
    }

    /// Expand a CURIE (Compact URI) to a full URI
    /// Examples: "schema:Person" -> "https://schema.org/Person"
    fn expand_curie(&self, curie: &str) -> String {
        if let Some((prefix, local_name)) = curie.split_once(':') {
            if let Some(namespace) = self.prefixes.get(prefix) {
                return format!("{}{}", namespace, local_name);
            }
        }
        // If not a valid CURIE, return as-is
        curie.to_string()
    }

    /// Expand all CURIEs in a space-separated list
    fn expand_curie_list(&self, list: &str) -> Vec<String> {
        list.split_whitespace()
            .map(|item| self.expand_curie(item))
            .collect()
    }
}

/// Extract all RDFa items from HTML
///
/// # Arguments
/// * `html` - The HTML content to extract from
/// * `base_url` - Optional base URL for resolving relative URLs
///
/// # Returns
/// * `Result<Vec<RdfaItem>>` - List of extracted RDFa items or error
///
/// # Example
/// ```rust
/// use meta_oxide::extractors::rdfa;
///
/// let html = r#"<div vocab="https://schema.org/" typeof="Person">
///     <span property="name">Jane Doe</span>
/// </div>"#;
/// let items = rdfa::extract(html, None).unwrap();
/// assert_eq!(items.len(), 1);
/// ```
pub fn extract(html: &str, base_url: Option<&str>) -> Result<Vec<RdfaItem>> {
    let doc = html_utils::parse_html(html);
    let mut items = Vec::new();

    // Create prefix context with default prefixes
    let mut prefix_ctx = PrefixContext::new();

    // Collect all prefix definitions from the document
    let prefix_selector = html_utils::create_selector("[prefix]")?;
    for element in doc.select(&prefix_selector) {
        if let Some(prefix_attr) = html_utils::get_attr(&element, "prefix") {
            prefix_ctx.parse_prefix_attr(&prefix_attr);
        }
    }

    // Find all RDFa root elements (elements with typeof or vocab)
    let roots = find_rdfa_roots(&doc)?;

    for root in roots {
        let item = extract_item_with_context(&root, base_url, &prefix_ctx)?;
        items.push(item);
    }

    Ok(items)
}

/// Find all RDFa root elements in the document
///
/// Root elements are those with `typeof` or `vocab` attributes
fn find_rdfa_roots(doc: &Html) -> Result<Vec<ElementRef<'_>>> {
    let mut roots = Vec::new();

    // Find elements with typeof attribute (type declaration)
    let typeof_selector = html_utils::create_selector("[typeof]")?;
    for element in doc.select(&typeof_selector) {
        // Only add if not nested within another typeof (we'll handle nesting later)
        if !is_nested_typeof(&element) {
            roots.push(element);
        }
    }

    // Find elements with vocab attribute that don't have typeof
    let vocab_selector = html_utils::create_selector("[vocab]:not([typeof])")?;
    for element in doc.select(&vocab_selector) {
        // Only add if not already in roots
        if !roots.iter().any(|r| r.id() == element.id()) {
            roots.push(element);
        }
    }

    Ok(roots)
}

/// Check if an element is nested within another typeof element
fn is_nested_typeof(element: &ElementRef) -> bool {
    let mut current = element.parent();
    while let Some(parent) = current {
        if let Some(parent_elem) = parent.value().as_element() {
            if parent_elem.attr("typeof").is_some() {
                return true;
            }
        }
        current = parent.parent();
    }
    false
}

/// Extract a single RDFa item from a root element with prefix context
fn extract_item_with_context(
    element: &ElementRef,
    base_url: Option<&str>,
    prefix_ctx: &PrefixContext,
) -> Result<RdfaItem> {
    let mut item = RdfaItem::new();

    // Extract vocab attribute
    if let Some(vocab) = html_utils::get_attr(element, "vocab") {
        item = item.with_vocab(vocab);
    }

    // Extract typeof attribute (can be space-separated list of types with CURIEs)
    if let Some(type_attr) = html_utils::get_attr(element, "typeof") {
        let types = prefix_ctx.expand_curie_list(&type_attr);
        if !types.is_empty() {
            item = item.with_type(types);
        }
    }

    // Extract about attribute (subject URI, can be CURIE)
    if let Some(about) = html_utils::get_attr(element, "about") {
        // First expand CURIE if applicable
        let expanded = prefix_ctx.expand_curie(&about);
        // Then resolve URL if base_url is provided
        let resolved = if let Some(base) = base_url {
            url_utils::resolve_url(Some(base), &expanded).unwrap_or(expanded)
        } else {
            expanded
        };
        item = item.with_about(resolved);
    }

    // Extract properties from this element and descendants
    let properties = extract_properties_with_context(element, base_url, prefix_ctx)?;
    item.properties = properties;

    Ok(item)
}

/// Extract all properties from an element and its descendants with prefix context
fn extract_properties_with_context(
    element: &ElementRef,
    base_url: Option<&str>,
    prefix_ctx: &PrefixContext,
) -> Result<HashMap<String, Vec<RdfaValue>>> {
    let mut properties: HashMap<String, Vec<RdfaValue>> = HashMap::new();

    // Check if this element has a property attribute (can be CURIE)
    if let Some(property_name) = html_utils::get_attr(element, "property") {
        // Expand CURIE in property name
        let expanded_name = prefix_ctx.expand_curie(&property_name);
        let value = extract_property_value_with_context(element, base_url, prefix_ctx)?;
        properties.entry(expanded_name).or_default().push(value);
    }

    // Recursively extract from children
    for child in element.children() {
        if let Some(child_element) = ElementRef::wrap(child) {
            // Skip nested typeof elements - they will be extracted as separate items
            if html_utils::get_attr(&child_element, "typeof").is_some() {
                // This is a nested item
                if html_utils::get_attr(element, "property").is_some() {
                    // Parent has property, so this nested item is the value
                    continue; // Will be handled by extract_property_value_with_context
                }
            }

            // Extract properties from child
            let child_properties =
                extract_properties_with_context(&child_element, base_url, prefix_ctx)?;
            for (key, values) in child_properties {
                properties.entry(key).or_default().extend(values);
            }
        }
    }

    Ok(properties)
}

/// Extract the value of a property from an element with prefix context
fn extract_property_value_with_context(
    element: &ElementRef,
    base_url: Option<&str>,
    prefix_ctx: &PrefixContext,
) -> Result<RdfaValue> {
    // Priority order for value extraction:
    // 1. content attribute (highest priority)
    // 2. resource, href, src attributes (for URIs, can be CURIEs)
    // 3. Check for nested typeof (nested item)
    // 4. Text content (lowest priority)

    // 1. Check for content attribute override
    if let Some(content) = html_utils::get_attr(element, "content") {
        // Check if there's a datatype attribute (can be CURIE like xsd:integer)
        if let Some(datatype) = html_utils::get_attr(element, "datatype") {
            let expanded_datatype = prefix_ctx.expand_curie(&datatype);
            return Ok(RdfaValue::TypedLiteral {
                value: content,
                datatype: expanded_datatype,
            });
        }
        return Ok(RdfaValue::Literal(content));
    }

    // 2. Check for resource/href/src attributes (URI values, can be CURIEs)
    for attr in &["resource", "href", "src"] {
        if let Some(uri) = html_utils::get_attr(element, attr) {
            // First expand CURIE if applicable
            let expanded = prefix_ctx.expand_curie(&uri);
            // Then resolve URL if base_url is provided
            let resolved = if let Some(base) = base_url {
                url_utils::resolve_url(Some(base), &expanded).unwrap_or(expanded)
            } else {
                expanded
            };
            return Ok(RdfaValue::Resource(resolved));
        }
    }

    // 3. Check for nested typeof (nested RDFa item)
    if html_utils::get_attr(element, "typeof").is_some() {
        let nested_item = extract_item_with_context(element, base_url, prefix_ctx)?;
        return Ok(RdfaValue::Item(Box::new(nested_item)));
    }

    // 4. Extract text content
    if let Some(text) = html_utils::extract_text(element) {
        // Check if there's a datatype attribute (can be CURIE)
        if let Some(datatype) = html_utils::get_attr(element, "datatype") {
            let expanded_datatype = prefix_ctx.expand_curie(&datatype);
            return Ok(RdfaValue::TypedLiteral {
                value: text,
                datatype: expanded_datatype,
            });
        }
        return Ok(RdfaValue::Literal(text));
    }

    // Fallback to empty literal
    Ok(RdfaValue::Literal(String::new()))
}

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

    #[test]
    fn test_extract_empty_html() {
        let result = extract("", None);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_extract_no_rdfa() {
        let html = r#"<div><p>No RDFa here</p></div>"#;
        let result = extract(html, None);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0);
    }

    #[test]
    fn test_extract_simple_typeof() {
        let html =
            r#"<div vocab="https://schema.org/" typeof="Person"><span property="name">Jane Doe</span></div>"#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].vocab, Some("https://schema.org/".to_string()));
        assert_eq!(result[0].type_of, Some(vec!["Person".to_string()]));
    }

    #[test]
    fn test_extract_multiple_types() {
        let html = r#"<div typeof="Person Employee" property="name">Jane</div>"#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 1);
        let types = result[0].type_of.as_ref().unwrap();
        assert_eq!(types.len(), 2);
        assert!(types.contains(&"Person".to_string()));
        assert!(types.contains(&"Employee".to_string()));
    }

    #[test]
    fn test_extract_property_literal() {
        let html = r#"<div typeof="Person"><span property="name">Jane Doe</span></div>"#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 1);
        let properties = &result[0].properties;
        assert!(properties.contains_key("name"));
        match &properties.get("name").unwrap()[0] {
            RdfaValue::Literal(s) => assert_eq!(s, "Jane Doe"),
            _ => panic!("Expected literal value"),
        }
    }

    #[test]
    fn test_extract_property_with_content_override() {
        let html = r#"<div typeof="Person"><span property="name" content="Jane Smith">Jane Doe</span></div>"#;
        let result = extract(html, None).unwrap();
        let properties = &result[0].properties;
        match &properties.get("name").unwrap()[0] {
            RdfaValue::Literal(s) => assert_eq!(s, "Jane Smith"),
            _ => panic!("Expected literal value"),
        }
    }

    #[test]
    fn test_extract_property_resource() {
        let html =
            r#"<div typeof="Person"><a property="url" href="https://example.com">Website</a></div>"#;
        let result = extract(html, None).unwrap();
        let properties = &result[0].properties;
        match &properties.get("url").unwrap()[0] {
            RdfaValue::Resource(uri) => assert_eq!(uri, "https://example.com"),
            _ => panic!("Expected resource value"),
        }
    }

    #[test]
    fn test_extract_property_resource_with_base_url() {
        let html = r#"<div typeof="Person"><a property="url" href="/page">Website</a></div>"#;
        let result = extract(html, Some("https://example.com")).unwrap();
        let properties = &result[0].properties;
        match &properties.get("url").unwrap()[0] {
            RdfaValue::Resource(uri) => assert_eq!(uri, "https://example.com/page"),
            _ => panic!("Expected resource value"),
        }
    }

    #[test]
    fn test_extract_multiple_properties() {
        let html = r#"
            <div typeof="Person">
                <span property="name">Jane</span>
                <span property="jobTitle">Engineer</span>
            </div>
        "#;
        let result = extract(html, None).unwrap();
        let properties = &result[0].properties;
        assert_eq!(properties.len(), 2);
        assert!(properties.contains_key("name"));
        assert!(properties.contains_key("jobTitle"));
    }

    #[test]
    fn test_extract_multiple_values_same_property() {
        let html = r#"
            <div typeof="Person">
                <span property="telephone">555-1234</span>
                <span property="telephone">555-5678</span>
            </div>
        "#;
        let result = extract(html, None).unwrap();
        let properties = &result[0].properties;
        assert_eq!(properties.get("telephone").unwrap().len(), 2);
    }

    #[test]
    fn test_extract_about_attribute() {
        let html = r#"<div typeof="Person" about="https://example.com/jane"><span property="name">Jane</span></div>"#;
        let result = extract(html, None).unwrap();
        assert_eq!(result[0].about, Some("https://example.com/jane".to_string()));
    }

    #[test]
    fn test_extract_about_with_base_url() {
        let html = r#"<div typeof="Person" about="/jane"><span property="name">Jane</span></div>"#;
        let result = extract(html, Some("https://example.com")).unwrap();
        assert_eq!(result[0].about, Some("https://example.com/jane".to_string()));
    }

    #[test]
    fn test_extract_vocab_only() {
        let html = r#"<div vocab="https://schema.org/"><div property="name">Test</div></div>"#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].vocab, Some("https://schema.org/".to_string()));
    }

    #[test]
    fn test_extract_nested_typeof() {
        let html = r#"
            <div typeof="Person">
                <span property="name">Jane</span>
                <div property="address" typeof="PostalAddress">
                    <span property="streetAddress">123 Main St</span>
                </div>
            </div>
        "#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 1);
        let properties = &result[0].properties;
        assert!(properties.contains_key("name"));
        assert!(properties.contains_key("address"));

        match &properties.get("address").unwrap()[0] {
            RdfaValue::Item(item) => {
                assert_eq!(item.type_of, Some(vec!["PostalAddress".to_string()]));
                assert!(item.properties.contains_key("streetAddress"));
            }
            _ => panic!("Expected nested item"),
        }
    }

    #[test]
    fn test_extract_datatype_attribute() {
        let html = r#"<div typeof="Person"><span property="age" datatype="xsd:integer">30</span></div>"#;
        let result = extract(html, None).unwrap();
        let properties = &result[0].properties;
        match &properties.get("age").unwrap()[0] {
            RdfaValue::TypedLiteral { value, datatype } => {
                assert_eq!(value, "30");
                assert_eq!(datatype, "xsd:integer");
            }
            _ => panic!("Expected typed literal"),
        }
    }

    #[test]
    fn test_extract_multiple_items() {
        let html = r#"
            <div typeof="Person"><span property="name">Jane</span></div>
            <div typeof="Person"><span property="name">John</span></div>
        "#;
        let result = extract(html, None).unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_is_nested_typeof() {
        let html = r#"
            <div typeof="Person">
                <div typeof="Address" id="nested">Test</div>
            </div>
        "#;
        let doc = html_utils::parse_html(html);
        let selector = html_utils::create_selector("#nested").unwrap();
        let element = doc.select(&selector).next().unwrap();
        assert!(is_nested_typeof(&element));
    }

    #[test]
    fn test_is_not_nested_typeof() {
        let html = r#"<div typeof="Person" id="root">Test</div>"#;
        let doc = html_utils::parse_html(html);
        let selector = html_utils::create_selector("#root").unwrap();
        let element = doc.select(&selector).next().unwrap();
        assert!(!is_nested_typeof(&element));
    }
}