]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 20 May 2018 23:57:09 +0000 (00:57 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sun, 20 May 2018 23:57:09 +0000 (00:57 +0100)
lib/lua/nb.lua

index 7a63aedeaebf8c899f9de0342f2194d6ff1b39bf..353778d69fa3a434da778682ca69f081f72f1c04 100644 (file)
@@ -33,14 +33,14 @@ function nb:ngx_location_tmpl(fheader,ffooter)
 -- location ~ \.(md|html)$ {
 --   content_by_lua_block { package.path = ngx.var.NB_ROOT .. "/lib/lua/?.lua;" .. package.path local nb = require("nb") nb:ngx_location_tmpl() }
 -- }
-       if not fheader then fheader = ngx.var.document_root .. "/header.html" end
-       if not ffooter then ffooter = ngx.var.document_root .. "/footer.html" end
+       local root = ngx.var.document_root
+       if not fheader then fheader = root .. "/header.html" end
+       if not ffooter then ffooter = root .. "/footer.html" end
 
        local template = require "resty.template"
-       -- template.markdown = require "resty.hoedown"
        if self.dev then template.caching(false) end
 
-       local file = ngx.var.document_root .. ngx.var.uri;
+       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
@@ -75,10 +75,16 @@ function nb:ngx_location_tmpl(fheader,ffooter)
 
        local ishtml = true
 
-       -- content = '{(header.html)}{*markdown[[' .. content .. ']]*}{footer.html)}'
-       if string.match(file,'%.md$') then
+       local discount = require("discount")
+       if false then
+               template.markdown = require "resty.hoedown"
+               -- ngx.say('zaza' .. template.markdown.html.flags.table)
+               --content = discount(content)
+               content = '{(header.html)}{*markdown([[' .. content .. ']], { smartypants = false, table = true })*}{(footer.html)}'
+               -- content = discount(content)
+               ishtml = false
+       elseif string.match(file,'%.md$') then
                -- Markdown
-               discount = require("discount")
                content = discount(content)
 
                -- Fix markdown module
@@ -98,19 +104,31 @@ function nb:ngx_location_tmpl(fheader,ffooter)
 
        if ishtml and file ~= fheader and file ~= ffooter then
                local header = self:readall(fheader)
-               local footer = nb:readall(ffooter)
+               local footer = self:readall(ffooter)
                content = header .. content .. footer
                ngx.header["Content-type"] = "text/html"
        end
 
+       local ptn = '%{%(([^%(%)]+%.md)%)%}'
+       while true do
+         local p1,p2,inc = content:find(ptn)
+         if p1 == nil then break end
+         inc = self:readall(root .. '/' .. inc)
+         if ishtml then inc = discount(inc) end
+         content = ''
+               .. content:sub(0,p1-1)
+               .. inc
+               .. (content:sub(p2+1))
+       end
+
        template.render(content,vars)
 
        ngx.exit(0)
 end
 
 function nb:values(t)
-  local i = 0
-  return function() i = i + 1; return t[i] end
+       local i = 0
+       return function() i = i + 1; return t[i] end
 end
 -- function nb:ngx_vars()
 --     local vars = {}
@@ -141,8 +159,8 @@ function nb:ngx_cmd(cmd)
 end
 
 function nb:script_path()
-  local str = debug.getinfo(2, "S").source:sub(2)
-  return str:match("(.*)/")
+       local str = debug.getinfo(2, "S").source:sub(2)
+       return str:match("(.*)/")
 end
 
 return nb