-
Notifications
You must be signed in to change notification settings - Fork 2
/
Viz.js
51 lines (41 loc) · 996 Bytes
/
Viz.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
// Viz.js
"use strict";
const Viz = require("viz.js");
const FileUtil = require("./FileUtil");
const svgexport = require("svgexport");
class MyViz {
constructor() {
this.source = "";
this.svg = "";
}
parse(source) {
this.source = source;
this.svg = Viz(this.source, { format: "svg", engine: "dot" });
return this.svg;
}
toPNG(filepath, callback) {
if (this.svg) {
const svgFile = this.save(filepath);
const filename = filepath.replace(/\.dot$/, ".png");
svgexport.render([{
input: [svgFile],
output: [filename]
}], function(){
callback(filename);
});
}
return "error.";
}
save(filepath, callback) {
if (this.svg) {
const filename = filepath.replace(/\.dot$/, ".svg");
FileUtil.write(filename, this.svg);
if (typeof callback == "function") {
callback(filename);
}
return filename;
}
return "error.";
}
}
module.exports = MyViz;