-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
executable file
·378 lines (347 loc) · 12.5 KB
/
Gruntfile.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
module.exports = function(grunt) {
'use strict';
var imgJSON = {
"imgsList": {
"rows": []
}
};
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
copy: {
defualt: {
files: [{
expand: true,
flatten: true,
src: ['data/*.{png,jpg,gif}', '!data/replace*.gif'],
dest: 'output/imgs/',
rename: function(dest, src) {
return dest + src.substring(src.indexOf('/')+1).replace(/\s/g, ''); // remove spaces in file name
}
}]
},
BBB: {
files: [{
expand: true,
flatten: true,
src: ['commonImgs/BBB/*.{png,jpg,gif}', 'data/*.{png,jpg,gif}', '!data/replace*.gif', '!data/spacer.gif'],
dest: 'output/imgs/',
rename: function(dest, src) {
return dest + src.substring(src.indexOf('/')+1).replace(/\s/g, ''); // remove spaces in file name
}
}]
}
},
dom_munger: {
defualtBuild: {
options: {
callback: function($) {
function clean(str) {
return str.replace(/^("|("))/gm, '') // remove start quote
.replace(/("|("))$/gm, '') // remove end quote
.replace(/[\u2018\u2019]/gm, "'") // replace all single smart quotes with dumb ones
.replace(/[\u201C\u201D]/gm, '"') // replace all double smart quotes with dumb ones
.replace(/\u00AE/gm, '(R)') // replace (R) symbol with (R)
.replace(/\u00A9/gm, '(c)') // replace (c) symbol with (c)
.replace(/\u2122/gm, '(TM)'); // replace (TM) symbol with (TM)
}
(function makeIndexFile() {
var imgJSON = {
"imgsList": {
"rows": []
}
};
function imgIsSpacer(img) {
if (img.attr('src') === 'spacer.gif') {
return true;
} else {
return false;
}
}
function everyEementInRowIsSpacerImg(tr) {
var bool = true;
tr.children('td').each(function() {
if($(this).find('img') !== []) {
if (!imgIsSpacer($(this))) {
bool = false;
return;
}
} else {
return;
}
});
tr.find('img').each(function() {
if (!imgIsSpacer($(this))) {
bool = false;
return;
}
});
return bool;
} // end everyEementInRowIsSpacerImg(tr)
$('tr').each(function() {
var row = [];
if (!everyEementInRowIsSpacerImg($(this))) {
$(this).children('td').each(function() {
if($(this).find('img').length !== 0) { // it's an image
var img = $(this).find('img');
if (!imgIsSpacer(img)) {
var imgObj = {};
imgObj.width = img.attr('width');
imgObj.height = img.attr('height');
imgObj.src = 'imgs/' + img.attr('src').replace(/\s/g, '');
if (img.attr('src').indexOf('replace') > -1) { // a img being replaced with a spacer
imgObj.src = 'imgs/spacer.gif';
}
if (img.parent('td').attr('bgcolor')) { // cell has a background color
imgObj.bgcolor = img.parent('td').attr('bgcolor');
}
if (img.parent('a')) { // the img has a link
imgObj.link = img.parent().attr('href');
}
if (img.attr('id').indexOf('background') > -1) { // it's meant to be a background img
imgObj.type = 'background';
imgObj.text = clean(img.attr('alt'));
} else {
imgObj.type = 'img';
imgObj.alt = clean(img.attr('alt'));
}
row.push(imgObj);
}
} else { // it's a p tag
var text = $(this).find('img');
var textObj = {};
textObj.type = 'text';
textObj.width = $(this).attr('width');
textObj.height = $(this).attr('height');
textObj.text = clean($(this).text());
if ($(this).attr('bgcolor')) { // cell has a background color
textObj.bgcolor = $(this).attr('bgcolor');
}
row.push(textObj);
}
}); // end for each 'td'
}
if (row.length > 0) imgJSON.imgsList.rows.push(row);
}); // end for each 'tr'
grunt.file.write('build/slices.json', JSON.stringify(imgJSON, null, ' '));
})(); // end makeIndexFile
} // end callback
}, // end options
src: 'data/photoshop.html'
},
defualtText: {
options: {
callback: function($) {
function clean(str) {
return str.replace(/^("|("))/gm, '') // remove start quote
.replace(/("|("))$/gm, '') // remove end quote
.replace(/[\u2018\u2019]/gm, "'") // replace all single smart quotes with dumb ones
.replace(/[\u201C\u201D]/gm, '"') // replace all double smart quotes with dumb ones
.replace(/\u00AE/gm, '(R)') // replace (R) symbol with (R)
.replace(/\u00A9/gm, '(c)') // replace (c) symbol with (c)
.replace(/\u2122/gm, '(TM)'); // replace (TM) symbol with (TM)
}
(function makeTextFile() {
var textFile = '';
$('td').each(function() {
$(this).find('img').each(function() {
if ($(this).attr('alt') !== '') {
textFile += clean($(this).attr('alt')) + '\n';
if ($(this).parent().attr('href') !== undefined) {
textFile += $(this).parent('a').attr('href') + '\n\n';
} else {
textFile += '\n';
}
}
});
}); // end for each 'td'
grunt.file.write('output/plain.txt', textFile.trim());
})(); // end makeTextFile
}
}, // end options
src: 'data/photoshop.html'
},
BBB: {
options: {
callback: function($, file) {
function clean(str) {
return str.replace(/^("|("))/gm, '') // remove start quote
.replace(/("|("))$/gm, '') // remove end quote
.replace(/[\u2018\u2019]/gm, "'") // replace all single smart quotes with dumb ones
.replace(/[\u201C\u201D]/gm, '"') // replace all double smart quotes with dumb ones
.replace(/\u00AE/gm, '(R)') // replace (R) symbol with (R)
.replace(/\u00A9/gm, '(c)') // replace (c) symbol with (c)
.replace(/\u2122/gm, '(TM)') // replace (TM) symbol with (TM)
.replace(/\s{2,}/gm, ' ') // replace all space characters and with 1 space
.trim();
}
function imgIsSpacer(img) {
return (img.attr('src') === 'spacer.gif');
}
function everyEementInRowIsSpacerImg(tr) {
var bool = true;
tr.find('img').each(function() {
if (!imgIsSpacer($(this))) {
bool = false;
return;
}
});
return bool;
}
function islink(str) {
return !(/\s*?(no|NO)\s+(LINK|link)\s*?/.test(str));
}
function createImgObj(img) {
var imgId = img.attr('id'),
imgSrc = img.attr('src'),
imgObj = {
id: imgId,
width: img.attr('width'),
height: img.attr('height'),
src: 'imgs/' + imgSrc.replace(/\s/g, ''),
type: 'img'
};
if (imgSrc.indexOf('replace') > -1) { // img being replaced with a spacer
imgObj.src = 'imgs/spacer.gif';
}
if (img.parent('td').attr('bgcolor')) { // cell has a background color
imgObj.bgcolor = img.parent('td').attr('bgcolor');
}
if (imgId.indexOf('background') > -1) { // it's meant to be a background img
imgObj.type = 'background';
}
return imgObj;
}
(function buildJSON() {
if (file.indexOf('1') > -1) { // process photoshop file
$('tr').each(function() { // for each row
var row = [];
if (!everyEementInRowIsSpacerImg($(this))) { // ignore the row if it's all spacer imgs
$(this).children('td').each(function() { // go through every cell
var img = $(this).find('img')[0];
if(img !== undefined) { // there's an img in the cell
if (!imgIsSpacer($(img))) { // ignore it if it's a spacer
row.push(createImgObj($(img)));
}
} else { // there's live text in the cell
var textObj = {
type: 'text',
width: $(this).attr('width'),
height: (this).attr('height'),
text: clean($(this).text())
};
if ($(this).attr('bgcolor')) { // the cell has a background color
textObj.bgcolor = $(this).attr('bgcolor');
}
row.push(textObj);
}
}); // end for each 'td'
}
if (row.length > 0) imgJSON.imgsList.rows.push(row);
}); // end for each 'tr'
} else { // process excel file
imgJSON.imgsList.rows.forEach(function(row) { // for every photoshop row
row.forEach(function(obj) { // for every obj in that row
if (obj.type !== 'text') {
$('tr').each(function() { // for every row in the excel sheet
var tds = $(this).children(); // cache its children
var objId = obj.id.replace(/_/g, '-'); // the replace is there because photoshop replaces slashes with underscores and that's dumb.
var isAlt = objId.indexOf('--alt');
if (isAlt > -1) {
objId = objId.slice(0, isAlt);
}
if (objId === $(tds[0]).text().trim().toLowerCase()) { // if the id matches the name
if (obj.type === 'background') {
obj.text = clean($(tds[1]).text());
} else if (obj.type === 'img' && isAlt === -1) { // if it's a normal image and if it's not a secondary one
obj.alt = clean($(tds[1]).text());
}
if (islink($(tds[2]).text())) {
obj.link = $(tds[2]).text();
}
}
});
}
});
});
grunt.file.write('build/slices.json', JSON.stringify(imgJSON, null, ' '));
}
})(); // end buildJSON()
(function buildText() {
var textFile = '', banner = '', disclaimer = 'All products are available online and may be available in select Bed Bath & Beyond(R) stores. Products may be ordered in any of our stores; shipping fees apply. Prices only apply in contiguous United States.';
for (var i = 59; i >= 0; i--) banner += '=';
textFile += banner + '\n\n' + 'Bed Bath & Beyond(R)' + '\n\n' + banner + '\n';
imgJSON.imgsList.rows.forEach(function(row) { // for every row in my master list
row.forEach(function(obj) { // for every obj in that row
if (obj.alt) {
if (obj.alt === 'Like Us on Facebook' || obj.alt === disclaimer) {
textFile += '\n' + banner;
}
textFile += '\n' + obj.alt + '\n';
if (obj.alt === 'CONNECT WITH US' || obj.alt === disclaimer) {
textFile += banner + '\n';
}
} else if (obj.text) {
textFile += '\n' + obj.text + '\n';
}
if (obj.link && obj.id.indexOf('__alt') === -1) { // if it has a link and if that link isn't on a supporting object. It's underscores because photoshop is dumb and replaces dashes with underscores
textFile += obj.link + '\n';
}
});
});
grunt.file.write('output/plain.txt', textFile.trim());
})(); // end build text
} // end callback
}, // end options
src: 'data/*.htm*'
}
},
jade: {
defualt: {
options: {
data: function() { return require('./build/slices.json'); }
},
files: [{
expand: true,
cwd: 'jade/',
src: ['*.jade'],
dest: 'build',
ext: '.html'
}]
}
},
inlinecss: {
defualt: {
options: {
removeStyleTags: false
},
files: {
'build/index.inline.html': 'build/index.html'
}
}
},
processhtml: { // to fix the bug with inlinecss where juice deletes the doctype
defualt: {
options: {
data : {
doctype: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
}
},
files: {
'build/index.fixed.html': 'build/index.inline.html'
}
}
},
prettify: {
defualt: {
files: {
'output/index.html': 'build/index.fixed.html'
}
}
}
});
require('load-grunt-tasks')(grunt); // register all tasks
// grunt.registerTask('default', ['copy:defualt', 'dom_munger:defualtBuild', 'dom_munger:defualtText', 'jade', 'inlinecss', 'processhtml', 'prettify']);
grunt.registerTask('default', 'BBB');
grunt.registerTask('BBB', ['copy:BBB', 'dom_munger:BBB', 'jade', 'inlinecss', 'processhtml', 'prettify']);
grunt.registerTask('bbb', 'BBB');
};