From 2beee27eb5803763a01f84d1d758a1d0200de143 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sun, 19 Dec 2021 01:39:31 +0100 Subject: [PATCH] Add :implicit_base option on the RDF.Turtle.Encoder --- CHANGELOG.md | 11 +++++++++ lib/rdf/serializations/turtle_encoder.ex | 24 +++++++++++++------- test/unit/turtle_encoder_test.exs | 29 +++++++++++++++++++++++- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b8a8d..c8a5cd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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, diff --git a/lib/rdf/serializations/turtle_encoder.ex b/lib/rdf/serializations/turtle_encoder.ex index 167500b..6d5e5f6 100644 --- a/lib/rdf/serializations/turtle_encoder.ex +++ b/lib/rdf/serializations/turtle_encoder.ex @@ -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 diff --git a/test/unit/turtle_encoder_test.exs b/test/unit/turtle_encoder_test.exs index 4a761c2..947b457 100644 --- a/test/unit/turtle_encoder_test.exs +++ b/test/unit/turtle_encoder_test.exs @@ -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 + ) == + """ + + . + """ + + assert Turtle.Encoder.encode!( + Graph.new([{EX.S1, EX.p1(), EX.O1}], + prefixes: %{}, + base_iri: EX + ), + implicit_base: true + ) == + """ + + . + """ + 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()})) == """