From c6eb9a3aafec13c2fde26ececaefc932475640e9 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Tue, 8 May 2018 02:57:56 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index ecdd745a..9cd81375 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -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) -- 2.47.3