-
Notifications
You must be signed in to change notification settings - Fork 25
/
render.js
115 lines (105 loc) · 3.8 KB
/
render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const fs = require('fs');
const sttl = require('sttl');
const urdf = require('urdf');
const toRDf = require('./toRdf.js').context;
const tpl = fs.readFileSync('ontology/templates.rq', 'utf-8');
sttl.register(tpl);
sttl.connect(async (query) => {
const graph = await urdf.query(query);
const bindings = graph.reduce((accumulator, value) => {
// Flat bindings that have list variables
// Note: this method only works for one list variable per binding
const found = Object.keys(value).find((k) => value[k].type === 'list');
if (found) {
value[found].value.forEach((listElement) => {
const newBinding = Object.assign({}, value);
newBinding[found] = listElement;
accumulator.push(newBinding);
});
} else {
accumulator.push(value);
}
return accumulator;
}, []);
return { results: { bindings } };
});
const ontologies = [
'bindings/protocols/coap/ontology.ttl',
'bindings/protocols/mqtt/ontology.ttl',
'bindings/protocols/modbus/ontology.ttl',
];
console.log("Rendering ontology documentation...");
const promiseChain = ontologies.reduce(async (previousPromise, ontologyFile) => {
const templateURI =
'http://w3c.github.io/wot-binding-templates/ontology#main';
await previousPromise;
await render(
ontologyFile,
templateURI,
ontologyFile.replace('.ttl', '.html')
);
await urdf.clear();
}, Promise.resolve());
console.log("Rendering WoT binding documentation...");
const mappings = [
{
ontologyFile: 'bindings/protocols/modbus/mapping.ttl',
baseOntologyFile: 'bindings/protocols/modbus/ontology.ttl',
templateURI:
'http://w3c.github.io/wot-binding-templates/mappings#modbus',
contextFile: 'bindings/protocols/modbus/context.jsonld',
},
{
ontologyFile: 'bindings/protocols/mqtt/mapping.ttl',
baseOntologyFile: 'bindings/protocols/mqtt/ontology.ttl',
templateURI: 'http://w3c.github.io/wot-binding-templates/mappings#mqtt',
contextFile: 'bindings/protocols/mqtt/context.jsonld',
},
{
ontologyFile: 'bindings/protocols/coap/mapping.ttl',
baseOntologyFile: 'bindings/protocols/coap/ontology.ttl',
templateURI: 'http://w3c.github.io/wot-binding-templates/mappings#coap',
contextFile: 'bindings/protocols/coap/context.jsonld',
},
];
const modbusTemplate = fs.readFileSync(
'bindings/protocols/modbus/template.sparql',
'utf-8'
);
const mqttTemplate = fs.readFileSync(
'bindings/protocols/mqtt/template.sparql',
'utf-8'
);
const coapTemplate = fs.readFileSync(
'bindings/protocols/coap/template.sparql',
'utf-8'
);
sttl.register(modbusTemplate);
sttl.register(mqttTemplate);
sttl.register(coapTemplate);
mappings.reduce(async (previousPromise, mapping) => {
const { ontologyFile, baseOntologyFile, templateURI, contextFile } = mapping;
const base = fs.readFileSync(baseOntologyFile, 'UTF-8');
await previousPromise;
await urdf.load(base, { format: 'text/turtle' });
const jsonld = fs.readFileSync(contextFile, 'utf8');
const result = toRDf(JSON.parse(jsonld));
await urdf.load(result);
await render(
ontologyFile,
templateURI,
ontologyFile.replace('mapping.ttl', 'index.html')
);
await urdf.clear();
}, promiseChain);
async function render(ontologyFile, templateURI, output) {
const ttl = fs.readFileSync(ontologyFile, 'UTF-8');
try {
await urdf.load(ttl, { format: 'text/turtle' });
const html = await sttl.callTemplate(templateURI, ttl);
fs.writeFileSync(output, html);
console.log('File ', output, 'generated');
} catch (e) {
console.error('Error while rendering ' + ontologyFile + ': ' + e);
}
}