-
Notifications
You must be signed in to change notification settings - Fork 1
/
vg2svg.js
43 lines (36 loc) · 1.29 KB
/
vg2svg.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
'use strict';
const vega = require('vega');
const svgHeader =
'<?xml version="1.0" encoding="utf-8"?>\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';
function read() {
return new Promise((resolve, reject) => {
let text = '';
process.stdin.setEncoding('utf8');
process.stdin.on('error', err => { reject(err); });
process.stdin.on('data', chunk => { text += chunk; });
process.stdin.on('end', () => { resolve(text); });
});
};
function compile(spec) {
try {
const view = new vega.View(vega.parse(spec), {
// loader: vega.loader({baseURL: base}), // load files from base path
// logger: vega.logger(loglevel, 'error'), // route all logging to stderr
renderer: 'none' // no primary renderer needed
}).finalize(); // clear any timers, etc
return view.toSVG().then(
function(_) {
process.stdout.write(svgHeader);
process.stdout.write(_ + '\n');
});
}
catch (err) {
console.error(err)
process.exit(1)
}
}
read()
.then(text => compile(JSON.parse(text)))
.catch(err => console.error(err));