1 local lfs = require "lfs"; |
1 local lfs = require "lfs"; |
2 |
2 |
3 local function readfile(filename) |
3 local function readfile(filename) |
4 local fh = assert(io.open(filename)); |
4 local fh = assert(io.open(filename)); |
5 local data = fh:read("*a"); |
5 local data, err = fh:read("*a"); |
|
6 if not data then |
|
7 return error(filename..": "..err); |
|
8 end |
6 fh:close(); |
9 fh:close(); |
7 return data; |
10 return data; |
8 end |
11 end |
9 |
12 |
|
13 local template_base_path = (os.getenv("TEMPLATE_PATH") or ".").."/"; |
|
14 |
|
15 local function read_template_file(template_path, filename) |
|
16 local template = readfile(template_path.."/"..filename); |
|
17 if template:sub(1,9) == "#EXTENDS:" then |
|
18 local parent_template, pragma_end = template:match("^#EXTENDS: *([^\r\n]+)\r?\n()"); |
|
19 template = template:sub(pragma_end); |
|
20 local base_template = read_template_file(template_path, parent_template); |
|
21 template = base_template:gsub("{SUBTEMPLATE}", ((template):gsub("%%", "%%%%"))); |
|
22 end |
|
23 return template; |
|
24 end |
|
25 |
10 local _M = {}; |
26 local _M = {}; |
11 |
|
12 local template_base_path = (os.getenv("TEMPLATE_PATH") or ".").."/"; |
|
13 |
27 |
14 function _M.init(config) |
28 function _M.init(config) |
15 local templates = {}; |
29 local templates = {}; |
16 |
30 |
17 local template_path = template_base_path..(config.templates or "html"); |
31 local template_path = template_base_path..(config.templates or "html"); |
18 |
|
19 for filename in lfs.dir(template_path) do |
32 for filename in lfs.dir(template_path) do |
20 local template_name = filename; |
33 if filename:sub(1,1) ~= "." then |
21 if filename:match("%.html$") then |
34 local template_name = filename; |
22 template_name = filename:gsub("%.html$", ""); |
35 if filename:match("%.html$") then |
|
36 template_name = filename:gsub("%.html$", ""); |
|
37 end |
|
38 templates[template_name] = read_template_file(template_path, filename); |
23 end |
39 end |
24 templates[template_name] = readfile(template_path.."/"..filename); |
|
25 end |
40 end |
26 |
41 |
27 return templates; |
42 return templates; |
28 end |
43 end |
29 |
44 |