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