-
Notifications
You must be signed in to change notification settings - Fork 2
/
plottable-node.js
148 lines (132 loc) · 3.48 KB
/
plottable-node.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
var fs = require('fs');
var minimist = require('minimist');
var phantom = require('phantom');
var path = require('path');
var _dataFile;
var _configFile;
var _outputFile;
var _svgHeight;
var _svgWidth;
var _templateFile = __dirname + '/template.html';
var _readmeFile = __dirname + 'README.md';
var _plottableCSS = __dirname + '/bower_components/plottable/plottable.css';
function createPlotAndExtractSVG() {
phantom.create(function(ph) {
ph.createPage(function(page) {
page.open(_templateFile, function(status) {
if (status !== "success") {
throw new Error("Could not find file " + _templateFile);
}
_prepareSVG(page);
_runPlottable(page);
_extractSVG(page);
ph.exit();
});
});
});
}
function _prepareSVG(page) {
page.evaluate(function(height, width) {
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.id = "svg";
svg.setAttribute("height", height);
svg.setAttribute("width", width);
document.body.appendChild(svg);
}, function() {
}, _svgHeight, _svgWidth);
}
function _runPlottable(page) {
_dataFile && page.injectJs(_dataFile, function(success) {
if (!success) {
throw new Error("Could not find file " + data_file);
}
});
_configFile && page.injectJs(_configFile, function(success) {
if (!success) {
throw new Error("Could not find file " + _configFile);
}
});
}
function _extractSVG(page) {
page.evaluate(function() {
var node = document.getElementById('svg');
node.setAttribute("xmlns", "http://www.w3.org/2000/svg");
var svgData = new XMLSerializer().serializeToString(node);
return svgData;
}, function (svgData) {
createSVGFile(svgData);
});
}
function createSVGFile(svgNodeData) {
var header = '<?xml version="1.0" standalone="no"?>';
generateCSS(function(cssContent){
cssInjectedSvgNodeData = svgNodeData.replace("</svg>", cssContent + "</svg>")
var content = header + cssInjectedSvgNodeData + "\n" ;
fs.writeFile(_outputFile, content, function (err, data) {
if (err) {
throw new Error(err);
}
})
})
}
function _loadFile(file, callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
callback(data);
});
}
function generateCSS(callback) {
_loadFile(_plottableCSS, function(stylesheet) {
var injection = "<defs>"
injection += '<style type="text/css">'
injection += '<![CDATA[';
injection += stylesheet;
injection += ']]>'
injection += '</style>';
injection += '</defs>';
callback(injection);
});
}
function processArguments() {
var args = minimist(process.argv.slice(2), {
boolean: ["help", "version"],
alias: {
d: "data",
o: "output",
h: "height",
w: "width",
}
});
if (args.help) {
showHelp();
}
if (args.version) {
console.log("v" + require('./package.json').version);
process.exit();
}
_dataFile = args.data || null;
_configFile = args._[0] || null;
_outputFile = args.output || "output.svg";
_svgHeight = args.height || 500;
_svgWidth = args.width || 500;
if (_configFile == null) {
showHelp();
}
}
function showHelp() {
var cmd = path.basename(process.argv[1]);
var help = fs
.readFileSync(_readmeFile, 'utf-8')
.match(/```help([^`]*)```/)[1]
.replace(/\$0/g, cmd)
.trim()
console.log(help);
process.exit(1);
}
function start() {
processArguments();
createPlotAndExtractSVG();
}
start();