Permalink
Browse files

Update engine to node v0.1.30-pre. Also use Do library some.

  • Loading branch information...
creationix committed Feb 21, 2010
1 parent 47fcea1 commit f113b8a3e1f9564183f11b3816bfa0f952d9716b
Showing with 149 additions and 190 deletions.
  1. +147 −183 build.js
  2. +2 −2 builder.js
  3. +0 −5 vendor/less.js
View
330 build.js
@@ -1,24 +1,24 @@
// Load some libraries
-require.paths.unshift(__dirname);
-var Haml = require('./vendor/haml'),
- Markdown = require('./vendor/markdown'),
- md5 = require('./vendor/md5').md5,
- fs = require('fs');
+require.paths.unshift(__dirname + "/vendor");
+var Haml = require('haml'),
+ Markdown = require('markdown'),
+ md5 = require('md5').md5,
+ Do = require('do'),
+ fs = Do.convert(require('fs'), ['readdir', 'stat', 'readFile', 'writeFile']),
+ sys = Do.convert(require('sys'), ['exec']);
var ARTICLE_DIR = __dirname + '/data/articles',
AUTHOR_DIR = __dirname + '/data/authors',
SKIN_DIR = __dirname + '/data/skin',
PUBLIC_DIR = __dirname + '/public';
-var articles, authors;
-
var Filters = {
// Extends markdown by allowing properties at the top.
markdown: function (markdown) {
var match;
var props = {};
- while(match = markdown.match(/^([a-z]+):\s*(.*)\s*\n/i)) {
+ while(match = markdown.match(/^([a-z]+):\s*(.*)\s*$/im)) {
var name = match[1].toLowerCase(),
value = match[2];
markdown = markdown.substr(match[0].length);
@@ -39,75 +39,35 @@ var Filters = {
// Catchall
method_missing: function (format, text) {
+ // return text;
return {
format: format,
content: text
}
}
}
-// Processes a folder of data.
-function process_folder(folder, filter, next) {
- var items = {};
- fs.readdir(folder).addCallback(function (names) {
- var left = 0;
- if (names.length === 0) {
- next(items);
- }
- names.forEach(function (filename) {
- var match = filename.match(filter),
- format, name;
- if (!match) {
- return;
- }
- name = match[1];
- if (match[3]) {
- format = match[3];
- name = name + "." + match[2]
- } else {
- format = match[2];
- }
- left++;
- fs.readFile(folder + "/" + filename).addCallback(function (text) {
- if (Filters[format]) {
- items[name] = Filters[format](text);
- items[name].name = name;
- } else {
- items[name] = Filters.method_missing(format, text);
- }
- left--;
- if (left <= 0) {
- next(items);
- }
- });
- });
- });
-}
-
-function main(folders, next) {
- var left = 0,
- data = {};
- folders.forEach(function (triple) {
- left++;
- process_folder(triple[1], triple[2], function(items) {
- data[triple[0]] = items;
- left--;
- if (left <= 0) {
- next(data);
- }
- });
- });
-}
-
-// Simple loop over objects.
-function loop(obj, callback) {
- var key, value;
- for (key in obj) {
- if (obj.hasOwnProperty(key)) {
- callback(key, obj[key]);
- }
- }
-}
+// Load all files in a directory and pre-process them based on extension.
+function process_folder(path) { return function (callback, errback) {
+ fs.readdir(path)(function (filepaths) {
+ filepaths = filepaths.map(function (filepath) { return path + "/" + filepath; });
+ Do.filter_map(filepaths, function (filepath, callback, errback) {
+ fs.stat(filepath)(function (stat) {
+ if (!stat.isFile()) { callback(); return ; }
+ fs.readFile(filepath)(function (data) {
+ callback([filepath, data]);
+ }, errback);
+ }, errback);
+ })(function (pairs) {
+ callback(pairs.reduce(function (obj, el) {
+ var ext = el[0].match(/[^.]*$/)[0];
+ var val = Filters[ext] ? Filters[ext](el[1]) : Filters.method_missing(ext, el[1]);
+ obj[el[0].match(/([^./]*)[^\/]*$/)[1]] = val;
+ return obj;
+ }, {}));
+ }, errback);
+ }, errback);
+}}
// combo library. Allows to group several callbacks.
function Combo(callback) {
@@ -133,6 +93,17 @@ Combo.prototype = {
}
};
+
+// Simple loop over objects.
+function loop(obj, callback) {
+ var key, value;
+ for (key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ callback(key, obj[key]);
+ }
+ }
+}
+
// Generic compare helper
function cmp(value1, value2) {
return ((value1 == value2) ? 0 : ((value1 > value2) ? 1 : -1));
@@ -151,129 +122,122 @@ function object_sort(obj, callback) {
return newobj;
}
-function render(data, next) {
- var Helpers, haml;
- var group = new Combo(function() {
- next("Done!\n" + Array.prototype.map.call(arguments, function (args) {
- return args[0] + " - " + args[1];
- }).join("\n"))
- });
+exports.build = function (next, error_handler) {
+ // Load up the data files
+ Do.parallel(
+ process_folder(AUTHOR_DIR),
+ process_folder(ARTICLE_DIR),
+ process_folder(SKIN_DIR)
+ )(function (authors, articles, templates) {
+ var group = new Combo(function() {
+ next("Done!\n" + Array.prototype.map.call(arguments, function (args) {
+ return args[0] + " - " + args[1];
+ }).join("\n"))
+ });
- function write_file(filename, content) {
- var cb = group.add();
- fs.writeFile(filename, content).addCallback(function () {
- cb(new Date(), " Wrote " + content.length + " bytes to " + filename);
- })
- }
+ function write_file(filename, content) {
+ var cb = group.add();
+ fs.writeFile(filename, content)(function () {
+ cb(new Date(), " Wrote " + content.length + " bytes to " + filename);
+ }, error_handler);
+ }
- Helpers = {
- github: function (name) {
- return "http://github.com/" + name;
- },
- gravitar: function (email, size) {
- size = size || 80
- return "http://www.gravatar.com/avatar/" +
- md5(email.trim().toLowerCase()) +
- "?r=pg&s=" + size + ".jpg&d=identicon";
- },
- partial: function (name, props) {
- var locals = Object.create(Helpers);
- process.mixin(locals, props);
- return Haml.execute(data.templates[name], {}, locals);
- },
- format_date: function (date, format) {
- var date = new Date(date),
- match, value;
- while (match = format.match(/(%[a-z])/i)) {
- switch (match[1]) {
- case "%d":
- value = date.getDate();
- break;
- case "%m":
- value = date.getMonth() + 1;
- break;
- case "%Y":
- value = date.getFullYear();
- break;
- case "%H":
- value = date.getHours();
- break;
- case "%M":
- value = date.getMinutes();
- break;
- case "%S":
- value = date.getSeconds();
- break;
- default:
- value = "";
- break;
+ var Helpers = {
+ github: function (name) {
+ return "http://github.com/" + name;
+ },
+ gravitar: function (email, size) {
+ size = size || 80
+ return "http://www.gravatar.com/avatar/" +
+ md5(email.trim().toLowerCase()) +
+ "?r=pg&s=" + size + ".jpg&d=identicon";
+ },
+ partial: function (name, props) {
+ var locals = Object.create(Helpers);
+ process.mixin(locals, props);
+ return Haml.execute(templates[name], {}, locals);
+ },
+ format_date: function (date, format) {
+ var date = new Date(date),
+ match, value;
+ while (match = format.match(/(%[a-z])/i)) {
+ switch (match[1]) {
+ case "%d":
+ value = date.getDate();
+ break;
+ case "%m":
+ value = date.getMonth() + 1;
+ break;
+ case "%Y":
+ value = date.getFullYear();
+ break;
+ case "%H":
+ value = date.getHours();
+ break;
+ case "%M":
+ value = date.getMinutes();
+ break;
+ case "%S":
+ value = date.getSeconds();
+ break;
+ default:
+ value = "";
+ break;
+ }
+ format = format.replace(match[1], value);
}
- format = format.replace(match[1], value);
+ return format;
}
- return format;
- }
- };
- haml = Helpers.partial;
+ };
+ var haml = Helpers.partial;
+
+ // Sort authors by name
+ authors = object_sort(authors, function (key1, value1, key2, value2) {
+ return cmp(key1, key2);
+ });
- // Sort authors by name
- data.authors = object_sort(data.authors, function (key1, value1, key2, value2) {
- return cmp(key1, key2);
- });
+ // Sort articles by date, newest first
+ articles = object_sort(articles, function (key1, value1, key2, value2) {
+ return cmp(Date.parse(value2.date), Date.parse(value1.date));
+ });
+
+ var data = {
+ authors: authors,
+ articles: articles
+ };
- // Sort articles by date, newest first
- data.articles = object_sort(data.articles, function (key1, value1, key2, value2) {
- return cmp(Date.parse(value2.date), Date.parse(value1.date));
- });
+ // Generate a page for each author...
+ loop(authors, function (name, props) {
+ props.link = name.toLowerCase().replace(/ /g, "_");
+ write_file(PUBLIC_DIR + "/" + props.link + ".html", haml("layout", {
+ title: "About " + name,
+ content: haml("author", props)
+ }));
+ });
- // Generate a page for each author...
- loop(data.authors, function (name, props) {
- props.link = name.toLowerCase().replace(/ /g, "_");
- write_file(PUBLIC_DIR + "/" + props.link + ".html", haml("layout", {
- title: "About " + name,
- content: haml("author", props)
- }));
- });
+ // Generate a page for each article...
+ loop(articles, function (name, props) {
+ props.link = name;
+ props.author = authors[props.author];
+ write_file(PUBLIC_DIR + "/" + props.link + ".html", haml("layout", {
+ title: props.title,
+ content: haml("article", props)
+ }));
+ });
- // Generate a page for each article...
- loop(data.articles, function (name, props) {
- props.link = name;
- props.author = data.authors[props.author];
- write_file(PUBLIC_DIR + "/" + props.link + ".html", haml("layout", {
- title: props.title,
- content: haml("article", props)
+ // Generate a index page...
+ write_file(PUBLIC_DIR + "/index.html", haml("layout", {
+ title: "Index",
+ content: haml("index", data)
}));
- });
-
- // Generate a index page...
- write_file(PUBLIC_DIR + "/index.html", haml("layout", {
- title: "Index",
- content: haml("index", data)
- }));
- // Make the RSS Feed...
- write_file(PUBLIC_DIR + "/feed.xml", haml("feed", data).replace(/(&lt;code&gt;)\#\![a-z]+\n/g, "$1"));
-
- // Write the static files as is...
- loop(data.static, function (filename, content) {
- write_file(PUBLIC_DIR + "/" + filename, content.content || content);
+ // Make the RSS Feed...
+ write_file(PUBLIC_DIR + "/feed.xml", haml("feed", data).replace(/(&lt;code&gt;)\#\![a-z]+\n/g, "$1"));
+
+ // Copy the static files...
+ sys.exec("cp -r " + SKIN_DIR + "/public/* " + PUBLIC_DIR + "/")(group.add(), error_handler);
});
-
}
-exports.build = function (next) {
- // Kick off the process
- articles = [];
- authors = {};
- main([
- ["articles", ARTICLE_DIR, /^(.*)\.(markdown)$/],
- ["authors", AUTHOR_DIR, /^(.*)\.(markdown)$/],
- ["templates", SKIN_DIR, /^(.*)\.(haml)$/],
- ["static", SKIN_DIR, /^(.*\.(?:css|js))$/]
- ], function (data) {
- fs.mkdir(PUBLIC_DIR, 0777).addCallback(function () {
- render(data, next);
- }).addErrback(function () {
- render(data, next);
- });
- });
-};
+// exports.build(puts, debug);
+
Oops, something went wrong.

0 comments on commit f113b8a

Please sign in to comment.