Changeset 1202 for lang/lua

Show
Ignore:
Timestamp:
11/07/07 12:04:25 (13 months ago)
Author:
cho45
Message:

lang/lua/bluasxom/bluasxom.lua:

だいたい完了

Location:
lang/lua/bluasxom
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/lua/bluasxom

    • Property svn:ignore set to
      data
      bluasxom.cgi
  • lang/lua/bluasxom/bluasxom.lua

    r1201 r1202  
    1212        __tostring = function (self) 
    1313                return string.format( 
    14                         "#<Entry title=%q path=%q time=%d>", 
     14                        "#<Entry title=%q path=%q time=%s>", 
    1515                        self.title, 
    1616                        self.path, 
    17                         self.time 
     17                        os.date("%Y-%m-%d %H:%M:%S", self.time) 
    1818                ) 
    1919        end, 
     
    8282} 
    8383 
     84Bluasxom = Class { super = nil, 
     85        initialize = function (self, config) 
     86                self.config = config 
     87        end, 
    8488 
    85 function get_entries(dir, ext) 
    86         local ret = List.new() 
    87         local tf = os.tmpname() 
    88         -- ruby++ 
    89         os.execute('ruby -rpathname -e "Pathname.glob(%|'..dir..'/**/*'..ext..'|){|f|puts %|#{f.mtime.to_i} #{f}|}" > ' .. tf) 
    90         for line in io.lines(tf) do 
    91                 local time, filename = string.match(line, "(%d+) (.+)") 
    92                 local e = Entry.new(filename, tonumber(time, 10)) 
    93                 e.name = string.gsub(string.gsub(filename, "%.%w+$", ""), "^"..dir, "") 
    94                 ret:push(e) 
    95         end 
    96         return ret 
    97 end 
     89        run = function (self) 
     90                path_info = os.getenv("PATH_INFO") or "/" 
     91                flavour   = string.match(path_info, "(%..+)$") or ".html" 
     92                path_info = string.gsub(path_info, "%..+$", "") 
     93                path_info = string.gsub(path_info, "index$", "") 
    9894 
    99 ELua.run("template.html", { 
    100         title    = "test", 
    101         home     = os.getenv("SCRIPT_NAME"), 
    102         server   = "http://" .. tostring(os.getenv("SERVER_NAME")), 
    103         entries  = 
    104                 get_entries("data", ".txt"):sort(function (a, b) 
     95                local entries = self.get_entries(self.config.datadir, self.config.dataext):sort(function (a, b) 
    10596                        return a.time > b.time 
    106                 end), 
    107         debugObj = "aaa", 
    108 }) 
     97                end) 
    10998 
     99                local year, month, day 
     100                for i, v in ipairs({"^/(%d+)/(%d+)/(%d+)", "^/(%d+)/(%d+)", "^/(%d+)"}) do 
     101                        year, month, day = string.match(path_info, v) 
     102                        if year then break end 
     103                end 
     104                if year then 
     105                        entries = entries:select(function (e) 
     106                                local ret = true 
     107                                for i, v in ipairs({{m = "%Y", v = year}, {m = "%m", v = month}, {m = "%d", v = day}}) do 
     108                                        if v.v and not(os.date(v.m, e.time) == v.v) then 
     109                                                ret = false 
     110                                        end 
     111                                end 
     112                                return ret 
     113                        end) 
     114                else 
     115                        entries = entries:select(function (e) 
     116                                return string.match(e.name, "^"..path_info) 
     117                        end) 
     118                end 
     119 
     120                ELua.run("template"..flavour, { 
     121                        title    = self.config.title, 
     122                        home     = os.getenv("SCRIPT_NAME"), 
     123                        server   = "http://" .. tostring(os.getenv("SERVER_NAME")), 
     124                        entries  = entries, 
     125                        debugObj = "aaa", 
     126                }) 
     127 
     128        end, 
     129 
     130        get_entries = function (dir, ext) 
     131                local ret = List.new() 
     132                local tf = os.tmpname() 
     133                -- ruby++ 
     134                os.execute('ruby -rpathname -e "Pathname.glob(%|'..dir..'/**/*'..ext..'|){|f|puts %|#{f.mtime.to_i} #{f}|}" > ' .. tf) 
     135                for line in io.lines(tf) do 
     136                        local time, filename = string.match(line, "(%d+) (.+)") 
     137                        local e = Entry.new(filename, tonumber(time, 10)) 
     138                        e.name = string.gsub(string.gsub(filename, "%.%w+$", ""), "^"..dir, "") 
     139                        ret:push(e) 
     140                end 
     141                return ret 
     142        end, 
     143} 
     144 
     145Bluasxom.new({ 
     146        title   = "Bluasxom", 
     147        author  = "jitensya", 
     148        dataext = ".txt", 
     149        datadir = "data", 
     150}):run() 
     151