#!llv8call require("uneval.js") v8ext.loadBinary("org.coderepos.fs"); v8ext.loadBinary("org.coderepos.env"); File = org.coderepos.fs.File; ENV = org.coderepos.env.ENV; function p (msg) { print(msg); } require("ejs.js"); BlosxomV8 = function (config) { this.initialize(config) }; BlosxomV8.prototype = { initialize : function (config) { this.config = config; }, run : function () { var entries = this.getEntries(); entries.sort(function (a, b) { return b.datetime.valueOf() - a.datetime.valueOf(); }); (this.getenv("PATH_INFO") || "/").match(/(.+?)(\.[^.]+)?$/); var path_info = RegExp.$1, flavour = RegExp.$2 || this.config.default_flavour; if (path_info.match(RegExp("^/(\\d{4})(?:/(\\d\\d)(?:/(\\d\\d))?)?"))) { var year = +RegExp.$1, month = RegExp.$2 - 1, day = +RegExp.$3; entries = entries.filter(function (i) { return [ {k:"getFullYear",v:year}, {k:"getMonth", v:month}, {k:"getDate",v:day} ].every(function (j) { return (j.v <= 0) || i.datetime[j.k]() == j.v }); }); } else { try { entries = entries.filter(function (i) { if (i.name == path_info) throw ["only-match", i]; return RegExp("^"+path_info).test(i.name); }); } catch (e) { if (e[0] != "only-match") throw e; // only match entries = [e[1]]; } } var template = new EJS(this._readLines("template"+flavour).join("\n"), {useWith:true}); print(template.run({ title : this.config.title, author : this.config.author, home : this.getenv("SCRIPT_NAME") || '/', path : (this.getenv("SCRIPT_NAME") || '/').split("/").slice(-1)[0], server_root : "http://" + this.getenv("SERVER_NAME"), entries : entries, }) + "\n"); }, getEntries : function () { function _getFiles (path) { var ret = [], dir = new Dir(path); while (f = dir.read()) { if (f.match(/\.txt$/)) ret.push(path + '/' + f); } dir.close(); return ret; } var self = this; var files = _getFiles(self.config.data_dir); var entries = files.map(function (i) { var content = self._readLines(i); return { file : i, name : String(i).replace(RegExp("^"+self.config.data_dir+"|\\..*$", "g"), ""), datetime : File.getFileInfo(i).mtime, title : String(content.shift()), body : content.join("\n"), }; }); return entries; }, _readLines : function (file) { var data = '', str, f = new File(file, "rb"); while (str = f.read(1024)) data += str; return data.split(/\r?\n/g); }, getenv : function (name) { return ENV[name] || ""; }, }; new BlosxomV8({ title : "Blosxom.V8!", data_dir : "data", default_flavour : ".html", }).run();