Sun, 07 May 2017 20:02:53 +0100
5.3 updates
2 | 1 | |
2 | module("handlers.render", package.seeall) | |
3 | ||
3 | 4 | local ltp=require('ltp.template') |
2 | 5 | require('markdown') |
6 | require('config') | |
3 | 7 | require('path') |
8 | require('filecache') | |
9 | require('log') | |
2 | 10 | |
11 | -- | |
12 | -- Phase 1: load & markup | |
13 | -- | |
14 | ||
15 | function phase1(file, env) | |
3 | 16 | local f = io.open(path.join(env.paths.src, file), 'r') |
17 | local data = nil | |
2 | 18 | local in_meta = false |
19 | local linenum=1 | |
3 | 20 | local meta = {} |
2 | 21 | |
22 | for l in f:lines() do | |
23 | if string.match(l, config.meta_marker) then | |
24 | in_meta = not in_meta | |
25 | elseif in_meta then | |
26 | local key, val = string.match(l, "%s*([^:]*)%s*:%s*(.*)%s*") | |
27 | if key and val then | |
3 | 28 | -- very primitive quoting, primarily as a hack to |
29 | -- not need converting my files that much from Webgen. | |
30 | local val_unq=string.match(val, '^"(.*)"$') | |
31 | meta[key] = val_unq or val | |
2 | 32 | else |
33 | err.file_pos(file, linenum, "meta data syntax error") | |
34 | end | |
35 | else | |
3 | 36 | if data then |
37 | data = data.."\n"..l | |
38 | elseif not string.match(l, "%s") then | |
39 | data=l | |
40 | end | |
2 | 41 | end |
42 | linenum = linenum+1 | |
43 | end | |
44 | ||
3 | 45 | log.log("Load "..file.."\n") |
46 | ||
2 | 47 | f:close() |
48 | ||
3 | 49 | local destination |
50 | if meta.destination then | |
51 | destination=path.join(path.dirname(file), meta.destination) | |
52 | else | |
53 | destination=path.rmext(file) | |
54 | end | |
55 | ||
56 | local page={ | |
57 | data=data, | |
58 | meta=meta, | |
59 | destination=destination, | |
60 | file=file, | |
61 | } | |
2 | 62 | |
3 | 63 | env.pages[file]=page |
2 | 64 | end |
65 | ||
3 | 66 | function process_lua(template, env) |
67 | env=table.join(env, {env=env}) -- TODO: should use __index | |
68 | --return ltp.render(nil, 1, template, env, {}, "<%", "%>", {}) | |
69 | return ltp.render_template(template, "<%", "%>", | |
70 | ltp.merge_index(env, _G)) | |
2 | 71 | end |
72 | ||
3 | 73 | function env_for(file, env, path_prefix) |
74 | local newenv=table.copy(env) | |
75 | ||
76 | newenv.base_url=path.to_root(file) | |
77 | newenv.path_prefix=(path_prefix or "") | |
78 | newenv.page=env.pages[file] | |
79 | ||
80 | return newenv | |
2 | 81 | end |
82 | ||
3 | 83 | function render(file, env, path_prefix) |
84 | local data=env.pages[file].data | |
85 | if data then | |
86 | local newenv=env_for(file, env, path_prefix) | |
87 | local data2=process_lua(data, newenv) | |
88 | return markdown(data2) | |
2 | 89 | end |
90 | end | |
91 | ||
92 | function phase2(file, env) | |
3 | 93 | local page=env.pages[file] |
94 | local src = path.join(env.paths.src, file) | |
95 | local dst = path.join(env.paths.dst, page.destination) | |
96 | local tmpl = path.join(env.paths.tmpl, | |
97 | page.meta.template or "page.template") | |
98 | local deps = {src} | |
99 | ||
100 | local build=page.meta.always_build | |
101 | if not build then | |
102 | if page.meta.dependencies then | |
103 | for p, _ in pairs(env.pages) do | |
4 | 104 | if string.match(p, page.meta.dependencies) then |
3 | 105 | table.insert(deps, path.join(env.paths.src, p)) |
106 | end | |
107 | end | |
108 | end | |
109 | table.insert(deps, tmpl) | |
110 | build=dependency.simple_update_check(dst, deps) | |
2 | 111 | end |
112 | ||
3 | 113 | if build then |
114 | log.log("Render "..file.."\n") | |
115 | local content=render(file, env) | |
116 | local page_template=filecache.get(tmpl) | |
117 | ||
118 | local newenv=table.join({content=content}, env_for(file, env)) | |
119 | local data2=process_lua(page_template, newenv) | |
120 | ||
121 | log.log("Write "..page.destination.."\n") | |
122 | local f=io.openX(dst, "w") | |
123 | f:write(data2) | |
124 | end | |
2 | 125 | end |
126 |