From 756db5fc34888415360463793193852357742a8d Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 21 May 2018 00:57:09 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index 7a63aede..353778d6 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -33,14 +33,14 @@ function nb:ngx_location_tmpl(fheader,ffooter) -- location ~ \.(md|html)$ { -- content_by_lua_block { package.path = ngx.var.NB_ROOT .. "/lib/lua/?.lua;" .. package.path local nb = require("nb") nb:ngx_location_tmpl() } -- } - if not fheader then fheader = ngx.var.document_root .. "/header.html" end - if not ffooter then ffooter = ngx.var.document_root .. "/footer.html" end + local root = ngx.var.document_root + if not fheader then fheader = root .. "/header.html" end + if not ffooter then ffooter = root .. "/footer.html" end local template = require "resty.template" - -- template.markdown = require "resty.hoedown" if self.dev then template.caching(false) end - local file = ngx.var.document_root .. ngx.var.uri; + local file = root .. ngx.var.uri; local content = self:readall(file) if ngx.ctx.tmpl_content then content = ngx.ctx.tmpl_content end if not content then return end @@ -75,10 +75,16 @@ function nb:ngx_location_tmpl(fheader,ffooter) local ishtml = true - -- content = '{(header.html)}{*markdown[[' .. content .. ']]*}{footer.html)}' - if string.match(file,'%.md$') then + local discount = require("discount") + if false then + template.markdown = require "resty.hoedown" + -- ngx.say('zaza' .. template.markdown.html.flags.table) + --content = discount(content) + content = '{(header.html)}{*markdown([[' .. content .. ']], { smartypants = false, table = true })*}{(footer.html)}' + -- content = discount(content) + ishtml = false + elseif string.match(file,'%.md$') then -- Markdown - discount = require("discount") content = discount(content) -- Fix markdown module @@ -98,19 +104,31 @@ function nb:ngx_location_tmpl(fheader,ffooter) if ishtml and file ~= fheader and file ~= ffooter then local header = self:readall(fheader) - local footer = nb:readall(ffooter) + local footer = self:readall(ffooter) content = header .. content .. footer ngx.header["Content-type"] = "text/html" end + local ptn = '%{%(([^%(%)]+%.md)%)%}' + while true do + local p1,p2,inc = content:find(ptn) + if p1 == nil then break end + inc = self:readall(root .. '/' .. inc) + if ishtml then inc = discount(inc) end + content = '' + .. content:sub(0,p1-1) + .. inc + .. (content:sub(p2+1)) + end + template.render(content,vars) ngx.exit(0) end function nb:values(t) - local i = 0 - return function() i = i + 1; return t[i] end + local i = 0 + return function() i = i + 1; return t[i] end end -- function nb:ngx_vars() -- local vars = {} @@ -141,8 +159,8 @@ function nb:ngx_cmd(cmd) end function nb:script_path() - local str = debug.getinfo(2, "S").source:sub(2) - return str:match("(.*)/") + local str = debug.getinfo(2, "S").source:sub(2) + return str:match("(.*)/") end return nb -- 2.47.3