@lblod/graph-rdfa-processor 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
+
10
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
11
+
12
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
13
+
14
+ var URIResolver =
15
+ /*#__PURE__*/
16
+ function () {
17
+ function URIResolver() {
18
+ _classCallCheck(this, URIResolver);
19
+ }
20
+
21
+ _createClass(URIResolver, [{
22
+ key: "parseURI",
23
+ value: function parseURI(uri) {
24
+ uri = uri.replace("\n", ""); // bugfix: sometimes there is a \n in the uri...
25
+
26
+ var match = URIResolver.SCHEME.exec(uri);
27
+
28
+ if (!match) {
29
+ throw new Error("Bad URI value, no scheme: " + uri);
30
+ }
31
+
32
+ var parsed = {
33
+ spec: uri
34
+ };
35
+ parsed.scheme = match[0].substring(0, match[0].length - 1);
36
+ parsed.schemeSpecificPart = parsed.spec.substring(match[0].length);
37
+
38
+ if (parsed.schemeSpecificPart.charAt(0) == "/" && parsed.schemeSpecificPart.charAt(1) == "/") {
39
+ this.parseGeneric(parsed);
40
+ } else {
41
+ parsed.isGeneric = false;
42
+ }
43
+
44
+ parsed.normalize = function () {
45
+ if (!this.isGeneric) {
46
+ return;
47
+ }
48
+
49
+ if (this.segments.length == 0) {
50
+ return;
51
+ } // edge case of ending in "/."
52
+
53
+
54
+ if (this.path.length > 1 && this.path.substring(this.path.length - 2) == "/.") {
55
+ this.path = this.path.substring(0, this.path.length - 1);
56
+ this.segments.splice(this.segments.length - 1, 1);
57
+ this.schemeSpecificPart = "//" + this.authority + this.path;
58
+
59
+ if (typeof this.query != "undefined") {
60
+ this.schemeSpecificPart += "?" + this.query;
61
+ }
62
+
63
+ if (typeof this.fragment != "undefined") {
64
+ this.schemeSpecificPart += "#" + this.fragment;
65
+ }
66
+
67
+ this.spec = this.scheme + ":" + this.schemeSpecificPart;
68
+ return;
69
+ }
70
+
71
+ var end = this.path.charAt(this.path.length - 1);
72
+
73
+ if (end != "/") {
74
+ end = "";
75
+ }
76
+
77
+ for (var i = 0; i < this.segments.length; i++) {
78
+ if (i > 0 && this.segments[i] == "..") {
79
+ this.segments.splice(i - 1, 2);
80
+ i -= 2;
81
+ }
82
+
83
+ if (this.segments[i] == ".") {
84
+ this.segments.splice(i, 1);
85
+ i--;
86
+ }
87
+ }
88
+
89
+ this.path = this.segments.length == 0 ? "/" : "/" + this.segments.join("/") + end;
90
+ this.schemeSpecificPart = "//" + this.authority + this.path;
91
+
92
+ if (typeof this.query != "undefined") {
93
+ this.schemeSpecificPart += "?" + this.query;
94
+ }
95
+
96
+ if (typeof this.fragment != "undefined") {
97
+ this.schemeSpecificPart += "#" + this.fragment;
98
+ }
99
+
100
+ this.spec = this.scheme + ":" + this.schemeSpecificPart;
101
+ };
102
+
103
+ parsed.resolve = function (href) {
104
+ if (!href) {
105
+ return this.spec;
106
+ }
107
+
108
+ if (href.charAt(0) == "#") {
109
+ var lastHash = this.spec.lastIndexOf("#");
110
+ return lastHash < 0 ? this.spec + href : this.spec.substring(0, lastHash) + href;
111
+ }
112
+
113
+ if (!this.isGeneric) {
114
+ throw new Error("Cannot resolve uri against non-generic URI: " + this.spec);
115
+ }
116
+
117
+ var colon = href.indexOf(":");
118
+
119
+ if (href.charAt(0) == "/") {
120
+ return this.scheme + "://" + this.authority + href;
121
+ } else if (href.charAt(0) == "." && href.charAt(1) == "/") {
122
+ if (this.path.charAt(this.path.length - 1) == "/") {
123
+ return this.scheme + "://" + this.authority + this.path + href.substring(2);
124
+ } else {
125
+ var last = this.path.lastIndexOf("/");
126
+ return this.scheme + "://" + this.authority + this.path.substring(0, last) + href.substring(1);
127
+ }
128
+ } else if (URIResolver.SCHEME.test(href)) {
129
+ return href;
130
+ } else if (href.charAt(0) == "?") {
131
+ return this.scheme + "://" + this.authority + this.path + href;
132
+ } else {
133
+ if (this.path.charAt(this.path.length - 1) == "/") {
134
+ return this.scheme + "://" + this.authority + this.path + href;
135
+ } else {
136
+ var last = this.path.lastIndexOf("/");
137
+ return this.scheme + "://" + this.authority + this.path.substring(0, last + 1) + href;
138
+ }
139
+ }
140
+ };
141
+
142
+ parsed.relativeTo = function (otherURI) {
143
+ if (otherURI.scheme != this.scheme) {
144
+ return this.spec;
145
+ }
146
+
147
+ if (!this.isGeneric) {
148
+ throw new Error("A non generic URI cannot be made relative: " + this.spec);
149
+ }
150
+
151
+ if (!otherURI.isGeneric) {
152
+ throw new Error("Cannot make a relative URI against a non-generic URI: " + otherURI.spec);
153
+ }
154
+
155
+ if (otherURI.authority != this.authority) {
156
+ return this.spec;
157
+ }
158
+
159
+ var i = 0;
160
+
161
+ for (; i < this.segments.length && i < otherURI.segments.length; i++) {
162
+ if (this.segments[i] != otherURI.segments[i]) {
163
+ //alert(this.path+" different from "+otherURI.path+" at '"+this.segments[i]+"' vs '"+otherURI.segments[i]+"'");
164
+ var offset = otherURI.path.charAt(otherURI.path.length - 1) == "/" ? 0 : -1;
165
+ var relative = "";
166
+
167
+ for (var j = i; j < otherURI.segments.length + offset; j++) {
168
+ relative += "../";
169
+ }
170
+
171
+ for (var j = i; j < this.segments.length; j++) {
172
+ relative += this.segments[j];
173
+
174
+ if (j + 1 < this.segments.length) {
175
+ relative += "/";
176
+ }
177
+ }
178
+
179
+ if (this.path.charAt(this.path.length - 1) == "/") {
180
+ relative += "/";
181
+ }
182
+
183
+ return relative;
184
+ }
185
+ }
186
+
187
+ if (this.segments.length == otherURI.segments.length) {
188
+ return this.hash ? this.hash : this.query ? this.query : "";
189
+ } else if (i < this.segments.length) {
190
+ var relative = "";
191
+
192
+ for (var j = i; j < this.segments.length; j++) {
193
+ relative += this.segments[j];
194
+
195
+ if (j + 1 < this.segments.length) {
196
+ relative += "/";
197
+ }
198
+ }
199
+
200
+ if (this.path.charAt(this.path.length - 1) == "/") {
201
+ relative += "/";
202
+ }
203
+
204
+ return relative;
205
+ } else {
206
+ throw new Error("Cannot calculate a relative URI for " + this.spec + " against " + otherURI.spec);
207
+ }
208
+ };
209
+
210
+ return parsed;
211
+ }
212
+ }, {
213
+ key: "parseGeneric",
214
+ value: function parseGeneric(parsed) {
215
+ if (parsed.schemeSpecificPart.charAt(0) != "/" || parsed.schemeSpecificPart.charAt(1) != "/") {
216
+ throw new Error("Generic URI values should start with '//':" + parsed.spec);
217
+ }
218
+
219
+ var work = parsed.schemeSpecificPart.substring(2);
220
+ var pathStart = work.indexOf("/");
221
+ parsed.authority = pathStart < 0 ? work : work.substring(0, pathStart);
222
+ parsed.path = pathStart < 0 ? "" : work.substring(pathStart);
223
+ var hash = parsed.path.indexOf("#");
224
+
225
+ if (hash >= 0) {
226
+ parsed.fragment = parsed.path.substring(hash + 1);
227
+ parsed.path = parsed.path.substring(0, hash);
228
+ }
229
+
230
+ var questionMark = parsed.path.indexOf("?");
231
+
232
+ if (questionMark >= 0) {
233
+ parsed.query = parsed.path.substring(questionMark + 1);
234
+ parsed.path = parsed.path.substring(0, questionMark);
235
+ }
236
+
237
+ if (parsed.path == "/" || parsed.path == "") {
238
+ parsed.segments = [];
239
+ } else {
240
+ parsed.segments = parsed.path.split(/\//);
241
+
242
+ if (parsed.segments.length > 0 && parsed.segments[0] == "" && parsed.path.length > 1 && parsed.path.charAt(1) != "/") {
243
+ // empty segment at the start, remove it
244
+ parsed.segments.shift();
245
+ }
246
+
247
+ if (parsed.segments.length > 0 && parsed.path.length > 0 && parsed.path.charAt(parsed.path.length - 1) == "/" && parsed.segments[parsed.segments.length - 1] == "") {
248
+ // we may have an empty the end
249
+ // check to see if it is legimate
250
+ if (parsed.path.length > 1 && parsed.path.charAt(parsed.path.length - 2) != "/") {
251
+ parsed.segments.pop();
252
+ }
253
+ } // check for non-escaped characters
254
+
255
+
256
+ for (var i = 0; i < parsed.segments.length; i++) {
257
+ var check = parsed.segments[i].split(/%[A-Za-z0-9][A-Za-z0-9]|[\ud800-\udfff][\ud800-\udfff]|[A-Za-z0-9\-\._~!$&'()*+,;=@:\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+/);
258
+
259
+ for (var j = 0; j < check.length; j++) {
260
+ if (check[j].length > 0) {
261
+ throw new Error("Unescaped character " + check[j].charAt(0) + " (" + check[j].charCodeAt(0) + ") in URI " + parsed.spec);
262
+ }
263
+ }
264
+ }
265
+ }
266
+
267
+ parsed.isGeneric = true;
268
+ }
269
+ }]);
270
+
271
+ return URIResolver;
272
+ }();
273
+
274
+ exports.default = URIResolver;
275
+ URIResolver.SCHEME = /^[A-Za-z][A-Za-z0-9\+\-\.]*\:/;
276
+ module.exports = exports.default;
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@lblod/graph-rdfa-processor",
3
+ "version": "0.13.2",
4
+ "description": "Green turtle GraphRdfaProcessor extracted and available as a commonJS module",
5
+ "main": "dist/index.js",
6
+ "module": "src/index.js",
7
+ "scripts": {
8
+ "test": "mocha --require @babel/register --exit",
9
+ "prepare": "rm -rf dist && babel src --out-dir dist",
10
+ "postversion": "git push && git push --tags"
11
+ },
12
+ "babel": {
13
+ "presets": [
14
+ [
15
+ "@babel/preset-env",
16
+ {
17
+ "targets": "> 0.25%, not dead"
18
+ }
19
+ ]
20
+ ],
21
+ "plugins": [
22
+ "add-module-exports"
23
+ ]
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/science-periodicals/graph-rdfa-processor.git"
28
+ },
29
+ "keywords": [
30
+ "RDFa"
31
+ ],
32
+ "author": "Sebastien Ballesteros",
33
+ "license": "Apache-2.0",
34
+ "bugs": {
35
+ "url": "https://github.com/science-periodicals/graph-rdfa-processor/issues"
36
+ },
37
+ "homepage": "https://github.com/science-periodicals/graph-rdfa-processor#readme",
38
+ "devDependencies": {
39
+ "@babel/cli": "^7.5.5",
40
+ "@babel/core": "^7.5.5",
41
+ "@babel/preset-env": "^7.5.5",
42
+ "@babel/register": "^7.5.5",
43
+ "babel-plugin-add-module-exports": "^1.0.2",
44
+ "jsdom": "^9.11.0",
45
+ "mocha": "^6.2.0"
46
+ }
47
+ }
package/release.sh ADDED
@@ -0,0 +1,15 @@
1
+ # usage: bash release.sh 0.1.2
2
+ git checkout master
3
+ git pull
4
+
5
+ npm version $1
6
+ npm i
7
+ git add .
8
+ git commit -m "version $1"
9
+
10
+ echo "push tag $1..."
11
+ git push origin v$1
12
+
13
+ git push
14
+
15
+ echo "done."
@@ -0,0 +1,177 @@
1
+ import RDFaProcessor from './rdfa-processor';
2
+ import { RDFaSubject, RDFaPredicate } from './rdfa-graph';
3
+
4
+ export default class GraphRDFaProcessor extends RDFaProcessor {
5
+ constructor(target) {
6
+ super(target);
7
+ }
8
+
9
+ getObjectSize(obj) {
10
+ var size = 0;
11
+ for (var key in obj) {
12
+ if (obj.hasOwnProperty(key)) {
13
+ size++;
14
+ }
15
+ }
16
+ return size;
17
+ }
18
+
19
+ init() {
20
+ var thisObj = this;
21
+ this.finishedHandlers.push(function(node) {
22
+ for (var subject in thisObj.target.graph.subjects) {
23
+ var snode = thisObj.target.graph.subjects[subject];
24
+ if (thisObj.getObjectSize(snode.predicates)==0) {
25
+ delete thisObj.target.graph.subjects[subject];
26
+ }
27
+ }
28
+ });
29
+ }
30
+
31
+ newBlankNode() {
32
+ return this.target.graph.newBlankNode();
33
+ }
34
+
35
+ newSubjectOrigin(origin,subject) {
36
+ var snode = this.newSubject(null,subject);
37
+ for (var i=0; i < snode.origins.length; i++) {
38
+ if (snode.origins[i]===origin) {
39
+ return;
40
+ }
41
+ }
42
+ snode.origins.push(origin);
43
+ if (!origin.data) {
44
+ Object.defineProperty(origin,"data", {
45
+ value: snode,
46
+ writable: false,
47
+ configurable: true,
48
+ enumerable: true
49
+ });
50
+ }
51
+ }
52
+
53
+ newSubject(origin,subject) {
54
+ var snode = this.target.graph.subjects[subject];
55
+ if (!snode) {
56
+ snode = new RDFaSubject(this.target.graph,subject);
57
+ this.target.graph.subjects[subject] = snode;
58
+ }
59
+ return snode;
60
+ }
61
+
62
+
63
+ addTriple(origin,subject,predicate,object) {
64
+ var snode = this.newSubject(origin,subject);
65
+ var pnode = snode.predicates[predicate];
66
+ if (!pnode) {
67
+ pnode = new RDFaPredicate(predicate);
68
+ snode.predicates[predicate] = pnode;
69
+ }
70
+
71
+ for (var i=0; i < pnode.objects.length; i++) {
72
+ if (pnode.objects[i].type==object.type && pnode.objects[i].value==object.value) {
73
+ if (pnode.objects[i].origin!==origin) {
74
+ if (!Array.isArray(pnode.objects[i].origin)) {
75
+ var origins = [];
76
+ origins.push(pnode.objects[i].origin);
77
+ pnode.objects[i].origin = origins;
78
+ }
79
+ pnode.objects[i].origin.push(origin);
80
+ }
81
+ return;
82
+ }
83
+ }
84
+ pnode.objects.push(object);
85
+ object.origin = origin;
86
+ if (predicate==RDFaProcessor.typeURI) {
87
+ snode.types.push(object.value);
88
+ }
89
+ }
90
+
91
+
92
+ copyProperties() {
93
+ var copySubjects = [];
94
+ var patternSubjects = {};
95
+ for (var subject in this.target.graph.subjects) {
96
+ var snode = this.target.graph.subjects[subject];
97
+ var pnode = snode.predicates[GraphRDFaProcessor.rdfaCopyPredicate];
98
+ if (!pnode) {
99
+ continue;
100
+ }
101
+ copySubjects.push(subject);
102
+ for (var i=0; i < pnode.objects.length; i++) {
103
+ if (pnode.objects[i].type!=RDFaProcessor.objectURI) {
104
+ continue;
105
+ }
106
+ var target = pnode.objects[i].value;
107
+ var patternSubjectNode = this.target.graph.subjects[target];
108
+ if (!patternSubjectNode) {
109
+ continue;
110
+ }
111
+ var patternTypes = patternSubjectNode.predicates[RDFaProcessor.typeURI];
112
+ if (!patternTypes) {
113
+ continue;
114
+ }
115
+ var isPattern = false;
116
+ for (var j=0; j < patternTypes.objects.length && !isPattern; j++) {
117
+ if (patternTypes.objects[j].value==GraphRDFaProcessor.rdfaPatternType &&
118
+ patternTypes.objects[j].type==RDFaProcessor.objectURI) {
119
+ isPattern = true;
120
+ }
121
+ }
122
+ if (!isPattern) {
123
+ continue;
124
+ }
125
+ patternSubjects[target] = true;
126
+ for (var predicate in patternSubjectNode.predicates) {
127
+ var targetPNode = patternSubjectNode.predicates[predicate];
128
+ if (predicate==RDFaProcessor.typeURI) {
129
+ if (targetPNode.objects.length==1) {
130
+ continue;
131
+ }
132
+ for (var j=0; j < targetPNode.objects.length; j++) {
133
+ if (targetPNode.objects[j].value!=GraphRDFaProcessor.rdfaPatternType) {
134
+ var subjectPNode = snode.predicates[predicate];
135
+ if (!subjectPNode) {
136
+ subjectPNode = new RDFaPredicate(predicate);
137
+ snode.predicates[predicate] = subjectPNode;
138
+ }
139
+ subjectPNode.objects.push(
140
+ { type: targetPNode.objects[j].type,
141
+ value: targetPNode.objects[j].value,
142
+ language: targetPNode.objects[j].language,
143
+ origin: targetPNode.objects[j].origin}
144
+ );
145
+ snode.types.push(targetPNode.objects[j].value);
146
+ }
147
+ }
148
+ } else {
149
+ var subjectPNode = snode.predicates[predicate];
150
+ if (!subjectPNode) {
151
+ subjectPNode = new RDFaPredicate(predicate);
152
+ snode.predicates[predicate] = subjectPNode;
153
+ }
154
+ for (var j=0; j < targetPNode.objects.length; j++) {
155
+ subjectPNode.objects.push(
156
+ { type: targetPNode.objects[j].type,
157
+ value: targetPNode.objects[j].value,
158
+ language: targetPNode.objects[j].language,
159
+ origin: targetPNode.objects[j].origin}
160
+ );
161
+ }
162
+ }
163
+ }
164
+ }
165
+ }
166
+ for (var i=0; i < copySubjects.length; i++) {
167
+ var snode = this.target.graph.subjects[copySubjects[i]];
168
+ delete snode.predicates[GraphRDFaProcessor.rdfaCopyPredicate];
169
+ }
170
+ for (var subject in patternSubjects) {
171
+ delete this.target.graph.subjects[subject];
172
+ }
173
+ }
174
+ };
175
+
176
+ GraphRDFaProcessor.rdfaCopyPredicate = "http://www.w3.org/ns/rdfa#copy";
177
+ GraphRDFaProcessor.rdfaPatternType = "http://www.w3.org/ns/rdfa#Pattern";
package/src/index.js ADDED
@@ -0,0 +1,21 @@
1
+ import URIResolver from "./uri-resolver";
2
+ import GraphRDFaProcessor from "./graph-rdfa-processor";
3
+ import { RDFaGraph } from "./rdfa-graph";
4
+
5
+ export default function(document, options = {}) {
6
+ let node = document.documentElement || document;
7
+ let baseURI = options.baseURI ? options.baseURI : node.baseURI;
8
+ let specialHtmlPredicates = options.specialHtmlPredicates || [];
9
+
10
+ let graph = new RDFaGraph();
11
+
12
+ let target = {
13
+ graph,
14
+ baseURI: new URIResolver().parseURI(baseURI),
15
+ specialHtmlPredicates,
16
+ };
17
+
18
+ var processor = new GraphRDFaProcessor(target);
19
+ processor.process(node, options);
20
+ return target.graph;
21
+ }
package/src/node.js ADDED
@@ -0,0 +1,16 @@
1
+ const Node = {
2
+ ELEMENT_NODE: 1,
3
+ ATTRIBUTE_NODE: 2,
4
+ TEXT_NODE: 3,
5
+ CDATA_SECTION_NODE: 4,
6
+ ENTITY_REFERENCE_NODE: 5,
7
+ ENTITY_NODE: 6,
8
+ PROCESSING_INSTRUCTION_NODE: 7,
9
+ COMMENT_NODE: 8,
10
+ DOCUMENT_NODE: 9,
11
+ DOCUMENT_TYPE_NODE: 10,
12
+ DOCUMENT_FRAGMENT_NODE: 11,
13
+ NOTATION_NODE:12
14
+ };
15
+
16
+ export default Node;