Skip to content

Commit 4eb2910

Browse files
committed
simplify property copying
1 parent 4c0127e commit 4eb2910

File tree

1 file changed

+65
-61
lines changed

1 file changed

+65
-61
lines changed

html2rdf/src/lib.rs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,64 +27,68 @@ pub fn process(
2727
}
2828

2929
fn property_copying(graph: &mut Graph) {
30-
let mut added_any = true;
31-
while added_any {
32-
added_any = false;
30+
// Step 1: add
31+
{
32+
let mut added_any = true;
33+
let mut new_triples = Vec::new();
34+
while added_any {
35+
new_triples.clear();
36+
added_any = false;
37+
38+
for copy_triple in graph.triples_for_predicate(rdfa_vocab::COPY) {
39+
let copy_target: oxrdf::NamedOrBlankNodeRef = match copy_triple.object {
40+
TermRef::NamedNode(n) => n.into(),
41+
TermRef::BlankNode(n) => n.into(),
42+
TermRef::Literal(_) => {
43+
continue; // TODO: warning?
44+
}
45+
};
3346

34-
let mut new_triples = Graph::new();
35-
for copy_triple in graph.triples_for_predicate(rdfa_vocab::COPY) {
36-
let copy_target: oxrdf::NamedOrBlankNodeRef = match copy_triple.object {
37-
TermRef::NamedNode(n) => n.into(),
38-
TermRef::BlankNode(n) => n.into(),
39-
TermRef::Literal(_) => {
47+
if !graph.contains(TripleRef::new(copy_target, rdf::TYPE, rdfa_vocab::PATTERN)) {
4048
continue; // TODO: warning?
4149
}
42-
};
4350

44-
if !graph.contains(TripleRef::new(copy_target, rdf::TYPE, rdfa_vocab::PATTERN)) {
45-
continue; // TODO: warning?
51+
for trip in graph.triples_for_subject(copy_target) {
52+
new_triples.push(
53+
TripleRef::new(copy_triple.subject, trip.predicate, trip.object)
54+
.into_owned(),
55+
);
56+
}
4657
}
4758

48-
for trip in graph.triples_for_subject(copy_target) {
49-
new_triples.insert(TripleRef::new(
50-
copy_triple.subject,
51-
trip.predicate,
52-
trip.object,
53-
));
59+
for triple in &new_triples {
60+
added_any |= graph.insert(triple);
5461
}
5562
}
56-
57-
for triple in new_triples.iter() {
58-
added_any |= graph.insert(triple);
59-
}
6063
}
6164

62-
let mut triples_to_remove = Graph::new();
63-
for copy_triple in graph.triples_for_predicate(rdfa_vocab::COPY) {
64-
triples_to_remove.insert(copy_triple);
65-
let copy_target: oxrdf::NamedOrBlankNodeRef = match copy_triple.object {
66-
TermRef::NamedNode(n) => n.into(),
67-
TermRef::BlankNode(n) => n.into(),
68-
TermRef::Literal(_) => continue,
69-
};
65+
// Step 2: remove
66+
{
67+
let mut triples_to_remove = Vec::new();
68+
for copy_triple in graph.triples_for_predicate(rdfa_vocab::COPY) {
69+
triples_to_remove.push(copy_triple.into_owned());
70+
let copy_target: oxrdf::NamedOrBlankNodeRef = match copy_triple.object {
71+
TermRef::NamedNode(n) => n.into(),
72+
TermRef::BlankNode(n) => n.into(),
73+
TermRef::Literal(_) => continue,
74+
};
7075

71-
if !graph.contains(TripleRef::new(copy_target, rdf::TYPE, rdfa_vocab::PATTERN)) {
72-
continue;
73-
}
76+
if !graph.contains(TripleRef::new(copy_target, rdf::TYPE, rdfa_vocab::PATTERN)) {
77+
continue;
78+
}
7479

75-
triples_to_remove.insert(TripleRef::new(
76-
copy_triple.subject,
77-
rdf::TYPE,
78-
rdfa_vocab::PATTERN,
79-
));
80+
triples_to_remove.push(
81+
TripleRef::new(copy_triple.subject, rdf::TYPE, rdfa_vocab::PATTERN).into_owned(),
82+
);
8083

81-
for trip in graph.triples_for_subject(copy_target) {
82-
triples_to_remove.insert(trip);
84+
for trip in graph.triples_for_subject(copy_target) {
85+
triples_to_remove.push(trip.into_owned());
86+
}
8387
}
84-
}
8588

86-
for triple in triples_to_remove.iter() {
87-
graph.remove(triple);
89+
for triple in triples_to_remove {
90+
graph.remove(&triple);
91+
}
8892
}
8993
}
9094

@@ -838,11 +842,21 @@ impl<'e> LocalScope<'e> {
838842
eval_context: &EvaluationContext<H>,
839843
resolver: &Resolver<'e, '_, '_, H>,
840844
) -> Self {
841-
let resource = resolver
842-
.attr_1_safecurie_or_curie_or_iri("resource")
845+
// subject attributes
846+
let about = resolver
847+
.attr_1_safecurie_or_curie_or_iri("about")
843848
.map(Rc::new);
849+
// resource attributes
850+
let resource = resolver.attr_1_safecurie_or_curie_or_iri("resource");
844851
let href = resolver.attr_iri("href");
845852
let src = resolver.attr_iri("src");
853+
let resource_present = resource.is_present() || href.is_present() || src.is_present();
854+
let resource_value = resource
855+
.into_value()
856+
.or_else(|| Some(href.into_value()?.into()))
857+
.or_else(|| Some(src.into_value()?.into()))
858+
.map(Rc::new);
859+
846860
let is_root_element = resolver.el.name() == "html";
847861
debug_assert!(is_root_element == eval_context.parent_object.is_none());
848862

@@ -862,25 +876,15 @@ impl<'e> LocalScope<'e> {
862876
list_mappings: eval_context.list_mapping.clone(),
863877

864878
// local attributes:
865-
is_root_element,
866-
in_list: resolver.el.attr("inlist").is_some(),
879+
about,
867880
content: resolver.el.attr("content"),
881+
datatype: resolver.attr_1_term_or_curie_or_absiri("datatype"),
882+
in_list: resolver.el.attr("inlist").is_some(),
883+
is_root_element,
868884
property: resolver.attr_many_term_or_curie_or_absiri("property"),
885+
resource_present,
886+
resource_value,
869887
type_of: resolver.attr_many_term_or_curie_or_absiri("typeof"),
870-
871-
about: resolver
872-
.attr_1_safecurie_or_curie_or_iri("about")
873-
.map(Rc::new),
874-
875-
datatype: resolver.attr_1_term_or_curie_or_absiri("datatype"),
876-
877-
// read from the "resource attributes"
878-
resource_present: resource.is_present() || href.is_present() || src.is_present(),
879-
resource_value: resource
880-
.as_value()
881-
.cloned()
882-
.or_else(|| Some(Rc::new(href.into_value()?.into())))
883-
.or_else(|| Some(Rc::new(src.into_value()?.into()))),
884888
}
885889
}
886890

0 commit comments

Comments
 (0)