graphy.js icon indicating copy to clipboard operation
graphy.js copied to clipboard

Serialize multiline strings in turtle

Open tpluscode opened this issue 2 years ago • 1 comments

Is it possible to output multiline string in triple-quotes to preserve visible line breaks?

@prefix : <http://example.org/stuff/1.0/> .

-:a :b "The first line\nThe second line\n  more" .

+:a :b """The first line
+The second line
+  more""" .

tpluscode avatar Mar 29 '23 18:03 tpluscode

You can use coercions; something like this (altho you'd need to modify further if you wanted datatypes):

class LongLiteral {
   constructor(contents) {
      this.contents = contents;
   }

   toTerm() {
      const raw = `"""${this.contents.replace(/"""/g, '""\\"')}""""`;

      return {
         terse: (prefixes) => raw,
         verbose: (prefixes) => raw,
      };
   }
}

const writer = graphy.ttl.write({
   prefixes: {
      // ...
   },
   coercions: new Map([
      [LongLiteral, literal => literal.toTerm()],
   ])
});

writer.write({
   '>https://about.me/': {
      ':quote': new LongLiteral('...'),
   },
});

blake-regalia avatar Mar 29 '23 19:03 blake-regalia