root/lang/javascript/blosxom.v8/blosxom-v8.cgi @ 38223

Revision 21403, 2.7 kB (checked in by mattn, 5 years ago)

following trunk of llv8call. and added example for .htaccess

Line 
1#!llv8call
2
3require("uneval.js")
4
5v8ext.loadBinary("org.coderepos.fs");
6v8ext.loadBinary("org.coderepos.env");
7File = org.coderepos.fs.File;
8ENV = org.coderepos.env.ENV;
9
10function p (msg) {
11        print(msg);
12}
13
14require("ejs.js");
15
16BlosxomV8 = function (config) { this.initialize(config) };
17BlosxomV8.prototype = {
18        initialize : function (config) {
19                this.config = config;
20        },
21
22        run : function () {
23                var entries = this.getEntries();
24                entries.sort(function (a, b) {
25                        return b.datetime.valueOf() - a.datetime.valueOf();
26                });
27                (this.getenv("PATH_INFO") || "/").match(/(.+?)(\.[^.]+)?$/);
28                var path_info = RegExp.$1, flavour = RegExp.$2 || this.config.default_flavour;
29                if (path_info.match(RegExp("^/(\\d{4})(?:/(\\d\\d)(?:/(\\d\\d))?)?"))) {
30                        var year = +RegExp.$1, month = RegExp.$2 - 1, day = +RegExp.$3;
31                        entries = entries.filter(function (i) {
32                                return [
33                                        {k:"getFullYear",v:year},
34                                        {k:"getMonth", v:month},
35                                        {k:"getDate",v:day}
36                                ].every(function (j) {
37                                        return (j.v <= 0) || i.datetime[j.k]() == j.v
38                                });
39                        });
40                } else {
41                        try {
42                                entries = entries.filter(function (i) {
43                                        if (i.name == path_info) throw ["only-match", i];
44                                        return RegExp("^"+path_info).test(i.name);
45                                });
46                        } catch (e) {
47                                if (e[0] != "only-match") throw e;
48                                // only match
49                                entries = [e[1]];
50                        }
51                }
52
53
54                var template = new EJS(this._readLines("template"+flavour).join("\n"), {useWith:true});
55                print(template.run({
56                        title       : this.config.title,
57                        author      : this.config.author,
58                        home        : this.getenv("SCRIPT_NAME") || '/',
59                        path        : (this.getenv("SCRIPT_NAME") || '/').split("/").slice(-1)[0],
60                        server_root : "http://" + this.getenv("SERVER_NAME"),
61                        entries     : entries,
62                }) + "\n");
63        },
64
65        getEntries : function () {
66                function _getFiles (path) {
67                        var ret = [], dir = new Dir(path);
68                        while (f = dir.read()) {
69                                if (f.match(/\.txt$/))
70                                        ret.push(path + '/' + f);
71                        }
72                        dir.close();
73                        return ret;
74                }
75
76                var self = this;
77                var files = _getFiles(self.config.data_dir);
78                var entries = files.map(function (i) {
79                        var content = self._readLines(i);
80                        return {
81                                file     : i,
82                                name     : String(i).replace(RegExp("^"+self.config.data_dir+"|\\..*$", "g"), ""),
83                                datetime : File.getFileInfo(i).mtime,
84                                title    : String(content.shift()),
85                                body     : content.join("\n"),
86                        };
87                });
88                return entries;
89        },
90
91        _readLines : function (file) {
92                var data = '', str, f = new File(file, "rb");
93                while (str = f.read(1024)) data += str;
94                return data.split(/\r?\n/g);
95        },
96
97        getenv : function (name) {
98                return ENV[name] || "";
99        },
100};
101
102new BlosxomV8({
103        title           : "Blosxom.V8!",
104        data_dir        : "data",
105        default_flavour : ".html",
106}).run();
Note: See TracBrowser for help on using the browser.