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
use std::{
    borrow::Cow,
    collections::HashMap,
    error::Error,
    fmt::{Display, Formatter},
    sync::Arc,
};

use constants::{
    BNODE_ID_GENERATOR, COMMON_PREFIXES, DEFAULT_WELL_KNOWN_PREFIX, NODE_NS_TYPE,
    NODE_RDFA_PATTERN_TYPE,
};
use itertools::Itertools;
use scraper::ElementRef;
use url::Url;
mod constants;

#[cfg(test)]
mod tests;

#[derive(Debug)]
pub struct RdfaGraph<'a>(Vec<Statement<'a>>);

#[derive(Debug, Default, Clone)]
pub struct Context<'a> {
    base: &'a str,
    vocab: Option<&'a str>,
    parent: Option<Arc<Context<'a>>>,
    current_node: Option<Node<'a>>,
    prefixes: HashMap<&'a str, &'a str>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Literal<'a> {
    datatype: Option<Box<Node<'a>>>,
    value: Cow<'a, str>,
    lang: Option<&'a str>,
}

#[derive(Debug, Clone, Eq, PartialOrd, Hash)]
pub enum Node<'a> {
    Iri(Cow<'a, str>),
    Literal(Literal<'a>),
    Ref(Arc<Node<'a>>),
    List(Vec<Node<'a>>),
    BNode(u64),
}

impl PartialEq for Node<'_> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Iri(l0), Self::Iri(r0)) => l0 == r0,
            (Self::Literal(l0), Self::Literal(r0)) => l0 == r0,
            (Self::Ref(l0), Self::Ref(r0)) => l0 == r0,
            (Self::Ref(l0), rhs) => l0.as_ref() == rhs,
            (lhs, Self::Ref(r0)) => lhs == r0.as_ref(),
            (Self::List(l0), Self::List(r0)) => l0 == r0,
            (Self::BNode(l0), Self::BNode(r0)) => l0 == r0,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct Statement<'a> {
    subject: Node<'a>,
    predicate: Node<'a>,
    object: Node<'a>,
}

impl Display for Node<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Node::Iri(iri) => f.write_str(&format!("<{}>", iri)),
            Node::Ref(iri) => f.write_str(&format!("{}", iri)),
            Node::Literal(Literal {
                datatype,
                lang,
                value,
            }) => {
                let mut s = if value
                    .as_ref()
                    .chars()
                    .any(|c| c.is_ascii_control() || c.is_control())
                {
                    format!(r#""""{value}""""#)
                } else {
                    format!(r#""{value}""#)
                };

                if let Some(datatype) = datatype {
                    s.push_str(&format!(r#"^^{datatype}"#));
                } else if let Some(lang) = lang {
                    s.push_str(&format!(r#"@{lang}"#));
                }
                f.write_str(&s)
            }
            Node::BNode(id) => {
                // todo maybe this should use the base?
                f.write_str(&format!("<{}{}>", DEFAULT_WELL_KNOWN_PREFIX, id))
            }
            e => {
                writeln!(f, "fixme! format for {e:?} not implemented")?;
                Err(std::fmt::Error)
            }
        }
    }
}

impl Display for Statement<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let Statement {
            subject,
            predicate,
            object,
        } = self;
        f.write_str(&format!(r#"{subject} {predicate} {object}."#))
    }
}
impl Display for RdfaGraph<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(
            &self
                .0
                .iter()
                .map(Statement::to_string)
                .collect::<Vec<String>>()
                .join("\n"),
        )
    }
}

impl<'a> RdfaGraph<'a> {
    pub fn parse(
        input: &ElementRef<'a>,
        initial_context: Context<'a>,
    ) -> Result<RdfaGraph<'a>, Box<dyn Error>> {
        let mut triples = vec![];
        traverse_element(input, initial_context, &mut triples)?;

        triples = copy_pattern(triples)?;
        // copy patterns

        Ok(RdfaGraph(triples))
    }
}

pub fn copy_pattern(triples: Vec<Statement<'_>>) -> Result<Vec<Statement<'_>>, Box<dyn Error>> {
    let (pattern_type, pattern): (Vec<Statement>, Vec<Statement>) = triples
        .into_iter()
        .partition(|stmt| stmt.object == *NODE_RDFA_PATTERN_TYPE);

    let (pattern_predicate, pattern): (Vec<Statement>, Vec<Statement>) = pattern
        .into_iter()
        .partition(|stmt| pattern_type.iter().any(|s| s.subject == stmt.subject));

    let (pattern_subject, mut triples): (Vec<Statement>, Vec<Statement>) = pattern
        .into_iter()
        .partition(|stmt| pattern_predicate.iter().any(|s| s.subject == stmt.object));

    for Statement {
        subject, object, ..
    } in pattern_subject
    {
        for Statement {
            predicate,
            object: obj,
            ..
        } in pattern_predicate
            .iter()
            .filter(|stmt| object == stmt.subject)
        {
            triples.push(Statement {
                subject: subject.clone(),
                predicate: predicate.clone(),
                object: obj.clone(),
            })
        }
    }
    Ok(triples)
}

pub fn traverse_element<'a>(
    element_ref: &ElementRef<'a>,
    mut ctx: Context<'a>,
    stmts: &mut Vec<Statement<'a>>,
) -> Result<Option<Node<'a>>, Box<dyn Error>> {
    let elt = element_ref.value();

    // extract attrs
    let vocab = elt
        .attr("vocab")
        .or_else(|| ctx.parent.as_ref().and_then(|p| p.vocab));
    let resource = elt.attr("resource");
    let about = elt.attr("about");
    let property = elt.attr("property");
    let rel = elt.attr("rel");
    let _href = elt.attr("href");
    let prefix = elt.attr("prefix");

    ctx.vocab = vocab;

    if let Some(prefix) = prefix {
        ctx.prefixes = parse_prefixes(prefix);
    } else if let Some(parent) = &ctx.parent {
        ctx.prefixes = parent.prefixes.clone();
    }

    let rel = rel.and_then(|r| {
        resolve_uri(r, &ctx, false)
            .map(|n| Node::Ref(Arc::new(n)))
            .ok()
    });

    let predicates = property.map(|p| parse_property_or_type_of(p, &ctx));
    let type_ofs = elt
        .attr("typeof")
        .map(|t| parse_property_or_type_of(t, &ctx));
    let current_node = if let Some(resource) = resource {
        // handle resource case. set the context.
        // if property is present, this becomes an object of the parent.
        let object = Node::Ref(Arc::new(resolve_uri(resource, &ctx, true)?));
        if let Some(predicates) = &predicates {
            let subject = ctx
                .parent
                .as_ref()
                .and_then(|p| p.current_node.clone())
                .ok_or("no parent node")?;
            for predicate in predicates {
                stmts.push(Statement {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: object.clone(),
                });
            }
            if type_ofs.is_some() {
                object
            } else {
                subject
            }
        } else {
            object
        }
    } else if let Some(about) = about {
        // handle about case. set the context.
        // if property is present, children become objects of current.
        let subject = Node::Ref(Arc::new(resolve_uri(about, &ctx, true)?));

        if let Some(predicates) = &predicates {
            for predicate in predicates {
                stmts.push(Statement {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: Node::Ref(Arc::new(extract_literal(element_ref, &ctx)?)),
                });
            }
        }
        subject
    } else if type_ofs.is_some() {
        // for some reasons it seems that if there is a typeof but no
        // about and no resource, it becomes an anon node
        // this might be incorrect
        let node =
            Node::BNode(BNODE_ID_GENERATOR.fetch_add(1, std::sync::atomic::Ordering::SeqCst));
        let subject = ctx
            .parent
            .as_ref()
            .and_then(|p| p.current_node.clone())
            .unwrap_or_else(|| {
                Node::BNode(BNODE_ID_GENERATOR.fetch_add(1, std::sync::atomic::Ordering::SeqCst))
            });
        if let Some(predicates) = &predicates {
            for predicate in predicates {
                stmts.push(Statement {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: node.clone(),
                });
            }
        }
        node
    } else {
        let subject = ctx
            .parent
            .as_ref()
            .and_then(|p| p.current_node.clone())
            .unwrap_or_else(|| {
                Node::BNode(BNODE_ID_GENERATOR.fetch_add(1, std::sync::atomic::Ordering::SeqCst))
            });

        if let Some(predicates) = &predicates {
            for predicate in predicates {
                stmts.push(Statement {
                    subject: subject.clone(),
                    predicate: predicate.clone(),
                    object: Node::Ref(Arc::new(extract_literal(element_ref, &ctx)?)),
                });
            }
        }
        subject
    };

    if let Some(type_ofs) = type_ofs {
        for type_of in type_ofs {
            stmts.push(Statement {
                subject: current_node.clone(),
                predicate: NODE_NS_TYPE.clone(),
                object: type_of,
            })
        }
    }
    ctx.current_node = Some(current_node.clone());

    if element_ref.has_children() {
        let child_ctx = Context {
            parent: Some(Arc::new(ctx.clone())),
            base: ctx.base,

            ..Default::default()
        };
        for child in element_ref.children() {
            if let Some(c) = ElementRef::wrap(child) {
                let obj = traverse_element(&c, child_ctx.clone(), stmts)?;
                if let (Some(rel), Some(obj)) = (&rel, obj) {
                    stmts.push(Statement {
                        subject: current_node.clone(),
                        predicate: rel.clone(),
                        object: obj,
                    });
                }
            } else if let Some(_text) = child.value().as_text() {
                // todo do smth with text
            }
        }
    }
    Ok(ctx.current_node.clone())
}

pub fn extract_literal<'a>(
    element_ref: &ElementRef<'a>,
    ctx: &Context<'a>,
) -> Result<Node<'a>, &'static str> {
    let elt_val = element_ref.value();
    let datatype = elt_val
        .attr("datatype")
        .and_then(|dt| match resolve_uri(dt, ctx, true) {
            Ok(d) => Some(Box::new(d)),
            Err(e) => {
                eprintln!("could not parse {dt}. error {e}");
                None
            }
        }); //todo lang

    if let Some(href) = elt_val.attr("href") {
        resolve_uri(href, ctx, true)
    } else if let Some(content) = elt_val.attr("content") {
        Ok(Node::Literal(Literal {
            datatype,
            value: Cow::Borrowed(content),
            lang: None,
        }))
    } else {
        let texts = element_ref.text().collect::<Vec<_>>();
        let text = if texts.is_empty() {
            Cow::Borrowed("")
        } else if texts.len() == 1 {
            Cow::Borrowed(texts[0])
        } else {
            Cow::Owned(texts.iter().map(|t| t.to_string()).collect())
        };
        Ok(Node::Literal(Literal {
            datatype,
            value: text,
            lang: None,
        }))
    }
}

pub fn resolve_uri<'a>(
    uri: &'a str,
    ctx: &Context<'a>,
    is_resource: bool,
) -> Result<Node<'a>, &'static str> {
    let iri = Url::parse(uri);
    match iri {
        Ok(iri) if !iri.cannot_be_a_base() || iri.is_special() => Ok(Node::Iri(Cow::Borrowed(uri))),

        // Curie
        Ok(iri) => {
            if uri.starts_with("mail") || uri.starts_with("tel") {
                Ok(Node::Iri(Cow::Borrowed(uri)))
            } else if let Some(value) = ctx.prefixes.get(iri.scheme()) {
                let iri = uri.replace(':', "").trim().replacen(iri.scheme(), value, 1);
                Ok(Node::Iri(Cow::Owned(iri)))
            } else if let Some(value) = COMMON_PREFIXES.get(iri.scheme()) {
                let iri = uri.replace(':', "").trim().replacen(iri.scheme(), value, 1);
                Ok(Node::Iri(Cow::Owned(iri)))
            } else {
                Ok(Node::Iri(Cow::Owned(format!("fixme! {uri}"))))
            }
        }
        Err(url::ParseError::RelativeUrlWithoutBase) => {
            if is_resource {
                Ok(Node::Iri(Cow::Owned([ctx.base, uri].join(""))))
            } else if let Some(vocab) = ctx.vocab {
                Ok(Node::Iri(Cow::Owned([vocab, uri].join(""))))
            } else {
                Err("could not determine base/vocab")
            }
        }
        Err(e) => {
            eprintln!("invalid uri {uri}. error: {e}");
            Err("could not resolve uri")
        }
    }
}

fn parse_prefixes(s: &str) -> HashMap<&str, &str> {
    s.split_whitespace()
        .map(|s| s.trim())
        .tuples::<(_, _)>()
        .filter_map(|(s, p)| {
            if let Some((s, _)) = s.split_once(':') {
                Some((s, p))
            } else {
                eprintln!("fixme! couldn't parse curie for {s}, {p}");
                None
            }
        })
        .collect()
}

fn parse_property_or_type_of<'a>(s: &'a str, ctx: &Context<'a>) -> Vec<Node<'a>> {
    s.split_whitespace()
        .filter_map(|uri| resolve_uri(uri, ctx, false).ok())
        .map(|n| Node::Ref(Arc::new(n)))
        .collect_vec()
}