Wed, 31 Jan 2018 19:51:10 +0000
added markdown: true/false meta-tag
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) |
15
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
91 | meta=env.pages[file].meta |
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
92 | if meta.markdown == nil or meta.markdown == "true" then |
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
93 | return markdown(data2) |
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
94 | else |
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
95 | return data2 |
82d254b8f01e
added markdown: true/false meta-tag
Tuomo Valkonen <tuomov@iki.fi>
parents:
7
diff
changeset
|
96 | end |
2 | 97 | end |
98 | end | |
99 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
100 | function handlers_render.phase2(file, env) |
3 | 101 | local page=env.pages[file] |
102 | local src = path.join(env.paths.src, file) | |
103 | local dst = path.join(env.paths.dst, page.destination) | |
104 | local tmpl = path.join(env.paths.tmpl, | |
105 | page.meta.template or "page.template") | |
106 | local deps = {src} | |
107 | ||
108 | local build=page.meta.always_build | |
109 | if not build then | |
110 | if page.meta.dependencies then | |
111 | for p, _ in pairs(env.pages) do | |
4 | 112 | if string.match(p, page.meta.dependencies) then |
3 | 113 | table.insert(deps, path.join(env.paths.src, p)) |
114 | end | |
115 | end | |
116 | end | |
117 | table.insert(deps, tmpl) | |
118 | build=dependency.simple_update_check(dst, deps) | |
2 | 119 | end |
120 | ||
3 | 121 | if build then |
122 | log.log("Render "..file.."\n") | |
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
123 | local content=handlers_render.render(file, env) |
3 | 124 | local page_template=filecache.get(tmpl) |
125 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
126 | 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
|
127 | local data2=handlers_render.process_lua(page_template, newenv) |
3 | 128 | |
129 | log.log("Write "..page.destination.."\n") | |
130 | local f=io.openX(dst, "w") | |
131 | f:write(data2) | |
132 | end | |
2 | 133 | end |
134 | ||
7
038275cd92ed
Convert module stuff to lua 5.3
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
135 | return handlers_render |