handlers/render.lua

changeset 7
038275cd92ed
parent 4
4cb0d1dbc65b
child 15
82d254b8f01e
equal deleted inserted replaced
6:219d7a7304f8 7:038275cd92ed
1 1
2 module("handlers.render", package.seeall) 2 --@module handlers.render
3
4 local handlers_render={}
3 5
4 local ltp=require('ltp.template') 6 local ltp=require('ltp.template')
5 require('markdown') 7 local markdown=require('markdown')
6 require('config') 8 local config=require('config')
7 require('path') 9 local path=require('path')
8 require('filecache') 10 local filecache=require('filecache')
9 require('log') 11 local log=require('log')
12 local dependency=require('dependency')
10 13
11 -- 14 --
12 -- Phase 1: load & markup 15 -- Phase 1: load & markup
13 -- 16 --
14 17
15 function phase1(file, env) 18 function handlers_render.phase1(file, env)
16 local f = io.open(path.join(env.paths.src, file), 'r') 19 local f = io.open(path.join(env.paths.src, file), 'r')
17 local data = nil 20 local data = nil
18 local in_meta = false 21 local in_meta = false
19 local linenum=1 22 local linenum=1
20 local meta = {} 23 local meta = {}
61 } 64 }
62 65
63 env.pages[file]=page 66 env.pages[file]=page
64 end 67 end
65 68
66 function process_lua(template, env) 69 function handlers_render.process_lua(template, env)
67 env=table.join(env, {env=env}) -- TODO: should use __index 70 env=table.join(env, {env=env}) -- TODO: should use __index
68 --return ltp.render(nil, 1, template, env, {}, "<%", "%>", {}) 71 --return ltp.render(nil, 1, template, env, {}, "<%", "%>", {})
69 return ltp.render_template(template, "<%", "%>", 72 return ltp.render_template(template, "<%", "%>",
70 ltp.merge_index(env, _G)) 73 ltp.merge_index(env, _G))
71 end 74 end
72 75
73 function env_for(file, env, path_prefix) 76 function handlers_render.env_for(file, env, path_prefix)
74 local newenv=table.copy(env) 77 local newenv=table.copy(env)
75 78
76 newenv.base_url=path.to_root(file) 79 newenv.base_url=path.to_root(file)
77 newenv.path_prefix=(path_prefix or "") 80 newenv.path_prefix=(path_prefix or "")
78 newenv.page=env.pages[file] 81 newenv.page=env.pages[file]
79 82
80 return newenv 83 return newenv
81 end 84 end
82 85
83 function render(file, env, path_prefix) 86 function handlers_render.render(file, env, path_prefix)
84 local data=env.pages[file].data 87 local data=env.pages[file].data
85 if data then 88 if data then
86 local newenv=env_for(file, env, path_prefix) 89 local newenv=handlers_render.env_for(file, env, path_prefix)
87 local data2=process_lua(data, newenv) 90 local data2=handlers_render.process_lua(data, newenv)
88 return markdown(data2) 91 return markdown(data2)
89 end 92 end
90 end 93 end
91 94
92 function phase2(file, env) 95 function handlers_render.phase2(file, env)
93 local page=env.pages[file] 96 local page=env.pages[file]
94 local src = path.join(env.paths.src, file) 97 local src = path.join(env.paths.src, file)
95 local dst = path.join(env.paths.dst, page.destination) 98 local dst = path.join(env.paths.dst, page.destination)
96 local tmpl = path.join(env.paths.tmpl, 99 local tmpl = path.join(env.paths.tmpl,
97 page.meta.template or "page.template") 100 page.meta.template or "page.template")
110 build=dependency.simple_update_check(dst, deps) 113 build=dependency.simple_update_check(dst, deps)
111 end 114 end
112 115
113 if build then 116 if build then
114 log.log("Render "..file.."\n") 117 log.log("Render "..file.."\n")
115 local content=render(file, env) 118 local content=handlers_render.render(file, env)
116 local page_template=filecache.get(tmpl) 119 local page_template=filecache.get(tmpl)
117 120
118 local newenv=table.join({content=content}, env_for(file, env)) 121 local newenv=table.join({content=content}, handlers_render.env_for(file, env))
119 local data2=process_lua(page_template, newenv) 122 local data2=handlers_render.process_lua(page_template, newenv)
120 123
121 log.log("Write "..page.destination.."\n") 124 log.log("Write "..page.destination.."\n")
122 local f=io.openX(dst, "w") 125 local f=io.openX(dst, "w")
123 f:write(data2) 126 f:write(data2)
124 end 127 end
125 end 128 end
126 129
130 return handlers_render

mercurial