-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
208 lines (179 loc) · 6.02 KB
/
index.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
var vfs = require('vinyl-fs');
var fs = require('graceful-fs');
var through2 = require('through2');
var cheerio = require('cheerio');
var juice = require('juice');
var mkdirp = require('mkdirp');
var path = require('path');
var css = require('css');
var _ = require('lodash');
var opt;
var streaming = false;
var defaults = {
src: false,
out: {
svg: false,
style: false
},
extension: 'css',
prefix: '',
classPrefix: '',
styledSelectorPrefix: '',
idHandling: 'none', // 'none', 'class', 'remove', 'prefix'
removeStyleTags: false,
inlineURLStyles: true
};
var cheerioOpts = {
xmlMode: false,
lowerCaseTags: false, // don't change the camelCase tag- and attribute names, since chrome only respects camels!
lowerCaseAttributeNames: false, // s.a.
recognizeCDATA: true,
recognizeSelfClosing: true
};
// File name without extension
function identifier (fullPath) {
return path.basename(fullPath, path.extname(fullPath));
}
function className (filepath) {
return opt.classPrefix + identifier(filepath);
}
function writeFile (name, contents, cb) {
mkdirp(path.dirname(name), function () {
fs.writeFile(name, contents, cb);
});
}
function writeSVG (name, contents, cb) {
var destination = path.join(opt.out.svg, path.basename(name));
writeFile(destination, contents, cb);
}
function writeCSS (svgPath, contents, cb) {
var destination = path.join(opt.out.style, opt.prefix + identifier(svgPath) + '.' + opt.extension);
if (contents) {
writeFile(destination, contents, cb);
} else {
cb();
}
}
function nestCSS (prefix, contents) {
var parsed = css.parse(contents);
_.forEach(parsed.stylesheet.rules, function (rule) {
rule.selectors = _.map(rule.selectors, function (selector) {
return opt.styledSelectorPrefix + prefix + ' ' + selector;
});
});
return css.stringify(parsed);
}
function prefixIdOfElem ($elem, id, file, replacedIds) {
var prefixedId = identifier(file.path) + '-' + id;
$elem.attr('id', prefixedId);
replacedIds[id] = prefixedId;
}
function handleIDs (file) {
if (opt.idHandling === 'none') return;
var fileContent = file.contents.toString();
var referencedIds = fileContent.match(/url\(('|")*#.+('|")*\)/g) || [];
var linkedIds = fileContent.match(/xlink\:href\=('|")*#.+('|")/g) || [];
var replacedIds = {};
var editedFileContent;
var $ = cheerio.load(file.contents, cheerioOpts);
var regEx;
referencedIds.forEach(function (elem, idx, arr) {
elem = elem.replace(/url\(('|")*#/g, '');
arr[idx] = elem.replace(/('|")*\)/g, '');
});
linkedIds.forEach(function (elem, idx, arr) {
elem = elem.replace(/xlink\:href\=('|")*#/g, '');
arr[idx] = elem.replace(/('|")+/g, '');
referencedIds.push(arr[idx]);
});
$('[id]').each(function (index, item) {
var $item = $(item);
var id = $item.attr('id');
switch (opt.idHandling) {
case 'class' :
// do not remove ids that are targeted from styles via "url(#id)"
if (referencedIds.indexOf(id) >= 0) {
prefixIdOfElem($item, id, file, replacedIds);
}
else{
$item.addClass(id);
$item.removeAttr('id');
}
break;
case 'remove' :
$item.removeAttr('id');
break;
case 'prefix' :
prefixIdOfElem($item, id, file, replacedIds);
break;
}
});
editedFileContent = $.html();
for(var oldId in replacedIds) {
regEx = new RegExp('#' + oldId, 'g');
editedFileContent = editedFileContent.replace(regEx, '#' + replacedIds[oldId]);
}
if (referencedIds.length >= 0 && opt.inlineURLStyles) {
// inline styles referencing ids
var regExForSelectorsWithUrls = /(.)*\{((\s)*(\w)*(\s)*:(\s)*url\(('|")*#.+('|")*\)(\s)*;)*(\s)*\}/g;
var selectorsWithUrls = editedFileContent.match(regExForSelectorsWithUrls);
editedFileContent = editedFileContent.replace(regExForSelectorsWithUrls, '');
if (selectorsWithUrls) {
selectorsWithUrls.forEach(function (elem, idx) {
console.log('inline styles', selectorsWithUrls[idx]);
editedFileContent = juice.inlineContent(editedFileContent, selectorsWithUrls[idx], {xmlMode: true});
});
}
}
file.contents = new Buffer(editedFileContent);
}
function extractStyle (file) {
var $ = cheerio.load(file.contents, cheerioOpts);
var styleBlocks = $('style');
return styleBlocks.text();
}
function classedSVG (file, styles, removeStyleTags) {
var $ = cheerio.load(file.contents, cheerioOpts),
styleTags = $('style');
$('svg').addClass(className(file.path));
removeStyleTags ? styleTags.remove() : styleTags.text(styles);
return $.html();
}
function extractStyles (file, enc, cb) {
var finished = _.after(2, cb);
handleIDs(file);
var styleText = nestCSS('.' + className(file.path), extractStyle(file));
if (opt.out.svg) {
writeSVG(file.path, new Buffer(classedSVG(file, styleText, opt.removeStyleTags)), finished);
} else {
file.contents = new Buffer(classedSVG(file, styleText, opt.removeStyleTags));
finished();
}
if(opt.out.style) {
writeCSS(file.path, (styleText ? new Buffer(styleText) : null), finished);
} else {
finished();
}
this.push(file);
}
function prepareOptions (options) {
return _.assign(defaults, options);
}
var stream = {
extract: function (options) {
streaming = true;
opt = prepareOptions(options);
opt.out.style = options.styleDest;
return through2.obj(extractStyles);
}
};
var run = function (options, done) {
opt = prepareOptions(options);
vfs.src(opt.src)
.pipe(through2.obj(extractStyles, done));
};
module.exports = {
extract: run,
stream: stream
};