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