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

index ecdd745a47a860b8fc5db609966da7b18e706b47..9cd81375aa93b72c2d456d122c33c58e3e77477e 100644 (file)
@@ -19,11 +19,13 @@ function nb:_ngx_location_md()
        return
 end
 
-function nb:ngx_location_md()
+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()';
 -- }
+       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 template = require "resty.template"
        if self.dev then template.caching(false) end
@@ -33,13 +35,17 @@ function nb:ngx_location_md()
 
        local vars = {}
        -- Extract title from content or filename or dirname
-       --vars.title = string.match(content,'^#[\t ]*([^\r\n]+)')
+       vars.title = string.match(content,'^#[\t ]*([^\r\n]+)')
+
+       -- From filename
 --     if not vars.title then
 --             vars.title = string.match(file,'([^/%.]+)%.') -- basename
 --             if vars.title == 'index' then
 --                     vars.title = string.match(file,'/([^/]+)/[^/]+$')
 --             end
 --     end
+
+       -- From request_uri
        if not vars.title then
                local basename = string.match(ngx.var.request_uri,'([^/]+)$')
                if basename then
@@ -47,9 +53,11 @@ function nb:ngx_location_md()
                        vars.title = noext
                end
        end
+
+       -- Default first tld domain
        if not vars.title then vars.title = string.match(ngx.var.http_host,'^([^%.]+)') end
 
-       local header = nb:readall(ngx.var.document_root .. "/header.html")
+       local header = nb:readall(fheader)
        if header then template.render(header,vars) end
 
        content = template.output(template.compile(content)(vars))
@@ -74,7 +82,7 @@ function nb:ngx_location_md()
 
        end
 
-       local footer = nb:readall(ngx.var.document_root .. "/footer.html")
+       local footer = nb:readall(ffooter)
        if footer then template.render(footer,vars) end
 
        ngx.exit(0)