graphy.js
graphy.js copied to clipboard
Serialize multiline strings in turtle
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""" .
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('...'),
},
});