Skip to content

Split out RDFa code from jsonld.js. #1

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
env: {
browser: true,
commonjs: true,
node: true
},
extends: 'eslint-config-digitalbazaar',
root: true
};
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.sublime-project
*.sublime-workspace
*.sw[op]
*~
.DS_Store
.cproject
.project
.settings
coverage
dist
node_modules
v8.log
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# jsonld-rdfa ChangeLog

## 0.1.0 - 20xx-xx-xx

### Added
- Split out RDFa code from jsonld.js.
40 changes: 40 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
You may use the jsonld-rdfa project under the terms of the BSD License.

You are free to use this project in commercial projects as long as the
copyright header is left intact.

If you are a commercial entity and use this set of libraries in your
commercial software then reasonable payment to Digital Bazaar, if you can
afford it, is not required but is expected and would be appreciated. If this
library saves you time, then it's saving you money. The cost of developing
JSON-LD was on the order of several months of work and tens of
thousands of dollars. We are attempting to strike a balance between helping
the development community while not being taken advantage of by lucrative
commercial entities for our efforts.

-------------------------------------------------------------------------------
New BSD License (3-clause)
Copyright (c) 2010, Digital Bazaar, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Digital Bazaar, Inc. nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DIGITAL BAZAAR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
jsonld-rdfa
===========

...
[![Dependency Status](https://img.shields.io/david/digitalbazaar/jsonld-rdfa.svg)](https://david-dm.org/digitalbazaar/jsonld-rdfa)

Introduction
------------

This JavaScript [Node.js][] library is used to register a [RDFa][] parser with
[jsonld.js][].

## Installation

```
npm install jsonld-rdfa
```

## Usage

```js
// load and register RDFa parser
require('jsonld-rdfa');
```

Commercial Support
------------------

Commercial support for this library is available upon request from
[Digital Bazaar][]: support@digitalbazaar.com

Source Code
-----------

http://github.com/digitalbazaar/jsonld-rdfa

[Digital Bazaar]: https://digitalbazaar.com/
[JSON-LD]: https://json-ld.org/
[RDFa]: https://www.w3.org/TR/rdfa-core/
[jsonld.js]: https://github.com/digitalbazaar/jsonld.js
[Node.js]: https://nodejs.org/
142 changes: 142 additions & 0 deletions lib/Rdfa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
*/
/* global Node, XMLSerializer */

'use strict';

const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const XSD = 'http://www.w3.org/2001/XMLSchema#';

const RDF_LANGSTRING = RDF + 'langString';
const RDF_PLAIN_LITERAL = RDF + 'PlainLiteral';
const RDF_OBJECT = RDF + 'object';
const RDF_XML_LITERAL = RDF + 'XMLLiteral';
const XSD_STRING = XSD + 'string';

let _Node;
if(typeof Node !== 'undefined') {
_Node = Node;
} else {
_Node = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
ENTITY_NODE: 6,
PROCESSING_INSTRUCTION_NODE: 7,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_TYPE_NODE: 10,
DOCUMENT_FRAGMENT_NODE: 11,
NOTATION_NODE: 12
};
}

module.exports = class Rdfa {
/**
* Parses the RDF dataset found via the data object from the RDFa API.
*
* @param data the RDFa API data object.
*
* @return the RDF dataset.
*/
parse(data) {
const dataset = {};
dataset['@default'] = [];

const subjects = data.getSubjects();
for(let si = 0; si < subjects.length; ++si) {
const subject = subjects[si];
if(subject === null) {
continue;
}

// get all related triples
const triples = data.getSubjectTriples(subject);
if(triples === null) {
continue;
}
const predicates = triples.predicates;
for(const predicate in predicates) {
// iterate over objects
const objects = predicates[predicate].objects;
for(let oi = 0; oi < objects.length; ++oi) {
const object = objects[oi];

// create RDF triple
const triple = {};

// add subject
if(subject.indexOf('_:') === 0) {
triple.subject = {type: 'blank node', value: subject};
} else {
triple.subject = {type: 'IRI', value: subject};
}

// add predicate
if(predicate.indexOf('_:') === 0) {
triple.predicate = {type: 'blank node', value: predicate};
} else {
triple.predicate = {type: 'IRI', value: predicate};
}

// serialize XML literal
let value = object.value;
if(object.type === RDF_XML_LITERAL) {
// initialize XMLSerializer
const XMLSerializer = getXMLSerializerClass();
const serializer = new XMLSerializer();
value = '';
for(let x = 0; x < object.value.length; x++) {
if(object.value[x].nodeType === _Node.ELEMENT_NODE) {
value += serializer.serializeToString(object.value[x]);
} else if(object.value[x].nodeType === _Node.TEXT_NODE) {
value += object.value[x].nodeValue;
}
}
}

// add object
triple.object = {};

// object is an IRI
if(object.type === RDF_OBJECT) {
if(object.value.indexOf('_:') === 0) {
triple.object.type = 'blank node';
} else {
triple.object.type = 'IRI';
}
} else {
// object is a literal
triple.object.type = 'literal';
if(object.type === RDF_PLAIN_LITERAL) {
if(object.language) {
triple.object.datatype = RDF_LANGSTRING;
triple.object.language = object.language;
} else {
triple.object.datatype = XSD_STRING;
}
} else {
triple.object.datatype = object.type;
}
}
triple.object.value = value;

// add triple to dataset in default graph
dataset['@default'].push(triple);
}
}
}

return dataset;
}
};

function getXMLSerializerClass() {
if(typeof XMLSerializer === 'undefined') {
return require('xmldom').XMLSerializer;
}
return XMLSerializer;
}
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* RDFa plugin for jsonld.js.
*
* @author Digital Bazaar, Inc.
*
* Copyright 2010-2019 Digital Bazaar, Inc.
*/
if(require('semver').gte(process.version, '8.6.0')) {
module.exports = require('./jsonld-rdfa');
} else {
module.exports = require('../dist/node6/lib/jsonld-rdfa');
}
40 changes: 40 additions & 0 deletions lib/jsonld-rdfa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* A RDFa plugin for jsonld.js.
*
* @author Digital Bazaar, Inc.
*
* @license BSD 3-Clause License
* Copyright (c) 2011-2019 Digital Bazaar, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the Digital Bazaar, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
const jsonld = require('jsonld');
const Rdfa = require('./Rdfa');

// register the RDFa API RDF parser
jsonld.registerRDFParser('rdfa-api', Rdfa.parse);
81 changes: 81 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"name": "jsonld-rdfa",
"version": "0.1.0-dev",
"description": "A jsonld.js plugin to hande RDFa.",
"homepage": "https://github.com/digitalbazaar/jsonld-rdfa",
"author": {
"name": "Digital Bazaar, Inc.",
"email": "support@digitalbazaar.com",
"url": "https://digitalbazaar.com/"
},
"contributors": [
"Dave Longley <dlongley@digitalbazaar.com>",
"David I. Lehn <dlehn@digitalbazaar.com>"
],
"repository": {
"type": "git",
"url": "https://github.com/digitalbazaar/jsonld-rdfa"
},
"bugs": {
"url": "https://github.com/digitalbazaar/jsonld-rdfa/issues",
"email": "support@digitalbazaar.com"
},
"license": "BSD-3-Clause",
"main": "lib/index.js",
"files": [
"dist/*.js",
"dist/*.js.map",
"dist/node6/**/*.js",
"lib/*.js",
"lib/**/*.js"
],
"dependencies": {
"semver": "^6.3.0",
"xmldom": "0.1.19"
},
"peerDependencies": {
"jsonld": "^1.8.1"
},
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/plugin-proposal-object-rest-spread": "^7.7.4",
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
"@babel/plugin-transform-runtime": "^7.7.5",
"@babel/preset-env": "^7.7.5",
"@babel/register": "^7.7.4",
"@babel/runtime": "^7.7.5",
"babel-loader": "^8.0.5",
"core-js": "^3.4.7",
"eslint": "^6.7.2",
"eslint-config-digitalbazaar": "^2.0.1",
"regenerator-runtime": "^0.13.3",
"webpack": "^4.29.5",
"webpack-cli": "^3.2.3",
"webpack-merge": "^4.2.1"
},
"engines": {
"node": ">=6"
},
"keywords": [
"JSON",
"JSON-LD",
"Linked Data",
"RDF",
"RDFa",
"Semantic Web",
"jsonld"
],
"scripts": {
"prepublish": "npm run build",
"build": "npm run build-webpack && npm run build-node6",
"build-webpack": "webpack",
"build-node6": "BROWSERSLIST='node 6' babel --no-babelrc lib --out-dir dist/node6/lib --presets=@babel/preset-env",
"lint": "eslint *.js lib/**.js"
},
"browser": {
"lib/index.js": "./lib/jsonld-rdfa.js",
"jsonld": false,
"xmldom": false
}
}
Loading