forked from w3c/wot-thing-description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
239 lines (205 loc) · 7.82 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
let fs = require('fs');
let sttl = require('sttl');
const http = require('http');
const urlm = require('url');
let jsonld = require('./context/json-ld.js');
//let tidyHTML = require('./tidy.js');
/**
* params:
* ep - SPARQL Update endpoint
* ttl - Vocabulary raw content (Turtle format);
* if null, the whole dataset is deleted
*
* returns a Promise (no resolve param)
*/
function load(ep, ttl) {
return new Promise((resolve, reject) => {
let url = urlm.parse(ep);
let req = http.request({
protocol: url.protocol,
hostname: url.hostname,
port: url.port,
path: url.pathname,
method: 'POST',
headers: { 'Content-Type': 'application/sparql-update' }
}, res => {
if (res.statusCode >= 400) {
let e = new Error('Endpoint returned ' + res.statusCode);
reject(e);
} else {
resolve();
}
});
req.on('error', e => reject(e));
if (ttl) {
// Turtle -> SPARQL Update syntax
let prefixRegex = /@(prefix\s+\w*\s*:\s*<.*>)\s*\.\s*\n/g;
let prefixes = '';
while ((m = prefixRegex.exec(ttl))) prefixes += m[1] + '\n';
let triples = ttl.replace(prefixRegex, '');
req.end(prefixes + ' insert data { ' + triples + ' }');
} else {
req.end('drop all;');
}
});
}
const ctxFiles = [
'context/td-context-1.1.jsonld'
// 'context/td-context.jsonld',
// 'context/json-schema-context.jsonld',
// 'context/wot-security-context.jsonld',
// 'context/web-linking-context.jsonld'
];
const ttlFiles = [
'ontology/td.ttl',
'ontology/json-schema.ttl',
'ontology/wot-security.ttl',
'ontology/web-linking.ttl',
'ontology/alignments.ttl',
'validation/td-validation.ttl'
];
const txtFiles = [
'templates.txt',
'visualization/templates.txt',
'ontology/templates.txt'
];
const src = fs.readFileSync('index.template.html', 'UTF-8');
const jsonSchemaValidation = fs.readFileSync('validation/td-json-schema-validation.json', 'UTF-8');
const atriskCSS = fs.readFileSync('testing/atrisk.css', 'UTF-8');
const updateEndpoint = process.env.WOT_SPARUL_ENDPOINT;
const queryEndpoint = process.env.WOT_SPARQL_ENDPOINT;
if (!updateEndpoint) throw new Error('WOT_SPARUL_ENDPOINT not defined (SPARQL update endpoint)');
if (!queryEndpoint) throw new Error('WOT_SPARQL_ENDPOINT not defined (SPARQL query endpoint)');
///////////////////////////////////////////////////////////// 1. clear endpoint
load(updateEndpoint, null)
///////////////////////////////////// 2. generate 1.1 context file with nesting
.then(() => {
let td = JSON.parse(fs.readFileSync('context/td-context.jsonld'));
let jsonschema = JSON.parse(fs.readFileSync('context/json-schema-context.jsonld'));
let wotsec = JSON.parse(fs.readFileSync('context/wot-security-context.jsonld'));
let lnk = JSON.parse(fs.readFileSync('context/web-linking-context.jsonld'));
let ctx = td['@context'];
ctx['@version'] = 1.1;
ctx.properties['@context'] = jsonschema['@context'];
ctx.input['@context'] = jsonschema['@context'];
ctx.output['@context'] = jsonschema['@context'];
ctx.data['@context'] = jsonschema['@context'];
ctx.subscription['@context'] = jsonschema['@context'];
ctx.cancellation['@context'] = jsonschema['@context'];
ctx.security['@context'] = wotsec['@context'];
ctx.links['@context'] = lnk['@context'];
fs.writeFileSync('context/td-context-1.1.jsonld', JSON.stringify(td));
return Promise.resolve();
})
//////////////////////////// 2. generate RDF for context and load all RDF files
.then(() => {
let promises = ttlFiles.map((f) => {
let ttl = fs.readFileSync(f, 'utf-8');
return load(updateEndpoint, ttl);
}).concat(ctxFiles.map(f => {
const context = fs.readFileSync(f, 'UTF-8');
let ttl = jsonld.toRDF(JSON.parse(context));
return load(updateEndpoint, ttl);
}));
return Promise.all(promises);
})
//////////////////////////////////////////////////// 3. register STTL templates
.then(() => {
sttl.clear();
txtFiles.forEach((f) => {
let txt = fs.readFileSync(f, 'UTF-8');
let templates = txt.split('---');
templates.forEach(t => sttl.register(t));
})
return Promise.resolve();
})
//////////////////////////// 4. for each namespace, call HTML and DOT templates
.then(() => {
sttl.connect(queryEndpoint);
let rendered = src;
let td = { type: 'uri', value: 'https://www.w3.org/2019/td#' };
let jsonschema = { type: 'uri', value: 'https://www.w3.org/2019/json-schema#' };
let wotsec = { type: 'uri', value: 'https://www.w3.org/2019/wot-security#' };
let lnk = { type: 'uri', value: 'https://www.w3.org/2019/web-linking#' };
// HTML rendering
let tpl1 = 'http://w3c.github.io/wot-thing-description/#classes';
sttl.callTemplate(tpl1, td)
.then(html => {
rendered = rendered.replace('{td}', html);
return sttl.callTemplate(tpl1, jsonschema);
})
.then((html) => {
rendered = rendered.replace('{json-schema}', html);
return sttl.callTemplate(tpl1, wotsec);
})
.then(html => {
rendered = rendered.replace('{wot-security}', html);
return sttl.callTemplate(tpl1, lnk);
})
.then(html => {
rendered = rendered.replace('{web-linking}', html);
return Promise.resolve();
})
.then(() => {
rendered = rendered.replace('{td.json-schema.validation}', jsonSchemaValidation);
rendered = rendered.replace('{atriskCSS}', atriskCSS);
// beautify html
/*var html = rendered.querySelector("body").outerHTML;
var result = tidy_html5(html, options);
tidyHTML.tidy_html5(html, options);
*/
fs.writeFileSync('index.html', rendered, 'UTF-8');
})
.catch(e => console.error('HTML rendering error: ' + e.message));
// DOT rendering
tpl2 = 'http://w3c.github.io/wot-thing-description/visualization#main';
sttl.callTemplate(tpl2, td)
.then(dot => {
fs.writeFileSync('visualization/td.dot', dot);
return sttl.callTemplate(tpl2, jsonschema);
})
.then(dot => {
fs.writeFileSync('visualization/json-schema.dot', dot);
return sttl.callTemplate(tpl2, wotsec);
})
.then(dot => {
fs.writeFileSync('visualization/wot-security.dot', dot);
return sttl.callTemplate(tpl2, lnk);
})
.then(dot => {
fs.writeFileSync('visualization/web-linking.dot', dot);
})
.catch(e => console.error('DOT rendering error: ' + e.message));
// HTML rendering (ontology documents)
let tdPrefix = { type: 'literal', value: 'td' };
let jsonschemaPrefix = { type: 'literal', value: 'jsonschema' };
let wotsecPrefix = { type: 'literal', value: 'wotsec' };
let lnkPrefix = { type: 'literal', value: 'lnk' };
let process = (ns, html) => {
let tpl = 'ontology/' + ns + '.template.html';
let f = 'ontology/' + ns + '.html';
let doc = fs.readFileSync(tpl, 'utf-8').replace('{axioms}', html);
fs.writeFileSync(f, doc);
};
tpl3 = 'http://w3c.github.io/wot-thing-description/ontology#main';
sttl.callTemplate(tpl3, td, tdPrefix)
.then(html => {
process('td', html);
return sttl.callTemplate(tpl3, jsonschema, jsonschemaPrefix);
})
.then(html => {
process('jsonschema', html);
return sttl.callTemplate(tpl3, wotsec, wotsecPrefix);
})
.then(html => {
process('wotsec', html);
return sttl.callTemplate(tpl3, lnk, lnkPrefix);
})
.then(html => {
process('lnk', html);
})
.catch(e => console.error('HTML (ontology) rendering error: ' + e.message));
})
.catch((e) => {
console.error('Initialization error: ' + e.message);
});