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