templates.lua

Sat, 11 Jul 2009 03:37:48 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 11 Jul 2009 03:37:48 +0100
changeset 10
58f3707e44d7
parent 0
b40ca010c49c
permissions
-rwxr-xr-x

Strip trailing punctuation from short titles

0
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 module("templates", package.seeall)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local tpl = {};
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local tpl_mt = { __index = tpl };
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local tplutil = {};
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local tplutil_mt = { __index = tplutil };
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function tpl:saverender(fn, vars)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local f, err = io.open(fn, "w+");
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if not f then return f,err; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 f:write(self:render(vars));
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 f:close();
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return true
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function tpl:render(vars)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local tpl = self.tpl or self.file:read("*a");
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 self.tpl = tpl;
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if not vars then return tpl; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return (tpl:gsub("%b{}", function (cap)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local v = cap:match("^%{%{%s*(.-)%s*%}%}*$")
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if not v then return cap; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local chunk, err = loadstring("return "..v);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if not chunk then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 chunk, err = loadstring(v);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not chunk then error(err); end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 setfenv(chunk, vars);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local ok, ret = pcall(chunk);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if not ok then error(ret); end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 do return ret or ""; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local v = cap:match("^%{%{%s*([%w_%.]+)%s*%}%}*$")
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if v then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local _vars = vars;
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local ret, subs = v:gsub("(%w+)%.", function (var) if _vars then _vars = _vars[var]; end end);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if _vars and _vars ~= vars then return _vars[v:match("%w+$")] or cap; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return vars[v] or cap;
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local v = cap:match("^%{*[%w_]+%(.*%}*$");
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if v then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local chunk, err = loadstring("return "..v:sub(2,-2));
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if not chunk then error("error in template: "..tostring(err)); end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 setfenv(chunk, setmetatable(vars, tplutil_mt));
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local success, ret = pcall(chunk);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if success then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return ret;
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 else
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 error("error in template: "..ret);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end));
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 -- -- -- --
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local function escape(s) return s; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 function tplutil.kvlist(t)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if #t > 0 then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 -- Assume an array
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if type(t[1]) == "table" then
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 -- Assume a table of tables
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 else
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 -- Assume a straight list
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 else
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 -- assume a k->v list
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local b = { "<table>" };
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 function tplutil.ulist(t)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 -- assume a list/array
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local b = { "<ul>\n" };
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 for _, v in ipairs(t) do
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 table.insert(b, " <li>"..escape(v).."</li>\n");
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 table.insert(b, "</ul>\n");
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return table.concat(b);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 function init(file)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local f, err = io.open(file);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if not f then return nil, "Couldn't open template file: "..err; end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 return setmetatable({ file = f }, tpl_mt);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 function load(template)
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 return setmetatable({ tpl = template }, tpl_mt);
b40ca010c49c Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end

mercurial