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