forked from Tradenomiliitto/tradenomiitti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg-to-elm
executable file
·58 lines (44 loc) · 1.29 KB
/
svg-to-elm
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
#!/usr/bin/env node
// NOTE: This is not suitable for use as is, really check the output and massage the SVG files to fit if need be
const xml2js = require('xml2js');
const fs = require('fs');
if (process.argv.length !== 3) {
console.error('Give svg file name as parameter');
process.exit(1);
}
const svgFileName = process.argv[2];
const contentsAsString = fs.readFileSync(svgFileName);
xml2js.parseString(contentsAsString, (err, res) => {
if (err) {
console.error(err);
process.exit(2);
}
const svg = res.svg;
const viewBox = svg.$.viewBox;
const g = svg.g[0];
const gAttributes = !g.$ ? [] : Object.keys(g.$).map(key => {
return `SvgA.${key} "${g.$[key]}"`;
});
const childElements = [].concat.apply([], Object.keys(g).filter(key => key !== '$').map(key => {
return g[key].map(child => {
const childAttributes = !child.$ ? [] : Object.keys(child.$).map(propKey => {
return `SvgA.${propKey} "${child.$[propKey].replace(/\s*/g, '')}"`;
});
return `Svg.${key}
[ ${childAttributes.join('\n , ')}
]
[]`;
})
}));
console.log(`
Svg.svg
[ SvgA.viewBox "${viewBox}"
]
[ Svg.g
[ ${gAttributes.join('\n , ')}
]
[ ${childElements.join('\n , ')}
]
]
`);
});