Skip to content

Commit dbf0512

Browse files
committed
webanno: fixup, also strip extra_context prefixes when predicate is NOT an iri
it may be an alias too
1 parent 58683ad commit dbf0512

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/api/webanno.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ fn output_predicate_datavalue(
454454
false
455455
};
456456
if is_iri(predicate) && value_is_iri {
457-
if let Some(s) = value_to_alias(predicate, datavalue, config) {
457+
if let Some(s) = value_to_alias(predicate, true, datavalue, config, false) {
458458
// if the predicate is an iri and the value looks like an IRI where the base url correspondonds to one of the added contexts, then strip this prefix and reduce the IRI value to an alias
459459
s
460460
} else {
@@ -466,6 +466,17 @@ fn output_predicate_datavalue(
466466
datavalue
467467
)
468468
}
469+
} else if value_is_iri {
470+
if let Some(s) = value_to_alias(predicate, false, datavalue, config, false) {
471+
// if the predicate is not an IRI but the value looks like an IRI where the base url correspondonds to one of the added contexts, then strip this prefix and reduce the IRI value to an alias
472+
s
473+
} else {
474+
format!(
475+
"\"{}\": {}",
476+
config.uri_to_namespace(predicate.into()),
477+
&value_to_json(datavalue)
478+
)
479+
}
469480
} else {
470481
format!(
471482
"\"{}\": {}",
@@ -480,20 +491,30 @@ fn output_predicate_datavalue(
480491
/// Returns None if this is not the case
481492
fn value_to_alias(
482493
predicate: &str,
494+
predicate_is_iri: bool,
483495
datavalue: &DataValue,
484496
config: &WebAnnoConfig,
497+
value_only: bool,
485498
) -> Option<String> {
486499
if !config.extra_context.is_empty() {
487500
if let DataValue::String(datavalue) = datavalue {
488501
for prefix in config.extra_context.iter() {
489502
if datavalue.starts_with(&format!("{}/", prefix.as_str()))
490503
|| datavalue.starts_with(&format!("{}#", prefix.as_str()))
491504
{
492-
return Some(format!(
493-
"\"{}\": \"{}\"",
494-
config.uri_to_namespace(predicate.into()),
495-
&datavalue[prefix.len() + 1..]
496-
));
505+
if value_only {
506+
return Some(format!("\"{}\"", &datavalue[prefix.len() + 1..]));
507+
} else {
508+
return Some(format!(
509+
"\"{}\": \"{}\"",
510+
if predicate_is_iri {
511+
config.uri_to_namespace(predicate.into())
512+
} else {
513+
predicate.into()
514+
},
515+
&datavalue[prefix.len() + 1..]
516+
));
517+
}
497518
}
498519
}
499520
}
@@ -508,14 +529,21 @@ fn output_datavalue(predicate: &str, datavalue: &DataValue, config: &WebAnnoConf
508529
false
509530
};
510531
if is_iri(predicate) && value_is_iri {
511-
if let Some(s) = value_to_alias(predicate, datavalue, config) {
532+
if let Some(s) = value_to_alias(predicate, true, datavalue, config, true) {
512533
// if the predicate is an iri and the value looks like an IRI where the base url correspondonds to one of the added contexts, then strip this prefix and reduce the IRI value to an alias
513534
s
514535
} else {
515536
// If the predicate is an IRI and the value *(looks like* an IRI, then the latter will be interpreted as an IRI rather than a string literal
516537
// (This is not formally defined in the spec! the predicate check is needed because we don't want this behaviour if the predicate is an alias defined in the JSON-LD context)
517538
format!("{{ \"id\": \"{}\" }}", datavalue)
518539
}
540+
} else if value_is_iri {
541+
if let Some(s) = value_to_alias(predicate, false, datavalue, config, true) {
542+
// if the predicate is not an iri but the value looks like an IRI where the base url correspondonds to one of the added contexts, then strip this prefix and reduce the IRI value to an alias
543+
s
544+
} else {
545+
value_to_json(datavalue)
546+
}
519547
} else {
520548
value_to_json(datavalue)
521549
}

0 commit comments

Comments
 (0)