]> git.nbdom.net Git - nb.git/commitdiff
lib/lua/nb.lua
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 16:51:53 +0000 (17:51 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 25 May 2018 16:51:53 +0000 (17:51 +0100)
lib/lua/nb.lua

index 09073592d37dc02280ad0994c4328792b8b65804..5e5fc6d5e0ae2376b0cc186d26596b996ec5dd35 100644 (file)
@@ -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,'<h1[^>]*>(.*?)</h1>') end
+               -- Extract title from h2
+               if not vars.title then vars.title = string.match(content,'<h2[^>]*>(.*?)</h2>') 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)