-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathheckle.js
180 lines (165 loc) · 5.73 KB
/
heckle.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
#!/usr/bin/env node
var path = require("path");
var fs = require("fs");
var rmrf = require("rimraf");
var yaml = require("js-yaml");
var Mold = require("mold-template");
var util = require("./util");
/**
* @param contents
* A string containing the file contents.
* @return
* An object with the properties `frontMatter` and `mainText`,
* representing the file front matter and a string containing the
* remainder of the file, respectively. `frontMatter` will be null if
* the file contains no front matter block, otherwise the block's YAML
* properties will be available as JS properties.
*/
function readContents(contents) {
if (/^---\n/.test(contents)) {
var end = contents.search(/\n---\n/);
if (end != -1)
return {
frontMatter: yaml.load(contents.slice(4, end + 1)) || {},
mainText: contents.slice(end + 5)
};
}
return {frontMatter: null, mainText: contents};
}
let renderMarkdown = null;
function getRenderMarkdown(config) {
return require(config.markdownRenderer ? path.resolve(process.cwd(), config.markdownRenderer) : "./markdownRenderer");
}
function readPosts(config) {
var postsDir = "_posts/";
var posts = [];
if (!fs.existsSync(postsDir)) return posts;
fs.readdirSync(postsDir).forEach(function(file) {
var d = file.match(/^(\d{4})-(\d\d?)-(\d\d?)-(.+)\.(md|markdown|link|html)$/);
if (!d) return;
var contents = readContents(fs.readFileSync(postsDir + file, "utf8"));
var post = contents.frontMatter || {};
post.date = new Date(d[1], d[2] - 1, d[3]);
post.name = d[4];
if (!post.tags) post.tags = [];
if (!post.tags.forEach && post.tags.split) post.tags = post.tags.split(/\s+/);
var extension = d[5];
if (extension == "link") {
var escd = Mold.prototype.escapeHTML(post.url);
post.content = "<p>Read this post at <a href=\"" + escd + "\">" + escd + "</a>.</p>";
post.isLink = true;
} else {
post.content = (extension == "md" || extension == "markdown") ?
renderMarkdown(contents.mainText) :
contents.mainText;
post.url = getURL(config, post);
}
posts.push(post);
});
posts.sort(function(a, b){return b.date - a.date;});
return posts;
}
function gatherTags(posts) {
var tags = {};
posts.forEach(function(post) {
if (post.tags) post.tags.forEach(function(tag) {
(tags.hasOwnProperty(tag) ? tags[tag] : (tags[tag] = [])).push(post);
});
else post.tags = [];
});
return tags;
}
var defaults = {
postLink: "${name}.html",
postFileName: "${url}"
};
function readConfig() {
var config = (util.exists("_config.yml") && yaml.load(fs.readFileSync("_config.yml", "utf8"))) || {};
for (var opt in defaults) if (defaults.hasOwnProperty(opt) && !config.hasOwnProperty(opt))
config[opt] = defaults[opt];
return config;
}
function fillTemplate(tpl, vars) {
for (var prop in vars) tpl = tpl.replace("${" + prop + "}", vars[prop]);
return tpl;
}
function getURL(config, post) {
return fillTemplate(config.postLink, post);
}
function ensureDirectories(path) {
var parts = path.split("/"), cur = "";
for (var i = 0; i < parts.length - 1; ++i) {
cur += parts[i] + "/";
if (!util.exists(cur, true)) fs.mkdirSync(cur);
}
}
function prepareMold(ctx) {
var mold = new Mold(ctx)
if (util.exists("_includes/", true))
fs.readdirSync("_includes/").forEach(function(file) {
mold.bake(file.match(/^(.*?)\.[^\.]+$/)[1], fs.readFileSync("_includes/" + file, "utf8"));
});
return mold
}
var layouts = {};
function getLayout(name, mold) {
if (name.indexOf(".") == -1) name = name + ".html";
if (layouts.hasOwnProperty(name)) return layouts[name];
var tmpl = function layout(doc) {
var text = layout.template(doc);
if (layout.parent) {
var wrapper = Object.create(doc, {content: {value: text}});
text = getLayout(layout.parent, mold)(wrapper);
}
return text;
};
tmpl.filename = name;
var contents = readContents(fs.readFileSync("_layouts/" + name, "utf8"));
tmpl.template = mold.bake(name, contents.mainText);
tmpl.parent = contents.frontMatter && "layout" in contents.frontMatter ?
contents.frontMatter.layout : null;
return layouts[name] = tmpl;
}
function generate(mold) {
var config = readConfig();
renderMarkdown = getRenderMarkdown(config);
var posts = readPosts(config);
var ctx = {site: {posts: posts, tags: gatherTags(posts), config: config},
dateFormat: require("dateformat")};
var mold = prepareMold(ctx);
if (util.exists("_site", true)) rmrf.sync("_site");
posts.forEach(function(post) {
if (post.isLink) return;
var path = "_site/" + fillTemplate(config.postFileName, post);
ensureDirectories(path);
fs.writeFileSync(path, getLayout(post.layout || "post.html", mold)(post), "utf8");
});
function walkDir(dir) {
fs.readdirSync(dir).forEach(function(fname) {
if (/^[_\.]/.test(fname)) return;
var file = dir + fname;
if (fs.statSync(file).isDirectory()) {
walkDir(file + "/");
} else {
var out = "_site/" + file;
ensureDirectories(out);
var contents = readContents(fs.readFileSync(file, "utf8"));
if (contents.frontMatter) {
var doc = contents.frontMatter;
var layout = getLayout(doc.layout || "default.html", mold);
doc.content = /\.(md|markdown)$/.test(fname) ?
renderMarkdown(contents.mainText) :
contents.mainText;
doc.name = fname.match(/^(.*?)\.[^\.]+$/)[1];
doc.url = file;
out = out.replace(/\.(md|markdown)$/, layout.filename.match(/(\.\w+|)$/)[1]);
fs.writeFileSync(out, layout(doc), "utf8");
} else {
util.copyFileSync(file, out);
}
}
});
}
walkDir("./");
}
generate();