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
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
# MetaOxide Rust API Reference

Complete API documentation for the Rust crate.

## Table of Contents

- [Core Types]#core-types
- [Extraction Functions]#extraction-functions
- [Metadata Formats]#metadata-formats
- [Error Handling]#error-handling
- [Advanced Usage]#advanced-usage

## Core Types

### `MetaOxide`

The main struct for extracting metadata from HTML.

```rust
pub struct MetaOxide {
    // Internal fields are private
}
```

#### Methods

##### `new(html: &str, base_url: &str) -> Result<Self>`

Creates a new MetaOxide instance from HTML content.

**Parameters:**
- `html: &str` - The HTML content to parse
- `base_url: &str` - Base URL for resolving relative URLs

**Returns:**
- `Result<Self>` - The extractor instance or an error

**Example:**
```rust
use meta_oxide::MetaOxide;

let html = r#"<!DOCTYPE html><html>...</html>"#;
let extractor = MetaOxide::new(html, "https://example.com")?;
```

##### `extract_all(&self) -> Result<HashMap<String, Value>>`

Extracts all metadata formats at once.

**Returns:**
- `Result<HashMap<String, Value>>` - All extracted metadata

**Example:**
```rust
let metadata = extractor.extract_all()?;
println!("{:#?}", metadata);
```

##### `extract_basic_meta(&self) -> Result<BasicMeta>`

Extracts basic HTML metadata (title, description, etc.).

**Returns:**
- `Result<BasicMeta>` - Basic HTML metadata

**Example:**
```rust
let basic = extractor.extract_basic_meta()?;
println!("Title: {:?}", basic.title);
println!("Description: {:?}", basic.description);
```

##### `extract_opengraph(&self) -> Result<Option<OpenGraph>>`

Extracts Open Graph metadata.

**Returns:**
- `Result<Option<OpenGraph>>` - Open Graph data if present

**Example:**
```rust
if let Some(og) = extractor.extract_opengraph()? {
    println!("OG Title: {:?}", og.title);
    println!("OG Image: {:?}", og.image);
}
```

##### `extract_twitter_card(&self) -> Result<Option<TwitterCard>>`

Extracts Twitter Card metadata.

**Returns:**
- `Result<Option<TwitterCard>>` - Twitter Card data if present

**Example:**
```rust
if let Some(twitter) = extractor.extract_twitter_card()? {
    println!("Card Type: {:?}", twitter.card_type);
}
```

##### `extract_jsonld(&self) -> Result<Vec<JsonValue>>`

Extracts JSON-LD structured data.

**Returns:**
- `Result<Vec<JsonValue>>` - Array of JSON-LD objects

**Example:**
```rust
let jsonld = extractor.extract_jsonld()?;
for item in jsonld {
    println!("{:#?}", item);
}
```

##### `extract_microdata(&self) -> Result<Vec<MicrodataItem>>`

Extracts Microdata (schema.org) structured data.

**Returns:**
- `Result<Vec<MicrodataItem>>` - Array of Microdata items

**Example:**
```rust
let microdata = extractor.extract_microdata()?;
```

##### `extract_microformats(&self) -> Result<MicroformatsData>`

Extracts all Microformats data.

**Returns:**
- `Result<MicroformatsData>` - All Microformats found

**Example:**
```rust
let mf = extractor.extract_microformats()?;
```

##### `extract_dublin_core(&self) -> Result<Option<DublinCore>>`

Extracts Dublin Core metadata.

**Returns:**
- `Result<Option<DublinCore>>` - Dublin Core data if present

**Example:**
```rust
if let Some(dc) = extractor.extract_dublin_core()? {
    println!("DC Title: {:?}", dc.title);
}
```

##### `extract_rdfa(&self) -> Result<Vec<RdfaTriple>>`

Extracts RDFa triples.

**Returns:**
- `Result<Vec<RdfaTriple>>` - Array of RDFa triples

**Example:**
```rust
let rdfa = extractor.extract_rdfa()?;
```

##### `extract_rel_links(&self) -> Result<HashMap<String, Vec<Link>>>`

Extracts link relations.

**Returns:**
- `Result<HashMap<String, Vec<Link>>>` - Links grouped by rel type

**Example:**
```rust
let links = extractor.extract_rel_links()?;
if let Some(canonical) = links.get("canonical") {
    println!("Canonical URL: {}", canonical[0].href);
}
```

##### `extract_manifest(&self) -> Result<Option<WebManifest>>`

Extracts web app manifest data.

**Returns:**
- `Result<Option<WebManifest>>` - Manifest data if present

**Example:**
```rust
if let Some(manifest) = extractor.extract_manifest()? {
    println!("App Name: {:?}", manifest.name);
}
```

## Metadata Formats

### `BasicMeta`

Basic HTML metadata structure.

```rust
pub struct BasicMeta {
    pub title: Option<String>,
    pub description: Option<String>,
    pub keywords: Vec<String>,
    pub author: Option<String>,
    pub canonical: Option<String>,
    pub charset: Option<String>,
    pub viewport: Option<String>,
    pub language: Option<String>,
    pub robots: Option<String>,
}
```

### `OpenGraph`

Open Graph protocol metadata.

```rust
pub struct OpenGraph {
    pub title: Option<String>,
    pub og_type: Option<String>,
    pub image: Option<String>,
    pub url: Option<String>,
    pub description: Option<String>,
    pub site_name: Option<String>,
    pub locale: Option<String>,
    pub audio: Option<String>,
    pub video: Option<String>,
    pub additional: HashMap<String, String>,
}
```

### `TwitterCard`

Twitter Card metadata.

```rust
pub struct TwitterCard {
    pub card_type: Option<String>,
    pub site: Option<String>,
    pub creator: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
    pub image: Option<String>,
    pub image_alt: Option<String>,
    pub additional: HashMap<String, String>,
}
```

### `MicrodataItem`

Schema.org Microdata item.

```rust
pub struct MicrodataItem {
    pub item_type: Vec<String>,
    pub properties: HashMap<String, Vec<Value>>,
    pub id: Option<String>,
}
```

### `HCard`

Microformats h-card (person/organization).

```rust
pub struct HCard {
    pub name: Option<String>,
    pub url: Option<String>,
    pub photo: Option<String>,
    pub email: Option<String>,
    pub tel: Option<String>,
    pub org: Option<String>,
    pub adr: Option<HAdr>,
    pub additional: HashMap<String, Value>,
}
```

### `HEntry`

Microformats h-entry (blog post/article).

```rust
pub struct HEntry {
    pub name: Option<String>,
    pub author: Option<HCard>,
    pub published: Option<String>,
    pub updated: Option<String>,
    pub content: Option<String>,
    pub summary: Option<String>,
    pub url: Option<String>,
    pub category: Vec<String>,
    pub syndication: Vec<String>,
}
```

### `HEvent`

Microformats h-event (calendar event).

```rust
pub struct HEvent {
    pub name: Option<String>,
    pub start: Option<String>,
    pub end: Option<String>,
    pub duration: Option<String>,
    pub summary: Option<String>,
    pub description: Option<String>,
    pub url: Option<String>,
    pub location: Option<String>,
    pub category: Vec<String>,
}
```

### `DublinCore`

Dublin Core metadata.

```rust
pub struct DublinCore {
    pub title: Option<String>,
    pub creator: Option<String>,
    pub subject: Option<String>,
    pub description: Option<String>,
    pub publisher: Option<String>,
    pub contributor: Option<String>,
    pub date: Option<String>,
    pub dc_type: Option<String>,
    pub format: Option<String>,
    pub identifier: Option<String>,
    pub source: Option<String>,
    pub language: Option<String>,
    pub relation: Option<String>,
    pub coverage: Option<String>,
    pub rights: Option<String>,
}
```

### `Link`

Represents an HTML link element.

```rust
pub struct Link {
    pub href: String,
    pub rel: String,
    pub media: Option<String>,
    pub title: Option<String>,
    pub link_type: Option<String>,
    pub hreflang: Option<String>,
}
```

## Error Handling

### `MicroformatError`

Main error type for MetaOxide operations.

```rust
pub enum MicroformatError {
    ParseError(String),
    ExtractionError(String),
    UrlError(String),
    JsonError(String),
    IoError(String),
}
```

#### Variants

- `ParseError` - HTML parsing failed
- `ExtractionError` - Metadata extraction failed
- `UrlError` - URL parsing or resolution failed
- `JsonError` - JSON-LD parsing failed
- `IoError` - I/O operation failed

#### Implementation

```rust
impl std::fmt::Display for MicroformatError { /* ... */ }
impl std::error::Error for MicroformatError { /* ... */ }
```

### `Result<T>`

Type alias for Result with MicroformatError.

```rust
pub type Result<T> = std::result::Result<T, MicroformatError>;
```

## Advanced Usage

### Custom Extraction

Extract specific metadata format:

```rust
use meta_oxide::MetaOxide;

let extractor = MetaOxide::new(html, url)?;

// Extract only what you need
let og = extractor.extract_opengraph()?;
let twitter = extractor.extract_twitter_card()?;
```

### Parallel Extraction

Process multiple pages concurrently:

```rust
use rayon::prelude::*;

let urls = vec!["url1", "url2", "url3"];
let results: Vec<_> = urls.par_iter()
    .map(|url| {
        let html = fetch_html(url)?;
        let extractor = MetaOxide::new(&html, url)?;
        extractor.extract_all()
    })
    .collect();
```

### Error Recovery

Handle errors gracefully:

```rust
match MetaOxide::new(html, url) {
    Ok(extractor) => {
        match extractor.extract_all() {
            Ok(metadata) => println!("Success: {:#?}", metadata),
            Err(e) => eprintln!("Extraction failed: {}", e),
        }
    }
    Err(e) => eprintln!("Parse failed: {}", e),
}
```

### Selective Extraction

Extract only specific formats for performance:

```rust
let extractor = MetaOxide::new(html, url)?;

// Only extract social media metadata
let og = extractor.extract_opengraph()?;
let twitter = extractor.extract_twitter_card()?;

// Skip other formats if not needed
```

## Performance Considerations

### Memory Usage

- MetaOxide parses HTML once and keeps the DOM in memory
- Call `drop(extractor)` when done to free memory
- For batch processing, create and drop extractors in a loop

### CPU Usage

- Extraction is CPU-intensive for complex HTML
- Use parallel processing for multiple documents
- Extract only needed formats to reduce CPU time

### Caching

```rust
use std::collections::HashMap;

struct MetadataCache {
    cache: HashMap<String, HashMap<String, Value>>,
}

impl MetadataCache {
    fn get_or_extract(&mut self, url: &str, html: &str) -> Result<&HashMap<String, Value>> {
        if !self.cache.contains_key(url) {
            let extractor = MetaOxide::new(html, url)?;
            let metadata = extractor.extract_all()?;
            self.cache.insert(url.to_string(), metadata);
        }
        Ok(&self.cache[url])
    }
}
```

## Feature Flags

Enable specific features in `Cargo.toml`:

```toml
[dependencies]
meta_oxide = { version = "0.1.0", features = ["python"] }
```

Available features:
- `python` - Python bindings via PyO3
- `default` - Standard Rust library

## Thread Safety

MetaOxide is thread-safe:

```rust
use std::sync::Arc;
use std::thread;

let html = Arc::new(html_string);
let handles: Vec<_> = (0..4).map(|_| {
    let html = Arc::clone(&html);
    thread::spawn(move || {
        let extractor = MetaOxide::new(&html, url)?;
        extractor.extract_all()
    })
}).collect();

for handle in handles {
    let result = handle.join().unwrap();
    println!("{:?}", result);
}
```

## See Also

- [Getting Started Guide]/docs/getting-started/getting-started-rust.md
- [Examples]/examples/real-world/rust-cli-tool/
- [Architecture]/docs/architecture/architecture-overview.md