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