From 77c6b87b5da5649587bc7e6b046b5c3329f59244 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 25 May 2018 02:14:10 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 90 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index 7ad9b0ea..e81846ba 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -108,26 +108,26 @@ function nb:ngx_location_tmpl(fheader,ffooter) -- Markdown includes local ptn = '%{%(([^%(%)]+%.md)%)%}' while true do - local p1,p2,inc = content:find(ptn) - if p1 == nil then break end - local inc_content = self:readall(root .. '/' .. inc) - if ishtml then - 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) - if false then - inc_content = '' .. inc_content .. '' - end - end - content = '' - .. content:sub(0,p1-1) - .. inc_content - .. (content:sub(p2+1)) + local p1,p2,inc = content:find(ptn) + if p1 == nil then break end + local inc_content = self:readall(root .. '/' .. inc) + if ishtml then + 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) + if false then + inc_content = '' .. inc_content .. '' + end + end + content = '' + .. content:sub(0,p1-1) + .. inc_content + .. (content:sub(p2+1)) end content = self:html_fix(content) @@ -205,4 +205,54 @@ function nb:md2html(content) return content end +function nb:api(url,fmt) + local res = ngx.location.capture(url) + local rows = json.decode(res.body) + + local fct + + -- Function + if type(fmt) == "function" then + fct = fmt + + -- Table + elseif 0 + or fmt == "table" + or fmt == "table-th" + or fmt == "table-td" + then + fct = function(r,i) + if i == 1 and (fmt == "table" or fmt == "table-th") then + ngx.print('') + for k,v in pairs(r) do + ngx.print('' .. k .. '') + end + ngx.print('') + end + ngx.print('') + for k,v in pairs(r) do + ngx.print('' .. v .. '') + end + ngx.print('\n') + end + + -- Default + else + fct = function(r,i) + for k,v in pairs(r) do + ngx.print(string.format(fmt,v)) + -- ngx.say(v) + end + end + + end + + -- Run + if fmt == "table" then ngx.print('\n') end + for i,r in pairs(rows) do + fct(r,i) + end + if fmt == "table" then ngx.print('
\n') end +end + return nb -- 2.47.3