-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
svg2js.js
executable file
·169 lines (153 loc) · 4.51 KB
/
svg2js.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const onml = require('onml');
const json5 = require('json5');
const argv = require('yargs').argv;
const styles = {};
const marker1 = ['marker',
{
id: 'arrowhead',
style: 'fill:#0041c4',
markerHeight: 7,
markerWidth: 10,
markerUnits: 'strokeWidth',
viewBox: '0 -4 11 8',
refX: 15,
refY: 0,
orient: 'auto'
},
['path', {'d':'M0 -4 11 0 0 4z'}]
];
const marker2 = ['marker',
{
id: 'arrowtail',
style: 'fill:#0041c4',
markerHeight: 7,
markerWidth: 10,
markerUnits: 'strokeWidth',
viewBox: '-11 -4 11 8',
refX: -15,
refY: 0,
orient: 'auto'
},
['path', {'d':'M0 -4 -11 0 0 4z'}]
];
const marker3 = ['marker',
{
id: 'tee',
style: 'fill:#0041c4',
markerHeight: 6,
markerWidth: 1,
markerUnits: 'strokeWidth',
viewBox: '0 0 1 6',
refX: 0,
refY: 3,
orient: 'auto'
},
['path', {'d':'M 0 0 L 0 6', 'style': 'stroke:#0041c4;stroke-width:2'}]
];
const defs = ['defs'];
const style = ['style', {type: 'text/css'}];
const getDefStyle = () => `
text{
font-size: 11pt;
font-style: normal;
font-variant: normal;
font-weight: normal;
font-stretch: normal;
text-align: center;
fill-opacity: 1;
font-family: Helvetica
}
.h1 { font-size: 33pt; font-weight: bold }
.h2 { font-size: 27pt; font-weight: bold }
.h3 { font-size: 20pt; font-weight: bold }
.h4 { font-size: 14pt; font-weight: bold }
.h5 { font-size: 11pt; font-weight: bold }
.h6 { font-size: 8pt; font-weight: bold }
`.replace(/\s+/g, '');
const res = ['svg',
{
id: 'svg',
xmlns: 'http://www.w3.org/2000/svg',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
height: '0'
},
style,
defs,
['g', {id: 'waves'}, ['g', {id: 'lanes'}], ['g', {id: 'groups'}]]
];
function getFill (fillClasses, node) {
const m = node.attr.style.match(/fill:(#[0-9a-fA-F]+);/);
if (m) {
const fill = m[1];
const texts = node.full[2][2].split(':');
const attr = texts[0];
const klass = texts[1];
if (attr === 'fill') {
fillClasses[klass] = fill;
console.error(attr, klass, fill);
}
}
}
function f2o (name, cb) {
const full = path.resolve(process.cwd(), name);
fs.readFile(full, { encoding: 'utf8'}, function (err, dat) {
if (err) { throw err; }
const ml = onml.parse(dat);
const fillClasses = {
'.muted': '#aaa',
'.warning': '#f6b900',
'.error': '#f60000',
'.info': '#0041c4',
'.success': '#00ab00'
};
onml.traverse(ml, {
leave: function (node) {
switch(node.name) {
case 'g':
delete node.attr.transform;
defs.push(node.full);
break;
case 'rect':
delete node.attr.id;
break;
case 'text':
getFill(fillClasses, node);
break;
case 'path':
delete node.attr.id;
if (styles[node.attr.style] === undefined) {
styles[node.attr.style] = 's' + (Object.keys(styles).length + 1);
}
node.attr['class'] = styles[node.attr.style];
delete node.attr.style;
delete node.attr['sodipodi:nodetypes'];
delete node.attr['inkscape:connector-curvature'];
break;
}
}
});
defs.push(marker1);
defs.push(marker2);
defs.push(marker3);
const fills = Object.keys(fillClasses).map(key =>
key + '{fill:' + fillClasses[key] + '}');
const extra = Object.keys(styles).map(key =>
'.' + styles[key] + '{' + key + '}');
style.push(getDefStyle() + fills.join('') + extra.join(''));
// cb('module.exports = ' + jsof.stringify(res) + ';');
cb(
'var WaveSkin=WaveSkin||{};WaveSkin.' +
path.basename(name, '.svg') + '=' +
json5.stringify(res) +
';\ntry { module.exports = WaveSkin; } catch(err) {}\n'
);
});
}
if (typeof argv.i === 'string') {
f2o(argv.i, console.log);
}
/* eslint no-console: 0 */