-
Notifications
You must be signed in to change notification settings - Fork 35
/
compile.js
138 lines (103 loc) · 3.3 KB
/
compile.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
// compile jade, including scripts properly
var fs = require('fs'),
jade = require('jade'),
_ = require('underscore'),
moment = require('moment'),
marked = require('marked'),
hljs = require('highlight.js');
var highlight = function(code, lang) {
if (lang === 'shell') {
lang = 'bash';
}
if (lang === 'html' || lang === 'jade') {
lang = 'http';
}
return hljs.highlight(lang || 'bash', code).value;
};
marked.setOptions({
gfm: true,
tables: true,
pedantic: false,
sanitize: false,
smartLists: true,
highlight: highlight
});
jade.filters.markdown = marked;
['javascript', 'jade', 'css', 'html'].forEach(function (lang) {
jade.filters[lang] = function (code) {
return '<pre><code>' + highlight(code, lang) + '</code></pre>';
};
});
var jadeBase = __dirname + '/jade';
var outBase = __dirname + '/out';
var excludes = ['templates'];
var compile = function (dir) {
var contents = fs.readdirSync(jadeBase + dir);
try {
fs.mkdirSync(outBase + dir);
} catch (e) {}
// jade pages
// ------------------------------------------------
var pages = _.filter(contents, function (file) {
return file.substr(-5) === '.jade';
});
_.each(pages, function (page) {
page = page.substr(0, page.length - 5);
var filename = jadeBase + dir + '/' + page + '.jade';
var template = fs.readFileSync(filename);
var jadeFn = jade.compile(template, {
filename: filename
});
var locals;
try {
locals = JSON.parse(fs.readFileSync(jadeBase + dir + '/' + page + '.json'));
} catch (e) {} //idc lol
if (!locals) {
locals = {};
}
locals.posts = JSON.parse(fs.readFileSync('json/blog.json'));
locals.posts.forEach(function (post) {
post.date = moment(post.date).format('YYYY.MM.DD');
});
//locals.title = filename.substr(0, -5);
var output = jadeFn(locals);
var outFilePath = outBase + dir + '/' + page + '.html';
fs.writeFileSync(outFilePath, output);
console.log('Finished ' + dir + '/' + page + '.html');
});
// markdown pages
// ------------------------------------------------
var down = _.filter(contents, function (file) {
return file.substr(-3) === '.md';
});
_.each(down, function (page) {
page = page.substr(0, page.length - 3);
var template = fs.readFileSync(jadeBase + '/templates/blog.jade');
var jadeFn = jade.compile(template);
var locals;
try {
locals = JSON.parse(fs.readFileSync(jadeBase + dir + '/' + page + '.json'));
} catch (e) {} //idc lol
if (!locals) {
locals = {};
}
locals.body = marked(fs.readFileSync(jadeBase + dir + '/' + page + '.md', 'utf8'));
locals.posts = JSON.parse(fs.readFileSync('json/blog.json'));
locals.posts.forEach(function (post) {
post.date = moment(post.date).format('YYYY.MM.DD');
});
var output = jadeFn(locals);
var outFilePath = outBase + dir + '/' + page + '.html';
fs.writeFileSync(outFilePath, output);
console.log('Finished ' + dir + '/' + page + '.html');
});
// recursively compile each dir
// ------------------------------------------------
var dirs = _.filter(contents, function (file) {
return file.indexOf('.') === -1 && excludes.indexOf(file) === -1;
});
_.each(dirs, function (subdir) {
compile(dir + '/' + subdir);
});
};
compile('');