Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize multiline strings in turtle #61

Open
tpluscode opened this issue Mar 29, 2023 · 1 comment
Open

Serialize multiline strings in turtle #61

tpluscode opened this issue Mar 29, 2023 · 1 comment

Comments

@tpluscode
Copy link

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""" .
@blake-regalia
Copy link
Owner

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('...'),
   },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants