Add :implicit_base option on the RDF.Turtle.Encoder

This commit is contained in:
Marcel Otto 2021-12-19 01:39:31 +01:00
parent 8e216c57a3
commit 2beee27eb5
3 changed files with 55 additions and 9 deletions

View file

@ -5,6 +5,17 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
[Keep a CHANGELOG](http://keepachangelog.com).
## Unreleased
### Added
- `:implicit_base` option on the `RDF.Turtle.Encoder`
[Compare v0.10.0...HEAD](https://github.com/rdf-elixir/rdf-ex/compare/v0.10.0...HEAD)
## 0.10.0 - 2021-12-13
This release adds RDF-star support on the RDF data structures, the N-Triples, N-Quads,

View file

@ -9,13 +9,17 @@ defmodule RDF.Turtle.Encoder do
## Options
- `:base`: : Allows to specify the base URI to be used for a `@base` directive.
If not specified the one from the given graph is used or if there is also none
specified for the graph the `RDF.default_base_iri/0`.
- `:prefixes`: Allows to specify the prefixes to be used as a `RDF.PrefixMap` or
anything from which a `RDF.PrefixMap` can be created with `RDF.PrefixMap.new/1`.
If not specified the ones from the given graph are used or if these are also not
present the `RDF.default_prefixes/0`.
- `:base`: : Allows to specify the base URI to be used for a `@base` directive.
If not specified the one from the given graph is used or if there is also none
specified for the graph the `RDF.default_base_iri/0`.
- `:implicit_base`: This boolean flag allows to use a base URI to get relative IRIs
without embedding it explicitly in the content with a `@base` directive, so that
the URIs will be resolved according to the remaining strategy specified in
section 5.1 of [RFC3986](https://www.ietf.org/rfc/rfc3986.txt).
- `:only`: Allows to specify which parts of a Turtle document should be generated.
Possible values: `:base`, `:prefixes`, `:directives` (means the same as `[:base, :prefixes]`),
`:triples` or a list with any combination of these values.
@ -119,11 +123,15 @@ defmodule RDF.Turtle.Encoder do
defp base_directive(nil, _), do: ""
defp base_directive({_, base}, opts) do
indent(opts) <>
case Keyword.get(opts, :directive_style) do
:sparql -> "BASE <#{base}>"
_ -> "@base <#{base}> ."
end <> "\n\n"
if Keyword.get(opts, :implicit_base, false) do
""
else
indent(opts) <>
case Keyword.get(opts, :directive_style) do
:sparql -> "BASE <#{base}>"
_ -> "@base <#{base}> ."
end <> "\n\n"
end
end
defp prefix_directive({prefix, ns}, opts) do

View file

@ -131,7 +131,7 @@ defmodule RDF.Turtle.EncoderTest do
"""
end
test "when a base IRI is given, it has used instead of the base IRI of the given graph" do
test "when a base IRI is given, it is used instead of the base IRI of the given graph" do
assert Turtle.Encoder.encode!(
Graph.new([{EX.S1, EX.p1(), EX.O1}],
prefixes: %{},
@ -147,6 +147,33 @@ defmodule RDF.Turtle.EncoderTest do
"""
end
test ":implicit_base option" do
assert Turtle.Encoder.encode!(
Graph.new([{EX.S1, EX.p1(), EX.O1}],
prefixes: %{},
base_iri: EX.other()
),
base_iri: EX,
implicit_base: true
) ==
"""
<S1>
<p1> <O1> .
"""
assert Turtle.Encoder.encode!(
Graph.new([{EX.S1, EX.p1(), EX.O1}],
prefixes: %{},
base_iri: EX
),
implicit_base: true
) ==
"""
<S1>
<p1> <O1> .
"""
end
test "when no prefixes are given and no prefixes are in the given graph the default_prefixes are used" do
assert Turtle.Encoder.encode!(Graph.new({EX.S, EX.p(), NS.XSD.string()})) ==
"""