aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-04-01 19:20:23 -0400
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commitc9afaab2a84f592e4567b37b3551511381e734e4 (patch)
tree414c77e7b41c222e9237d76add5c1c4944079ebd
parent3cc5a4fcaea8f1e2fd47c53135b53f8edcb8619d (diff)
downloadserd-c9afaab2a84f592e4567b37b3551511381e734e4.tar.gz
serd-c9afaab2a84f592e4567b37b3551511381e734e4.tar.bz2
serd-c9afaab2a84f592e4567b37b3551511381e734e4.zip
Add "longhand" mode to write rdf:type predicates normally
-rw-r--r--doc/man/serd-pipe.15
-rw-r--r--include/serd/writer.h7
-rw-r--r--src/writer.c2
-rw-r--r--test/extra/longhand/blank_object.ttl2
-rw-r--r--test/extra/longhand/blank_subject.ttl2
-rw-r--r--test/extra/longhand/collection_object.ttl2
-rw-r--r--test/extra/longhand/collection_subject.ttl2
-rw-r--r--test/extra/longhand/longhand.ttl13
-rw-r--r--test/extra/longhand/manifest.ttl16
-rw-r--r--test/extra/longhand/shorthand.ttl13
-rw-r--r--test/meson.build11
-rw-r--r--tools/console.c1
12 files changed, 75 insertions, 1 deletions
diff --git a/doc/man/serd-pipe.1 b/doc/man/serd-pipe.1
index 4d475985..7f91de29 100644
--- a/doc/man/serd-pipe.1
+++ b/doc/man/serd-pipe.1
@@ -167,6 +167,11 @@ Escape all non-ASCII characters.
Write expanded URIs instead of prefixed names.
.It Cm lax
Tolerate corrupt UTF-8 and write replacements.
+.It Cm longhand
+Avoid using the
+.Ql a
+shorthand for
+.Ql rdf:type .
.It Cm terse
Write terser output with fewer, longer lines.
.It Cm verbatim
diff --git a/include/serd/writer.h b/include/serd/writer.h
index 91baa7e8..5e8f5e6e 100644
--- a/include/serd/writer.h
+++ b/include/serd/writer.h
@@ -80,6 +80,13 @@ typedef enum {
carefully, since it can result in data loss.
*/
SERD_WRITE_LAX = 1U << 4U,
+
+ /**
+ Write rdf:type as a normal predicate.
+
+ This disables the special "a" syntax in Turtle and TriG.
+ */
+ SERD_WRITE_LONGHAND = 1U << 5U,
} SerdWriterFlag;
/// Bitwise OR of #SerdWriterFlag values
diff --git a/src/writer.c b/src/writer.c
index 0b0574d5..30b8bb4e 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -841,7 +841,7 @@ write_uri_node(SerdWriter* const writer,
const bool has_scheme = serd_uri_string_has_scheme(string.data);
if (supports_abbrev(writer)) {
- if (field == SERD_PREDICATE &&
+ if (!(writer->flags & SERD_WRITE_LONGHAND) && field == SERD_PREDICATE &&
serd_node_equals(node, writer->world->rdf_type)) {
return esink("a", 1, writer);
}
diff --git a/test/extra/longhand/blank_object.ttl b/test/extra/longhand/blank_object.ttl
new file mode 100644
index 00000000..270e406b
--- /dev/null
+++ b/test/extra/longhand/blank_object.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+eg:s eg:p1 [ eg:p11 1 ; eg:p12 2 ] ; eg:p2 [ eg:p23 3 ; eg:p24 4 ] .
diff --git a/test/extra/longhand/blank_subject.ttl b/test/extra/longhand/blank_subject.ttl
new file mode 100644
index 00000000..5e3303f9
--- /dev/null
+++ b/test/extra/longhand/blank_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+[] eg:p1 1 ; eg:p2 2 .
diff --git a/test/extra/longhand/collection_object.ttl b/test/extra/longhand/collection_object.ttl
new file mode 100644
index 00000000..3310c418
--- /dev/null
+++ b/test/extra/longhand/collection_object.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+eg:s eg:p1 ( 1 2 ) ; eg:p2 ( 3 4 ) .
diff --git a/test/extra/longhand/collection_subject.ttl b/test/extra/longhand/collection_subject.ttl
new file mode 100644
index 00000000..43670620
--- /dev/null
+++ b/test/extra/longhand/collection_subject.ttl
@@ -0,0 +1,2 @@
+@prefix eg: <http://example.org/> .
+( 1 2 ) eg:p3 3 ; eg:p4 4 .
diff --git a/test/extra/longhand/longhand.ttl b/test/extra/longhand/longhand.ttl
new file mode 100644
index 00000000..b756ce91
--- /dev/null
+++ b/test/extra/longhand/longhand.ttl
@@ -0,0 +1,13 @@
+@prefix eg: <http://example.org/eg#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+
+[]
+ rdf:type eg:Node ;
+ eg:with [
+ rdf:type eg:NodeChild ;
+ eg:having (
+ [
+ rdf:type eg:ListItem
+ ]
+ )
+ ] .
diff --git a/test/extra/longhand/manifest.ttl b/test/extra/longhand/manifest.ttl
new file mode 100644
index 00000000..313ca846
--- /dev/null
+++ b/test/extra/longhand/manifest.ttl
@@ -0,0 +1,16 @@
+@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix rdft: <http://www.w3.org/ns/rdftest#> .
+
+<>
+ a mf:Manifest ;
+ rdfs:comment "Serd longhand writing test suite" ;
+ mf:entries (
+ <#write-longhand>
+ ) .
+
+<#write-longhand>
+ a rdft:TestTurtleEval ;
+ mf:action <shorthand.ttl> ;
+ mf:name "write-longhand" ;
+ mf:result <longhand.ttl> .
diff --git a/test/extra/longhand/shorthand.ttl b/test/extra/longhand/shorthand.ttl
new file mode 100644
index 00000000..d563021e
--- /dev/null
+++ b/test/extra/longhand/shorthand.ttl
@@ -0,0 +1,13 @@
+@prefix eg: <http://example.org/eg#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+
+[]
+ a eg:Node ;
+ eg:with [
+ a eg:NodeChild ;
+ eg:having (
+ [
+ a eg:ListItem
+ ]
+ )
+ ] .
diff --git a/test/meson.build b/test/meson.build
index 43fc7211..c186b6e4 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -507,6 +507,17 @@ test_suites = {
files('extra/lax/manifest.ttl'),
ns_serdtest + 'lax/',
],
+ 'longhand_forward': [
+ files('extra/longhand/manifest.ttl'),
+ ns_serdtest + 'longhand/',
+ '--',
+ ['-O', 'longhand'],
+ ],
+ 'longhand_reverse': [
+ '--reverse',
+ files('extra/longhand/manifest.ttl'),
+ ns_serdtest + 'longhand/',
+ ],
'pattern': [
files('extra/pattern/manifest.ttl'),
ns_serdtest + 'pattern/',
diff --git a/tools/console.c b/tools/console.c
index 2a861d1b..0f66a2f1 100644
--- a/tools/console.c
+++ b/tools/console.c
@@ -294,6 +294,7 @@ serd_set_output_option(const SerdStringView name,
{"ascii", SERD_WRITE_ASCII},
{"expanded", SERD_WRITE_EXPANDED},
{"lax", SERD_WRITE_LAX},
+ {"longhand", SERD_WRITE_LONGHAND},
{"terse", SERD_WRITE_TERSE},
{"verbatim", SERD_WRITE_VERBATIM},
{NULL, SERD_WRITE_ASCII},