local nb = {
dev = 1,
}
+local discount = nil
if ngx and ngx.var.PRODUCTION == 'yes' then nb.dev = 0 end
-- 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
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(" +(</" .. tag .. ">)","%1")
- content = content:gsub("(<" .. tag .. ">) +","%1")
- end
+ content = self:md2html(content)
-- ngx.print(content)
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)
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*<p>(.*)</p>%s*$','%1')
- inc = '<div class="' .. file:match('^.*/([^/%.%?&]+)') .. '">' .. inc .. '</div>'
+ inc_content = self:md2html(inc_content)
+ inc_content = inc_content:gsub('^%s*<p>%s*',''):gsub('%s*</p>%s*$','')
+ -- class = inc:gsub('/index%.[%w]+$',''):match('^.*/([^/%.%?&]+)')
+ -- id = inc:gsub('/index%.[%w]+$','')
+ local name = self:filepath2name(inc)
+ inc_content = '<div'
+ .. ' id="inc-' .. name .. '"'
+ .. ' class="' .. name .. '"'
+ .. '>' .. inc_content .. '</div>'
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)
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(" +(</" .. tag .. ">)","%1")
+ content = content:gsub("(<" .. tag .. ">) +","%1")
+ end
+
+ content = content:gsub('<p><div','<div')
+ content = content:gsub('div><p>','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