-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate.js
79 lines (68 loc) · 1.9 KB
/
generate.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
/*
* generate.js
* Copyright (C) 2016 dhilipsiva <[email protected]>
*
* Distributed under terms of the MIT license.
*/
(function(){
'use strict';
var sass = require('node-sass'),
pug = require('pug'),
fs = require('fs'),
juice = require('juice'),
cheerio = require('cheerio'),
Inky = require('inky').Inky,
minify = require('html-minifier').minify,
sourceDir = "source/",
distDir = "dist/",
scssDir = sourceDir + "scss/",
scssMain = scssDir + "main.scss",
pugDir = sourceDir + "pug/",
privateDir = pugDir + "private/",
styles, html, inlinedHtml, $, result, css, inky, minifiedHtml, files, file,
i, len, srcPath, targetPath;
// Render SCSS to CSS
styles = sass.renderSync({
file: scssMain,
outputStyle: 'compressed',
});
css = styles.css.toString();
// List files
files = fs.readdirSync(privateDir);
for (i = 0, len = files.length; i < len; i++) {
file = files[i];
if (!file.endsWith(".pug")) {
continue;
}
srcPath = privateDir + file;
targetPath = distDir + file.replace(".pug", ".html");
// Render PUG to HTML
html = pug.renderFile(srcPath);
// Apply Inky on Previous HTML
$ = cheerio.load(html);
inky = new Inky();
html = inky.releaseTheKraken($);
// Apply the Styles Inline
inlinedHtml = juice(html, {extraCss: css});
// Add Additional attributes to HTML
$ = cheerio.load(inlinedHtml);
$('table').attr("border", "0");
$('table').attr("cellpadding", "0");
$('table').attr("cellspacing", "0");
$('a').attr("target", "_blank");
// Minify the File
minifiedHtml = minify($.html(), {
minifyCSS: true,
collapseWhitespace: true
});
// Save the file
fs.writeFile(targetPath, minifiedHtml, function(err) {
if(err) {
console.log(err);
}
else{
console.log("Template generated! ");
}
});
}
})();