From 3fe5240e14b3c02c52f5e8ede32305ee0790196e Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 25 May 2018 17:51:53 +0100 Subject: [PATCH] lib/lua/nb.lua --- lib/lua/nb.lua | 59 +++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/lua/nb.lua b/lib/lua/nb.lua index 09073592..5e5fc6d5 100644 --- a/lib/lua/nb.lua +++ b/lib/lua/nb.lua @@ -37,6 +37,7 @@ function nb:ngx_location_tmpl(fheader,ffooter) local root = ngx.var.document_root local file = root .. ngx.var.uri; local path = file:gsub('/[^/]+$','') + local vars = {} if not fheader then fheader = root .. "/header.tmpl.html" end if not ffooter then ffooter = root .. "/footer.tmpl.html" end @@ -52,34 +53,6 @@ function nb:ngx_location_tmpl(fheader,ffooter) if ngx.ctx.tmpl_content then content = ngx.ctx.tmpl_content end if not content then return end - local vars = {} - -- Extract title from h1 - vars.title = string.match(content,'^#[\t ]*([^\r\n]+)') - - if not vars.title then - -- Extract title from h2 - vars.title = string.match(content,'##[\t ]*([^\r\n]+)') - end - -- 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 - local noext = string.match(basename,'([^%.]+)') - vars.title = noext - end - end - - -- Default first tld domain - if not vars.title then vars.title = string.match(ngx.var.http_host,'^([^%.]+)') end - local ishtml = true if false then @@ -93,9 +66,17 @@ function nb:ngx_location_tmpl(fheader,ffooter) -- Markdown content = self:md2html(content) - -- ngx.print(content) + -- Extract title from h1 + if not vars.title then vars.title = string.match(content,'^#[\t ]*([^\r\n]+)') end + -- Extract title from h2 + if not vars.title then vars.title = string.match(content,'##[\t ]*([^\r\n]+)') end + elseif string.match(file,'%.html?$') then + -- Extract title from h1 + if not vars.title then vars.title = string.match(content,']*>(.*?)') end + -- Extract title from h2 + if not vars.title then vars.title = string.match(content,']*>(.*?)') end elseif not ngx.ctx.tmpl_content then ishtml = false @@ -134,6 +115,26 @@ function nb:ngx_location_tmpl(fheader,ffooter) .. (content:sub(p2+1)) end + -- 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 + local noext = string.match(basename,'([^%.]+)') + vars.title = noext + end + end + + -- Default first tld domain + if not vars.title then vars.title = string.match(ngx.var.http_host,'^([^%.]+)') end + content = self:html_fix(content) template.render(content,vars) -- 2.47.3