Skip to content

rdf/turtle: emit a space before ';' and '.' for readability #419

Description

@melvincarvalho

Symptom

Today's Turtle output (e.g. https://test.solid.social/profile/card.jsonld with Accept: text/turtle) packs ; and . directly against the previous token:

<...#me> a foaf:Person, schema:Person;
    foaf:name "test";
    pim:storage <https://test.solid.social/>.
<...#nostr-key-1> a <https://www.w3.org/ns/cid/v1#Multikey>;
    <https://www.w3.org/ns/cid/v1#controller> <...#me>;
    <https://www.w3.org/ns/cid/v1#publicKeyMultibase> "fe70102de7ec...".

A more typical/canonical-leaning style separates the statement terminators from the previous token by a space:

<...#me> a foaf:Person, schema:Person ;
    foaf:name "test" ;
    pim:storage <https://test.solid.social/> .
<...#nostr-key-1> a <https://www.w3.org/ns/cid/v1#Multikey> ;
    <https://www.w3.org/ns/cid/v1#controller> <...#me> ;
    <https://www.w3.org/ns/cid/v1#publicKeyMultibase> "fe70102de7ec..." .

Status of "canonical form" for Turtle

There is no formally W3C-defined canonical Turtle. RDF Dataset Canonicalization (RDC-1.0) operates on N-Quads, not Turtle. So strictly speaking, both forms are spec-conformant Turtle.

However, the spaced form is widely treated as the "pretty" / preferred style:

  • W3C Turtle 1.1 spec examples consistently use a space before ; and .
  • Apache Jena's RIOT Turtle writer emits the spaced form
  • Most hand-written Turtle in the Solid / Linked Data ecosystem uses the spaced form
  • It improves human readability (the terminator is visually separable from the value) and reduces ambiguity for naive line-counters

The current N3.js default packs them. JSS uses N3.js (new Writer({...})) — see src/rdf/turtle.js:72.

What to investigate

  1. Whether N3.js's Writer exposes a config option (spaceBeforeTerminator, pretty, etc.) for this. If it does, flip the option in our usage.
  2. If not, evaluate whether a small post-processing pass on the writer's output (regex /([^\s])([;.])(\s|$)/g$1 $2$3, with care around literals containing ; or .) is the right approach. Note the literal-safety concern is real — "foo;bar" must not become "foo ;bar".
  3. Check whether any existing test depends on the no-space form (it shouldn't — Turtle parsers must accept both — but a snapshot-style test might).

Acceptance

After the change, curl -H 'Accept: text/turtle' <profile> emits:

  • A space before every ; that terminates a predicate-list continuation
  • A space before every . that terminates a triple
  • Existing parsers (N3.js, Jena, rdflib, etc.) round-trip unchanged
  • Existing JSS Turtle tests still pass (or are minimally updated to match the new style)

Refs

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions