From 9526c922fca707de7be2de097e570522ff57d1c0 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 8 May 2018 19:30:59 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index da4f721a..e67abc09 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -1,10 +1,11 @@ -- NB 30.04.18: -- See https://github.com/openresty/lua-nginx-module/blob/master/README.markdown -- ngx.header["Content-type"] = "text/plain" -nb = { +local nb = { dev = 1, } +if ngx.var.PRODUCTION then nb.dev = 0 end function nb:readall(file) -- local f = io.open(file) or error("Could not open file: " .. file) local f = io.open(file) @@ -22,7 +23,7 @@ end function nb:ngx_location_md(fheader,ffooter) -- location ~ \.(md|html)$ { -- default_type text/html; --- content_by_lua 'dofile(ngx.var.NB_ROOT .. "/lib/lua/nb.lua") nb:ngx_location_md()'; +-- content_by_lua 'package.path = ngx.var.NB_ROOT .. "/lib/lua/?.lua;" .. package.path local nb = require("nb") nb:ngx_location_md()'; -- } if not fheader then fheader = ngx.var.document_root .. "/header.html" end if not ffooter then ffooter = ngx.var.document_root .. "/footer.html" end @@ -69,15 +70,14 @@ function nb:ngx_location_md(fheader,ffooter) -- Markdown discount = require("discount") content = discount(content) - local tags = { 'th', 'td' } - -- for tag in self:values(tags) do - -- content = content:gsub(" +()","%1") - -- end + -- Fix markdown module - content = content:gsub(" +()","%1") - content = content:gsub("() +","%1") - content = content:gsub(" +()","%1") - content = content:gsub("() +","%1") + local tags = { 'th', 'td' } + for tag in self:values(tags) do + content = content:gsub(" +()","%1") + content = content:gsub("(<" .. tag .. ">) +","%1") + end + ngx.print(content) elseif string.match(file,'%.html$') then @@ -93,14 +93,7 @@ function nb:ngx_location_md(fheader,ffooter) ngx.exit(0) end -function readcmd(cmd) - local f = assert(io.popen(cmd, 'r')) - local s = assert(f:read('*a')) - f:close() - return s -end - -function values(t) +function nb:values(t) local i = 0 return function() i = i + 1; return t[i] end end @@ -119,3 +112,17 @@ end -- } -- return vars -- end + +function nb:readcmd(cmd) + local f = assert(io.popen(cmd, 'r')) + local s = assert(f:read('*a')) + f:close() + return s +end + +function nb:ngx_cmd(cmd) + ngx.say(self:readcmd(cmd)) + return 0 +end + +return nb -- 2.47.3