@rdfjs-elements/formats-pretty
v0.6.8
Published
RDF/JS Sink Maps with serializers configured for pretty printing
Downloads
6,391
Readme
@rdfjs-elements/formats-pretty
Common RDF/JS parsers and serializers, the latter returning a nicely formatted output.
For basic usage see @rdfjs/formats-common
Serializers
| Format | Pretty or not | Implementation | | -- | -- | -- | | application/ld+json | 😀 | @rdfjs/serializer-jsonld-ext | | application/trig | 🤩 | @graphy/content.trig.writer | | text/n3 | 🤩 | @graphy/content.ttl.writer | | text/turtle | 🤩 | @graphy/content.ttl.writer | | application/n-triples | 😶 | @rdfjs/serializer-ntriples | | application/n-quads | 😶 | @rdfjs/serializer-ntriples | | application/rdf+xml | 😶 | @graphy/content.xml.scribe |
Individual serializer skins can also be created by importing from @rdfjs-elements/formats-pretty/serializers
. This
allows for initializing a sink preloaded with a given set of prefixes.
import prefixes from '@zazuko/prefixes'
import { TurtleSerializer } from '@rdfjs-elements/formats-pretty'
const { schema, dcterms, foaf } = prefixes
const sink = new TurtleSerializer({
prefixes: { schema, dcterms, foaf, ex: 'http://example/org/' }
})
This sink can then be used to produce pretty-printed RDF
import rdf from '@rdfjs/data-model'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'
// Example data
const data = [
rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]
const stream = await sink.import(Readable.from(data))
console.log(await getStream(stream))
// Outputs:
// @prefix schema: <http://schema.org/> .
// @prefix dcterms: <http://purl.org/dc/terms/> .
// @prefix foaf: <http://xmlns.com/foaf/0.1/> .
// @prefix ex: <http://example/org/> .
//
// ex:s1 schema:name "Alice" ;
// foaf:knows ex:o1 .
//
// ex:o1 schema:name "Bob" .
To get an output in n-triples, n-quads or n3 format the sink map interface can be used where media type must be specified:
import rdf from '@rdfjs/data-model'
import prefixes from '@zazuko/prefixes'
import formats from '@rdfjs-elements/formats-pretty'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'
// Example data
const data = [
rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]
const { schema, dcterms, foaf } = prefixes
const stream = formats.serialisers.import('application/n-triples', Readable.from(data), { schema, dcterms, foaf, ex:'http://example/org/' })
console.log(await getStream(stream))
Parsers
| Format | Implementation | | -- | -- | | application/ld+json | @rdfjs/parser-jsonld | | application/trig | @rdfjs/parser-n3 | | text/n3 | @rdfjs/parser-n3 | | text/turtle | @rdfjs/parser-n3 | | application/n-triples | @rdfjs/parser-n3 | | application/n-quads | @rdfjs/parser-n3 | | application/rdf+xml | rdfxml-streaming-parser |