]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 21 May 2018 23:01:32 +0000 (00:01 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 21 May 2018 23:01:32 +0000 (00:01 +0100)
lib/lua/nb.lua
www/dbq/dbq.php

index 2e3662e4ab889e5cf5ae66c2968e0e6e04a97d75..184cf32a306123de7a373c198326c58f40d9ee76 100644 (file)
@@ -5,6 +5,7 @@
 local nb = {
        dev = 1,
 } 
+local discount = nil
 
 if ngx and ngx.var.PRODUCTION == 'yes' then nb.dev = 0 end
 
@@ -34,13 +35,15 @@ function nb:ngx_location_tmpl(fheader,ffooter)
 --   content_by_lua_block { package.path = ngx.var.NB_ROOT .. "/lib/lua/?.lua;" .. package.path local nb = require("nb") nb:ngx_location_tmpl() }
 -- }
        local root = ngx.var.document_root
+       local file = root .. ngx.var.uri;
+       local path = file:gsub('/[^/]+$','')
+
        if not fheader then fheader = root .. "/header.html" end
        if not ffooter then ffooter = root .. "/footer.html" end
 
        local template = require "resty.template"
        if self.dev then template.caching(false) end
 
-       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,24 +78,16 @@ function nb:ngx_location_tmpl(fheader,ffooter)
 
        local ishtml = true
 
-       local discount = require("discount")
        if false then
                template.markdown = require "resty.hoedown"
                -- ngx.say('zaza' .. template.markdown.html.flags.table)
-               --content = discount(content)
+               --content = self:md2html(content)
                content = '{(header.html)}{*markdown([[' .. content .. ']], { smartypants = false, table = true })*}{(footer.html)}'
-               -- content = discount(content)
+               -- content = self:md2html(content)
                ishtml = false
        elseif string.match(file,'%.md$') then
                -- Markdown
-               content = discount(content)
-
-               -- Fix markdown module
-               local tags = { 'th', 'td' }
-               for tag in self:values(tags) do
-                       content = content:gsub(" +(</" .. tag .. ">)","%1")
-                       content = content:gsub("(<" .. tag .. ">) +","%1")
-               end
+               content = self:md2html(content)
 
                -- ngx.print(content)
 
@@ -102,6 +97,7 @@ function nb:ngx_location_tmpl(fheader,ffooter)
                ishtml = false
        end
 
+       -- Header and footer
        if ishtml and file ~= fheader and file ~= ffooter then
                local header = self:readall(fheader)
                local footer = self:readall(ffooter)
@@ -109,22 +105,30 @@ function nb:ngx_location_tmpl(fheader,ffooter)
                ngx.header["Content-type"] = "text/html"
        end
 
+       -- Markdown includes
        local ptn = '%{%(([^%(%)]+%.md)%)%}'
        while true do
          local p1,p2,inc = content:find(ptn)
          if p1 == nil then break end
-         inc = self:readall(root .. '/' .. inc)
+         local inc_content = self:readall(root .. '/' .. inc)
          if ishtml then
-               inc = discount(inc)
-               inc = inc:gsub('^%s*<p>(.*)</p>%s*$','%1')
-               inc = '<div class="' .. file:match('^.*/([^/%.%?&]+)') .. '">' .. inc .. '</div>'
+               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)
+               inc_content = '<div'
+                       .. ' id="inc-' .. name .. '"'
+                       .. ' class="' .. name .. '"'
+               .. '>' .. inc_content .. '</div>'
          end
          content = ''
                .. content:sub(0,p1-1)
-               .. inc
+               .. inc_content
                .. (content:sub(p2+1))
        end
 
+       content = nb:html_fix(content)
        template.render(content,vars)
 
        ngx.exit(0)
@@ -167,4 +171,32 @@ function nb:script_path()
        return str:match("(.*)/")
 end
 
+function nb:filepath2name(inc)
+       local name = inc
+       name = name:gsub('%.[^/]+$','')
+       name = name:gsub('/index$','')
+       name = name:gsub('[^%w]+','-')
+       return name
+end
+
+function nb:html_fix(content)
+
+       -- Fix markdown module
+       local tags = { 'th', 'td' }
+       for tag in self:values(tags) do
+               content = content:gsub(" +(</" .. tag .. ">)","%1")
+               content = content:gsub("(<" .. tag .. ">) +","%1")
+       end
+
+       content = content:gsub('<p><div','<div')
+       content = content:gsub('div><p>','div>')
+       return content
+end
+
+function nb:md2html(content)
+       if self.discount == nil then self.discount = require("discount") end
+       content = self.discount(content)
+       return content
+end
+
 return nb
index 20b52a7cc1fd6c439b80e2d5ac4c49f37229bbf5..260fa8258d9ba1a9786f56121f2983bee04c6f71 100644 (file)
@@ -85,6 +85,7 @@ class DbQ extends nb {
        ];
        public $ext;
        public $expires;
+       public $base;
 
        # Others
        public $expode_args = '&';
@@ -293,6 +294,8 @@ class DbQ extends nb {
 
                        ;
 
+                       if ($this->base) $this->page->head[] = '<base href="' . $this->base . '" />';
+
                        $this->page->begin();
                } # if (empty($this->_nopage)) {
 
@@ -745,6 +748,7 @@ class DbQ extends nb {
 
                }
 
+               if ($base = $this->client_header('Dbq-Base')) $this->base = $base;
                return $this->params;
        }