Changeset 1201 for lang/lua

Show
Ignore:
Timestamp:
11/07/07 04:36:41 (6 years ago)
Author:
cho45
Message:

lang/lua/bluasxom/bluasxom.lua,
lang/lua/bluasxom/template.html:

いろいろシンプルになるように修正。
ねよう

Location:
lang/lua/bluasxom
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/lua/bluasxom/bluasxom.lua

    r1200 r1201  
    22-- vim:ft=lua: 
    33-- POSIX かつ ruby がインストールされている前提 
     4-- Lua 5.1 
    45 
    56package.path = "./lib/?.lua" 
     
    2728} 
    2829 
    29 function get_entries(dir, ext) 
    30         local ret = List.new() 
    31         local tf = os.tmpname() 
    32         os.execute('ruby -rpathname -e "Pathname.glob(%|'..dir..'/**/*'..ext..'|){|f|puts %|#{f.mtime.to_i} #{f}|}" > ' .. tf) 
    33         for line in io.lines(tf) do 
    34                 local time, filename = string.match(line, "(%d+) (.+)") 
    35                 local e = Entry.new(filename, tonumber(time, 10)) 
    36                 e.name = string.gsub(string.gsub(filename, "%.%w+$", ""), "^"..dir, "") 
    37                 ret:push(e) 
    38         end 
    39         return ret 
    40 end 
    41  
    42  
    4330ELua = Class { super = nil, 
    4431        initialize = function (self, template) 
     
    5643                        ret = ret .. pre 
    5744                        if     string.match(code, "^==") then 
    58                                 ret = ret .. "]===] .. " .. string.sub(code, 3) .. " .. [===[" 
     45                                ret = ret .. "]===] .. tostring(" .. string.sub(code, 3) .. ") .. [===[" 
    5946                        elseif string.match(code, "^=") then 
    60                                 ret = ret .. "]===] .. " .. string.sub(code, 2) .. " .. [===[" 
     47                                ret = ret .. "]===] .. htmlescape(" .. string.sub(code, 2) .. ") .. [===[" 
    6148                        else 
    6249                                ret = ret .. "]===]\n" .. code .. "\nret = ret .. [===[" 
     
    6855        end, 
    6956 
     57        htmlescape = function (str) 
     58                local m = {["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;"} 
     59                return string.gsub(tostring(str), "[<>&]", function (s) 
     60                        return m[s] 
     61                end) 
     62        end, 
     63 
    7064        result = function (self, context) 
    7165                local f = loadstring(self.src, "ELua") 
    72                 -- グローバル環境を context コピー 
     66                -- グローバル環境を context コピー 
    7367                -- うわがきされる。 
    7468                for k, v in pairs(_G) do 
    7569                        context[k] = v 
    7670                end 
     71                context.htmlescape = self.htmlescape 
    7772                setfenv(f, context) 
    7873                return f() 
    7974        end, 
     75 
     76        run = function (filename, context) 
     77                local f = io.open(filename) 
     78                local tmpl = f:read("*a") 
     79                f:close() 
     80                print(ELua.new(tmpl):result(context)) 
     81        end, 
    8082} 
    8183 
    82 local f = io.open("template.html") 
    83 tmpl = f:read("*a") 
    84 f:close() 
    8584 
    86 local entries = get_entries("data", ".txt") 
     85function 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 
     97end 
    8798 
    88 entries = entries:sort(function (a, b) 
    89         return a.time > b.time 
    90 end) 
    91  
    92 local result = ELua.new(tmpl):result({ 
    93         title = "test", 
    94         home  = "test", 
    95         entries = entries, 
     99ELua.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) 
     105                        return a.time > b.time 
     106                end), 
    96107        debugObj = "aaa", 
    97         version = _VERSION, 
    98108}) 
    99109 
    100 print(result) 
  • lang/lua/bluasxom/template.html

    r1200 r1201  
    9999 
    100100                        <div id="footer"> 
    101                                 <p>Powered by Lua (+ruby) <%= version %></p> 
     101                                <p>Powered by Lua (+ruby) <%= _VERSION %></p> 
    102102                        </div> 
    103103                </div>