Skip to content

Commit 66fbc01

Browse files
committed
#318 Added script to sync Turtle & JSON-LD snippets
1 parent 5444fbb commit 66fbc01

File tree

7 files changed

+5435
-0
lines changed

7 files changed

+5435
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ crashlytics.properties
4747
crashlytics-build.properties
4848

4949
# Created by .ignore support plugin (hsz.mobi)
50+
51+
# Node.js packages
52+
node_modules/

scripts/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Scripts
2+
3+
## sync-code-snippets.js
4+
5+
Verifies if the Turtle and JSON-LD code snippets are in sync and generates JSON-LD for missing or out-of-sync Turtle code snippets.
6+
7+
Call the script with `--help` for usage information.

scripts/lib/prettyJsonld.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
function countBlankNode (json, id) {
2+
if (typeof json !== 'object') {
3+
return 0
4+
}
5+
6+
let count = 0
7+
8+
if (json['@id'] === id) {
9+
count++
10+
}
11+
12+
for (const child of Object.values(json)) {
13+
count += countBlankNode(child, id)
14+
}
15+
16+
return count
17+
}
18+
19+
function findRef (json, id) {
20+
if (typeof json !== 'object') {
21+
return null
22+
}
23+
24+
if (json['@id'] === id && Object.keys(json).length === 1) {
25+
return json
26+
}
27+
28+
for (const child of Object.values(json)) {
29+
const ref = findRef(child, id)
30+
31+
if (ref) {
32+
return ref
33+
}
34+
}
35+
36+
return null
37+
}
38+
39+
function rebuildBlankNodeIds (json, blankNodeMap) {
40+
if (typeof json !== 'object') {
41+
return
42+
}
43+
44+
if (json['@id'] && json['@id'].startsWith('_:')) {
45+
const id = json['@id']
46+
47+
if (!blankNodeMap.has(id)) {
48+
blankNodeMap.set(id, `_:b${blankNodeMap.size + 1}`)
49+
}
50+
51+
json['@id'] = blankNodeMap.get(id)
52+
}
53+
54+
for (const child of Object.values(json)) {
55+
rebuildBlankNodeIds(child, blankNodeMap)
56+
}
57+
}
58+
59+
function prettyJsonld (json) {
60+
if (!json['@graph']) {
61+
return json
62+
}
63+
64+
const toClean = new Set()
65+
const toLink = new Set()
66+
67+
for (const child of json['@graph']) {
68+
const id = child['@id']
69+
70+
if (id.startsWith('_:')) {
71+
const count = countBlankNode(json, id)
72+
73+
if (count === 1) {
74+
toClean.add(id)
75+
}
76+
77+
if (count === 2) {
78+
toLink.add(id)
79+
}
80+
}
81+
}
82+
83+
json['@graph'] = json['@graph'].sort((a, b) => a['@id'].localeCompare(b['@id']))
84+
85+
for (const id of toClean) {
86+
const node = json['@graph'].find(n => n['@id'] === id)
87+
88+
delete node['@id']
89+
}
90+
91+
for (const id of toLink) {
92+
const nodeIndex = json['@graph'].findIndex(n => n['@id'] === id)
93+
const node = json['@graph'][nodeIndex]
94+
const ref = findRef(json, id)
95+
96+
delete node['@id']
97+
delete ref['@id']
98+
99+
for (const [key, values] of Object.entries(node)) {
100+
ref[key] = values
101+
}
102+
103+
json['@graph'].splice(nodeIndex, 1)
104+
}
105+
106+
const blankNodeMap = new Map()
107+
108+
for (const node of json['@graph']) {
109+
rebuildBlankNodeIds(node, blankNodeMap)
110+
}
111+
112+
if (json['@graph'].length === 1) {
113+
return json['@graph'][0]
114+
}
115+
116+
return json
117+
}
118+
119+
export default prettyJsonld

scripts/lib/utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import rdf from 'rdf-ext'
2+
3+
async function parseJsonld (jsonld, context = {}) {
4+
try {
5+
rdf._data.blankNodeCounter = 0
6+
7+
return await rdf.io.dataset.fromText('application/ld+json', JSON.stringify({
8+
...context,
9+
...JSON.parse(jsonld)
10+
}))
11+
} catch (err) {
12+
return null
13+
}
14+
}
15+
16+
async function parseTurtle (turtle, prefixes = '') {
17+
try {
18+
rdf._data.blankNodeCounter = 0
19+
20+
return await rdf.io.dataset.fromText('text/turtle', `${prefixes}\n${turtle}`)
21+
} catch (err) {
22+
return null
23+
}
24+
}
25+
26+
export {
27+
parseJsonld,
28+
parseTurtle
29+
}

0 commit comments

Comments
 (0)