From 2a847688564a38ad06b7b648fdac12502e491976 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 22 May 2018 00:01:32 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 66 ++++++++++++++++++++++++++++++++++++------------- www/dbq/dbq.php | 4 +++ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index 2e3662e4..184cf32a 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -5,6 +5,7 @@ local nb = { dev = 1, } +local discount = nil if ngx and ngx.var.PRODUCTION == 'yes' then nb.dev = 0 end @@ -34,13 +35,15 @@ function nb:ngx_location_tmpl(fheader,ffooter) -- content_by_lua_block { package.path = ngx.var.NB_ROOT .. "/lib/lua/?.lua;" .. package.path local nb = require("nb") nb:ngx_location_tmpl() } -- } local root = ngx.var.document_root + local file = root .. ngx.var.uri; + local path = file:gsub('/[^/]+$','') + if not fheader then fheader = root .. "/header.html" end if not ffooter then ffooter = root .. "/footer.html" end local template = require "resty.template" if self.dev then template.caching(false) end - 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,24 +78,16 @@ function nb:ngx_location_tmpl(fheader,ffooter) local ishtml = true - local discount = require("discount") if false then template.markdown = require "resty.hoedown" -- ngx.say('zaza' .. template.markdown.html.flags.table) - --content = discount(content) + --content = self:md2html(content) content = '{(header.html)}{*markdown([[' .. content .. ']], { smartypants = false, table = true })*}{(footer.html)}' - -- content = discount(content) + -- content = self:md2html(content) ishtml = false elseif string.match(file,'%.md$') then -- Markdown - content = discount(content) - - -- Fix markdown module - local tags = { 'th', 'td' } - for tag in self:values(tags) do - content = content:gsub(" +()","%1") - content = content:gsub("(<" .. tag .. ">) +","%1") - end + content = self:md2html(content) -- ngx.print(content) @@ -102,6 +97,7 @@ function nb:ngx_location_tmpl(fheader,ffooter) ishtml = false end + -- Header and footer if ishtml and file ~= fheader and file ~= ffooter then local header = self:readall(fheader) local footer = self:readall(ffooter) @@ -109,22 +105,30 @@ function nb:ngx_location_tmpl(fheader,ffooter) ngx.header["Content-type"] = "text/html" end + -- Markdown includes local ptn = '%{%(([^%(%)]+%.md)%)%}' while true do local p1,p2,inc = content:find(ptn) if p1 == nil then break end - inc = self:readall(root .. '/' .. inc) + local inc_content = self:readall(root .. '/' .. inc) if ishtml then - inc = discount(inc) - inc = inc:gsub('^%s*

(.*)

%s*$','%1') - inc = '
' .. inc .. '
' + inc_content = self:md2html(inc_content) + inc_content = inc_content:gsub('^%s*

%s*',''):gsub('%s*

%s*$','') + -- class = inc:gsub('/index%.[%w]+$',''):match('^.*/([^/%.%?&]+)') + -- id = inc:gsub('/index%.[%w]+$','') + local name = self:filepath2name(inc) + inc_content = '' .. inc_content .. '' end content = '' .. content:sub(0,p1-1) - .. inc + .. inc_content .. (content:sub(p2+1)) end + content = nb:html_fix(content) template.render(content,vars) ngx.exit(0) @@ -167,4 +171,32 @@ function nb:script_path() return str:match("(.*)/") end +function nb:filepath2name(inc) + local name = inc + name = name:gsub('%.[^/]+$','') + name = name:gsub('/index$','') + name = name:gsub('[^%w]+','-') + return name +end + +function nb:html_fix(content) + + -- Fix markdown module + local tags = { 'th', 'td' } + for tag in self:values(tags) do + content = content:gsub(" +()","%1") + content = content:gsub("(<" .. tag .. ">) +","%1") + end + + content = content:gsub('

','div>') + return content +end + +function nb:md2html(content) + if self.discount == nil then self.discount = require("discount") end + content = self.discount(content) + return content +end + return nb diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 20b52a7c..260fa825 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -85,6 +85,7 @@ class DbQ extends nb { ]; public $ext; public $expires; + public $base; # Others public $expode_args = '&'; @@ -293,6 +294,8 @@ class DbQ extends nb { ; + if ($this->base) $this->page->head[] = ''; + $this->page->begin(); } # if (empty($this->_nopage)) { @@ -745,6 +748,7 @@ class DbQ extends nb { } + if ($base = $this->client_header('Dbq-Base')) $this->base = $base; return $this->params; } -- 2.47.3