handlers/render.lua

Wed, 17 Jan 2018 18:14:57 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 17 Jan 2018 18:14:57 +0000
changeset 14
bbf5bbd72f48
parent 7
038275cd92ed
child 15
82d254b8f01e
permissions
-rwxr-xr-x

Improved CSS for different display sizes


--@module handlers.render

local handlers_render={}

local ltp=require('ltp.template')
local markdown=require('markdown')
local config=require('config')
local path=require('path')
local filecache=require('filecache')
local log=require('log')
local dependency=require('dependency')

--
-- Phase 1: load & markup
--

function handlers_render.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 handlers_render.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 handlers_render.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 handlers_render.render(file, env, path_prefix)
    local data=env.pages[file].data
    if data then
        local newenv=handlers_render.env_for(file, env, path_prefix)
        local data2=handlers_render.process_lua(data, newenv)
        return markdown(data2)
    end
end

function handlers_render.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=handlers_render.render(file, env)
        local page_template=filecache.get(tmpl)

        local newenv=table.join({content=content}, handlers_render.env_for(file, env))
        local data2=handlers_render.process_lua(page_template, newenv)
        
        log.log("Write "..page.destination.."\n")
        local f=io.openX(dst, "w")
        f:write(data2)
    end
end

return handlers_render

mercurial