]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 May 2018 18:30:59 +0000 (19:30 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 8 May 2018 18:30:59 +0000 (19:30 +0100)
lib/lua/nb.lua

index da4f721a3199fbe1bc4ccebdc3d23e3fae0227ff..e67abc096075466b6c947641d6b2cf7ecc9ad388 100644 (file)
@@ -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(" +(</th>)","%1")
-               -- end
+
                -- Fix markdown module
-               content = content:gsub(" +(</th>)","%1")
-               content = content:gsub("(<th>) +","%1")
-               content = content:gsub(" +(</td>)","%1")
-               content = content:gsub("(<td>) +","%1")
+               local tags = { 'th', 'td' }
+               for tag in self:values(tags) do
+                       content = content:gsub(" +(</" .. tag .. ">)","%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