Skip to content

Commit

Permalink
Upgrade to mold 2.0
Browse files Browse the repository at this point in the history
And modernize package.json a little
  • Loading branch information
marijnh committed Mar 15, 2017
1 parent e970886 commit fdfb54e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
30 changes: 16 additions & 14 deletions heckle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function readPosts(config) {
if (!post.tags.forEach && post.tags.split) post.tags = post.tags.split(/\s+/);
var extension = d[5];
if (extension == "link") {
var escd = Mold.escapeHTML(post.url);
var escd = Mold.prototype.escapeHTML(post.url);
post.content = "<p>Read this post at <a href=\"" + escd + "\">" + escd + "</a>.</p>";
post.isLink = true;
} else {
Expand Down Expand Up @@ -101,47 +101,49 @@ function ensureDirectories(path) {
}
}

function prepareIncludes(ctx) {
if (!util.exists("_includes/", true)) return;
fs.readdirSync("_includes/").forEach(function(file) {
Mold.define(file.match(/^(.*?)\.[^\.]+$/)[1],
Mold.bake(fs.readFileSync("_includes/" + file, "utf8"), ctx));
});
function prepareMold(ctx) {
var mold = new Mold(ctx)
if (util.exists("_includes/", true))
fs.readdirSync("_includes/").forEach(function(file) {
var name = file.match(/^(.*?)\.[^\.]+$/)[1]
mold.defs[name] = mold.bake(name, fs.readFileSync("_includes/" + file, "utf8"));

This comment has been minimized.

Copy link
@adrianheine

adrianheine Mar 15, 2017

Contributor

Setting defs explicitly is not necessary, is it?

This comment has been minimized.

Copy link
@marijnh

marijnh Mar 15, 2017

Author Owner

Good point. See e240a88

});
return mold
}

var layouts = {};
function getLayout(name, ctx) {
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, ctx)(wrapper);
text = getLayout(layout.parent, mold)(wrapper);
}
return text;
};
tmpl.filename = name;
var contents = readContents(fs.readFileSync("_layouts/" + name, "utf8"));
tmpl.template = Mold.bake(contents.mainText, ctx);
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() {
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")};
prepareIncludes(ctx);
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", ctx)(post), "utf8");
fs.writeFileSync(path, getLayout(post.layout || "post.html", mold)(post), "utf8");
});
function walkDir(dir) {
fs.readdirSync(dir).forEach(function(fname) {
Expand All @@ -155,7 +157,7 @@ function generate() {
var contents = readContents(fs.readFileSync(file, "utf8"));
if (contents.frontMatter) {
var doc = contents.frontMatter;
var layout = getLayout(doc.layout || "default.html", ctx);
var layout = getLayout(doc.layout || "default.html", mold);
doc.content = /\.(md|markdown)$/.test(fname) ?
renderMarkdown(contents.mainText) :
contents.mainText;
Expand Down
2 changes: 1 addition & 1 deletion highlightCode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {escapeHTML} = require("mold-template")
const escapeHTML = require("mold-template").prototype.escapeHTML
const CodeMirror = require("codemirror/addon/runmode/runmode.node.js")

module.exports = function highlightCode(code, lang) {
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
"dependencies": {"dateformat": "1.x.x",
"markdown-it": "^8.3.1",
"markdown-it-anchor": "^4.0.0",
"rimraf": "2.x.x",
"js-yaml": "1.x.x",
"codemirror": "git+https://github.com/marijnh/CodeMirror.git",
"mold-template": "1.x.x"},
"rimraf": "^2.0.0",
"js-yaml": "^1.0.0",
"codemirror": "^5.24.0",
"mold-template": "2.0.1"},
"bugs": "http://github.com/marijnh/heckle/issues",
"keywords": ["Jekyll", "Blog"],
"homepage": "https://github.com/marijnh/heckle",
"author": "Marijn Haverbeke <marijnh@gmail.com>",
"repositories": [{"type": "git", "url": "https://github.com/marijnh/heckle.git"}]
"repository": {"type": "git", "url": "https://github.com/marijnh/heckle.git"},
"license": "MIT",
"bin": {
"heckle": "heckle.js"

This comment has been minimized.

Copy link
@adrianheine

adrianheine Mar 15, 2017

Contributor

This does not work for me since heckle.js doesn't have a shebang line.

This comment has been minimized.

Copy link
@marijnh

marijnh Mar 15, 2017

Author Owner

Maybe with 0b3f6b7? (Let me know when you have everything working, whatever it is you're doing, and I'll cut a release, by the way)

This comment has been minimized.

Copy link
@adrianheine

adrianheine Mar 15, 2017

Contributor

I'm happy, if you want to you can cut a release :)

This comment has been minimized.

Copy link
@marijnh

marijnh Mar 15, 2017

Author Owner

Released as 0.2.0

}
}

0 comments on commit fdfb54e

Please sign in to comment.