aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-03-01 19:50:23 -0500
committerDavid Robillard <d@drobilla.net>2023-04-02 20:04:00 -0400
commitb7ed626f5396e70fb9fff94e7edce0b54fde1ca5 (patch)
tree9bc360de8ec906a598e7781438c8983251eaff8c
parent2eb037b3f1abebbac697c7d804f1e6339c907aaa (diff)
downloadserd-b7ed626f5396e70fb9fff94e7edce0b54fde1ca5.tar.gz
serd-b7ed626f5396e70fb9fff94e7edce0b54fde1ca5.tar.bz2
serd-b7ed626f5396e70fb9fff94e7edce0b54fde1ca5.zip
Improve TriG pretty-printing and remove trailing newlines
-rw-r--r--NEWS3
-rw-r--r--src/writer.c12
-rw-r--r--test/test_reader_writer.c4
-rw-r--r--test/test_writer.c4
4 files changed, 15 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index a86e4d8b..77ac2fd4 100644
--- a/NEWS
+++ b/NEWS
@@ -13,12 +13,13 @@ serd (0.31.0) unstable; urgency=medium
* Fix possible hang when writing nested Turtle lists
* Gracefully handle bad characters in Turtle blank node syntax
* Gracefully handle bad characters in Turtle datatype syntax
+ * Improve TriG pretty-printing and remove trailing newlines
* Improve serdi man page
* Override pkg-config dependency within meson
* Replace duplicated dox_to_sphinx script with sphinxygen dependency
* Test header for warnings more strictly
- -- David Robillard <d@drobilla.net> Wed, 01 Mar 2023 03:38:47 +0000
+ -- David Robillard <d@drobilla.net> Thu, 02 Mar 2023 00:45:22 +0000
serd (0.30.16) stable; urgency=medium
diff --git a/src/writer.c b/src/writer.c
index d4b47e0a..3433a924 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -62,7 +62,7 @@ typedef struct {
} SepRule;
static const SepRule rules[] = {{NULL, 0, 0, 0, 0},
- {" .\n\n", 4, 0, 0, 0},
+ {" .\n", 3, 0, 0, 0},
{" ;", 2, 0, 1, 1},
{" ,", 2, 0, 1, 0},
{NULL, 0, 0, 1, 0},
@@ -73,7 +73,7 @@ static const SepRule rules[] = {{NULL, 0, 0, 0, 0},
{NULL, 0, 0, 1, 0},
{")", 1, 1, 0, 0},
{" {", 2, 0, 1, 1},
- {" }", 2, 0, 1, 1},
+ {"}\n", 2, 0, 0, 0},
{"<", 1, 0, 0, 0},
{">", 1, 0, 0, 0},
{"\n", 1, 0, 1, 0}};
@@ -459,7 +459,8 @@ write_sep(SerdWriter* writer, const Sep sep)
if ((writer->last_sep && rule->space_after_sep) ||
(!writer->last_sep && rule->space_after_node)) {
write_newline(writer);
- } else if (writer->last_sep && rule->space_after_node) {
+ } else if (writer->last_sep && writer->last_sep != SEP_GRAPH_BEGIN &&
+ rule->space_after_node) {
sink(" ", 1, writer);
}
@@ -837,6 +838,7 @@ serd_writer_write_statement(SerdWriter* writer,
reset_context(writer, true);
if (graph) {
+ write_newline(writer);
TRY(write_node(writer, graph, datatype, lang, FIELD_GRAPH, flags));
++writer->indent;
write_sep(writer, SEP_GRAPH_BEGIN);
@@ -885,6 +887,10 @@ serd_writer_write_statement(SerdWriter* writer,
}
if (!(flags & SERD_ANON_CONT)) {
+ if (writer->last_sep == SEP_END_S) {
+ write_newline(writer);
+ }
+
write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags);
++writer->indent;
write_sep(writer, SEP_S_P);
diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c
index 5a0509d3..32367bf7 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -1,4 +1,4 @@
-// Copyright 2011-2020 David Robillard <d@drobilla.net>
+// Copyright 2011-2023 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#undef NDEBUG
@@ -363,7 +363,7 @@ test_writer(const char* const path)
serd_writer_free(writer);
out = serd_chunk_sink_finish(&chunk);
- assert(!strcmp((const char*)out, "<>\n\t<http://example.org/pred> .\n\n"));
+ assert(!strcmp((const char*)out, "<>\n\t<http://example.org/pred> .\n"));
serd_free(out);
serd_env_free(env);
diff --git a/test/test_writer.c b/test/test_writer.c
index b740eafb..c1a5f468 100644
--- a/test/test_writer.c
+++ b/test/test_writer.c
@@ -1,4 +1,4 @@
-// Copyright 2011-2021 David Robillard <d@drobilla.net>
+// Copyright 2011-2023 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
#undef NDEBUG
@@ -35,7 +35,7 @@ test_write_long_literal(void)
static const char* const expected =
"<http://example.org/s>\n"
- "\t<http://example.org/p> \"\"\"hello \"\"\\\"world\"\"\\\"!\"\"\" .\n\n";
+ "\t<http://example.org/p> \"\"\"hello \"\"\\\"world\"\"\\\"!\"\"\" .\n";
assert(!strcmp((char*)out, expected));
serd_free(out);