]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 01:14:10 +0000 (02:14 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 01:14:10 +0000 (02:14 +0100)
lib/lua/nb.lua

index 7ad9b0ead7f458c81060baa142e10c52ebd9590d..e81846ba6775b7af58b6994168b2b31787c6a3ca 100644 (file)
@@ -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*<p>%s*',''):gsub('%s*</p>%s*$','')
-               -- class = inc:gsub('/index%.[%w]+$',''):match('^.*/([^/%.%?&]+)')
-               -- id = inc:gsub('/index%.[%w]+$','')
-               local name = self:filepath2name(inc)
-               if false then
-                       inc_content = '<div'
-                               .. ' id="inc-' .. name .. '"'
-                               .. ' class="' .. name .. '"'
-                       .. '>' .. inc_content .. '</div>'
-               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*<p>%s*',''):gsub('%s*</p>%s*$','')
+                       -- class = inc:gsub('/index%.[%w]+$',''):match('^.*/([^/%.%?&]+)')
+                       -- id = inc:gsub('/index%.[%w]+$','')
+                       local name = self:filepath2name(inc)
+                       if false then
+                               inc_content = '<div'
+                                       .. ' id="inc-' .. name .. '"'
+                                       .. ' class="' .. name .. '"'
+                               .. '>' .. inc_content .. '</div>'
+                       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('<tr>')
+                               for k,v in pairs(r) do
+                                       ngx.print('<th class="' .. k .. '">' .. k .. '</th>')
+                               end
+                               ngx.print('</tr>')
+                       end
+                       ngx.print('<tr>')
+                       for k,v in pairs(r) do
+                               ngx.print('<td>' .. v .. '</td>')
+                       end
+                       ngx.print('<tr>\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('<table>\n') end
+       for i,r in pairs(rows) do
+               fct(r,i)
+       end
+       if fmt == "table" then ngx.print('</table>\n') end
+end
+
 return nb