Sat, 01 Nov 2014 23:34:21 +0000
Lua 5.2 compatibility hack
module("handlers.render", package.seeall) local ltp=require('ltp.template') require('markdown') require('config') require('path') require('filecache') require('log') -- -- Phase 1: load & markup -- function phase1(file, env) local f = io.open(path.join(env.paths.src, file), 'r') local data = nil local in_meta = false local linenum=1 local meta = {} for l in f:lines() do if string.match(l, config.meta_marker) then in_meta = not in_meta elseif in_meta then local key, val = string.match(l, "%s*([^:]*)%s*:%s*(.*)%s*") if key and val then -- very primitive quoting, primarily as a hack to -- not need converting my files that much from Webgen. local val_unq=string.match(val, '^"(.*)"$') meta[key] = val_unq or val else err.file_pos(file, linenum, "meta data syntax error") end else if data then data = data.."\n"..l elseif not string.match(l, "%s") then data=l end end linenum = linenum+1 end log.log("Load "..file.."\n") f:close() local destination if meta.destination then destination=path.join(path.dirname(file), meta.destination) else destination=path.rmext(file) end local page={ data=data, meta=meta, destination=destination, file=file, } env.pages[file]=page end function process_lua(template, env) env=table.join(env, {env=env}) -- TODO: should use __index --return ltp.render(nil, 1, template, env, {}, "<%", "%>", {}) return ltp.render_template(template, "<%", "%>", ltp.merge_index(env, _G)) end function env_for(file, env, path_prefix) local newenv=table.copy(env) newenv.base_url=path.to_root(file) newenv.path_prefix=(path_prefix or "") newenv.page=env.pages[file] return newenv end function render(file, env, path_prefix) local data=env.pages[file].data if data then local newenv=env_for(file, env, path_prefix) local data2=process_lua(data, newenv) return markdown(data2) end end function phase2(file, env) local page=env.pages[file] local src = path.join(env.paths.src, file) local dst = path.join(env.paths.dst, page.destination) local tmpl = path.join(env.paths.tmpl, page.meta.template or "page.template") local deps = {src} local build=page.meta.always_build if not build then if page.meta.dependencies then for p, _ in pairs(env.pages) do if string.match(p, page.meta.dependencies) then table.insert(deps, path.join(env.paths.src, p)) end end end table.insert(deps, tmpl) build=dependency.simple_update_check(dst, deps) end if build then log.log("Render "..file.."\n") local content=render(file, env) local page_template=filecache.get(tmpl) local newenv=table.join({content=content}, env_for(file, env)) local data2=process_lua(page_template, newenv) log.log("Write "..page.destination.."\n") local f=io.openX(dst, "w") f:write(data2) end end