-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from rdfjs-base/pretty
feat!: renamed package, added pretty-print serializers
- Loading branch information
Showing
18 changed files
with
424 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Formats from './lib/Formats.js' | ||
|
||
class Factory { | ||
init () { | ||
this.formats = new Formats({ factory: this }) | ||
} | ||
|
||
clone (original) { | ||
this.formats.import(original.formats) | ||
} | ||
} | ||
|
||
export default Factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Thomas Bergwinkl | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,42 @@ | ||
import JsonLdParser from '@rdfjs/parser-jsonld' | ||
import N3Parser from '@rdfjs/parser-n3' | ||
import NTriplesSerializer from '@rdfjs/serializer-ntriples' | ||
import TurtleSerializer from '@rdfjs/serializer-turtle' | ||
import SinkMap from '@rdfjs/sink-map' | ||
import JsonLdSerializer from './lib/CustomJsonLdSerializer.js' | ||
import RdfXmlParser from './lib/CustomRdfXmlParser.js' | ||
import JsonLdSerializer from './lib/JsonLdSerializer.js' | ||
import PrettyJsonLdSerializer from './lib/PrettyJsonLdSerializer.js' | ||
import RdfXmlParser from './lib/RdfXmlParser.js' | ||
|
||
const parsers = new SinkMap() | ||
const serializers = new SinkMap() | ||
const parsers = new SinkMap([ | ||
['application/ld+json', new JsonLdParser()], | ||
['application/trig', new N3Parser()], | ||
['application/n-quads', new N3Parser()], | ||
['application/n-triples', new N3Parser()], | ||
['text/n3', new N3Parser()], | ||
['text/turtle', new N3Parser()], | ||
['application/rdf+xml', new RdfXmlParser()] | ||
]) | ||
|
||
const serializers = new SinkMap([ | ||
['application/ld+json', new JsonLdSerializer()], | ||
['application/n-quads', new NTriplesSerializer()], | ||
['application/n-triples', new NTriplesSerializer()], | ||
['text/n3', new NTriplesSerializer()], | ||
['text/turtle', new NTriplesSerializer()] | ||
]) | ||
|
||
const formats = { | ||
parsers, | ||
serializers | ||
} | ||
|
||
formats.parsers.set('application/ld+json', new JsonLdParser()) | ||
formats.parsers.set('application/trig', new N3Parser()) | ||
formats.parsers.set('application/n-quads', new N3Parser()) | ||
formats.parsers.set('application/n-triples', new N3Parser()) | ||
formats.parsers.set('text/n3', new N3Parser()) | ||
formats.parsers.set('text/turtle', new N3Parser()) | ||
formats.parsers.set('application/rdf+xml', new RdfXmlParser()) | ||
|
||
formats.serializers.set('application/ld+json', new JsonLdSerializer()) | ||
formats.serializers.set('application/n-quads', new NTriplesSerializer()) | ||
formats.serializers.set('application/n-triples', new NTriplesSerializer()) | ||
formats.serializers.set('text/n3', new NTriplesSerializer()) | ||
formats.serializers.set('text/turtle', new NTriplesSerializer()) | ||
|
||
export { parsers, serializers } | ||
export default formats | ||
export { | ||
formats as default, | ||
JsonLdParser, | ||
JsonLdSerializer, | ||
N3Parser, | ||
NTriplesSerializer, | ||
RdfXmlParser | ||
PrettyJsonLdSerializer, | ||
RdfXmlParser, | ||
TurtleSerializer | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import SinkMap from '@rdfjs/sink-map' | ||
|
||
class Formats { | ||
constructor ({ factory }) { | ||
this.factory = factory | ||
this.parsers = new SinkMap() | ||
this.serializers = new SinkMap() | ||
} | ||
|
||
import (other) { | ||
if (other.parsers) { | ||
for (const [mediaType, parser] of other.parsers) { | ||
this.parsers.set(mediaType, new parser.constructor({ factory: this.factory })) | ||
} | ||
} | ||
|
||
if (other.serializers) { | ||
for (const [mediaType, serializer] of other.serializers) { | ||
this.serializers.set(mediaType, new serializer.constructor({ factory: this.factory })) | ||
} | ||
} | ||
|
||
return this | ||
} | ||
} | ||
|
||
export default Formats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import JsonLdSerializerBase from '@rdfjs/serializer-jsonld' | ||
|
||
class JsonLdSerializer extends JsonLdSerializerBase { | ||
constructor ({ ...args } = {}) { | ||
super({ ...args, encoding: 'string' }) | ||
} | ||
} | ||
|
||
export default JsonLdSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import JsonLdSerializer from '@rdfjs/serializer-jsonld-ext' | ||
|
||
class PrettyJsonLdSerializer extends JsonLdSerializer { | ||
constructor ({ ...args } = {}) { | ||
super({ | ||
...args, | ||
compact: true, | ||
encoding: 'string', | ||
prettyPrint: true | ||
}) | ||
} | ||
} | ||
|
||
export default PrettyJsonLdSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RdfXmlParser as RdfXmlParserBase } from 'rdfxml-streaming-parser' | ||
|
||
class RdfXmlParser extends RdfXmlParserBase { | ||
constructor ({ factory, ...args } = {}) { | ||
super({ ...args, dataFactory: factory }) | ||
} | ||
} | ||
|
||
export default RdfXmlParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
{ | ||
"name": "@rdfjs/formats-common", | ||
"name": "@rdfjs/formats", | ||
"version": "3.1.0", | ||
"description": "Parsers and serializers for common RDF formats", | ||
"description": "Bundle of RDF/JS parsers and serializers", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "stricter-standard && c8 --reporter=lcov --reporter=text mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/rdfjs-base/formats-common.git" | ||
"url": "https://github.com/rdfjs-base/formats.git" | ||
}, | ||
"keywords": [ | ||
"rdf", | ||
|
@@ -21,20 +21,23 @@ | |
"author": "Thomas Bergwinkl <[email protected]> (https://www.bergnet.org/people/bergi/card#me)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/rdfjs-base/formats-common/issues" | ||
"url": "https://github.com/rdfjs-base/formats/issues" | ||
}, | ||
"homepage": "https://github.com/rdfjs-base/formats-common", | ||
"homepage": "https://github.com/rdfjs-base/formats", | ||
"dependencies": { | ||
"@rdfjs/parser-jsonld": "^2.0.0", | ||
"@rdfjs/parser-n3": "^2.0.0", | ||
"@rdfjs/parser-jsonld": "^2.1.0", | ||
"@rdfjs/parser-n3": "^2.0.1", | ||
"@rdfjs/serializer-jsonld": "^2.0.0", | ||
"@rdfjs/serializer-jsonld-ext": "^4.0.0", | ||
"@rdfjs/serializer-ntriples": "^2.0.0", | ||
"@rdfjs/serializer-turtle": "^1.1.1", | ||
"@rdfjs/sink-map": "^2.0.0", | ||
"rdfxml-streaming-parser": "^2.2.0" | ||
"rdfxml-streaming-parser": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"c8": "^7.12.0", | ||
"mocha": "^10.0.0", | ||
"stricter-standard": "^0.2.0" | ||
"@rdfjs/environment": "^1.0.0", | ||
"c8": "^8.0.1", | ||
"mocha": "^10.2.0", | ||
"stricter-standard": "^0.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import SinkMap from '@rdfjs/sink-map' | ||
import defaultFormats, { PrettyJsonLdSerializer, TurtleSerializer } from './index.js' | ||
|
||
const parsers = new SinkMap(defaultFormats.parsers) | ||
|
||
const serializers = new SinkMap(defaultFormats.serializers) | ||
|
||
serializers.set('application/ld+json', new PrettyJsonLdSerializer()) | ||
serializers.set('text/n3', new TurtleSerializer()) | ||
serializers.set('text/turtle', new TurtleSerializer()) | ||
|
||
const formats = { | ||
parsers, | ||
serializers | ||
} | ||
|
||
export default formats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { notStrictEqual, strictEqual } from 'assert' | ||
import Environment from '@rdfjs/environment' | ||
import { describe, it } from 'mocha' | ||
import Factory from '../Factory.js' | ||
import Formats from '../lib/Formats.js' | ||
|
||
describe('Factory', () => { | ||
it('should be a constructor', () => { | ||
strictEqual(typeof Factory, 'function') | ||
}) | ||
|
||
it('should attach a Formats instance to .formats', () => { | ||
const env = new Environment([Factory]) | ||
|
||
strictEqual(env.formats instanceof Formats, true) | ||
}) | ||
|
||
it('should assign itself as factory to the formats instance', () => { | ||
const env = new Environment([Factory]) | ||
|
||
strictEqual(env.formats.factory, env) | ||
}) | ||
|
||
describe('.clone', () => { | ||
it('should import the parsers and serializers from the original environment', () => { | ||
class Parser {} | ||
class Serializer {} | ||
const original = { | ||
parsers: new Map([['a', new Parser()]]), | ||
serializers: new Map([['a', new Serializer()]]) | ||
} | ||
const env = new Environment([Factory]) | ||
env.formats.import(original) | ||
|
||
const clone = env.clone() | ||
|
||
strictEqual(clone.formats.parsers.size, 1) | ||
strictEqual(clone.formats.parsers.get('a') instanceof Parser, true) | ||
notStrictEqual(clone.formats.parsers.get('a'), env.formats.parsers.get('a')) | ||
strictEqual(clone.formats.serializers.size, 1) | ||
strictEqual(clone.formats.serializers.get('a') instanceof Serializer, true) | ||
notStrictEqual(clone.formats.serializers.get('a'), env.formats.serializers.get('a')) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.