src/templates.lua

Thu, 22 Jun 2023 21:25:35 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Jun 2023 21:25:35 +0100
changeset 11
635b385df3a2
parent 0
6279a7d40ae7
permissions
-rw-r--r--

templates: Support for templates that extend other templates

0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local lfs = require "lfs";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local function readfile(filename)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 fh:close();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 return data;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function _M.init(config)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local templates = {};
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local template_path = template_base_path..(config.templates or "html");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return templates;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return _M;

mercurial