plugin/inline.lua

changeset 3
b2df1b3f2c83
child 9
751216807683
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugin/inline.lua	Sun Sep 13 22:22:47 2009 +0300
@@ -0,0 +1,80 @@
+
+module("plugin.inline", package.seeall)
+
+require('handlers.render')
+
+function find(env, opts)
+    local found={}
+    for name, _ in pairs(env.pages) do
+        if string.match(name, opts.pattern) then
+            table.insert(found, name)
+        end
+    end
+    local function get_created(name)
+        local meta=env.pages[name].meta
+        return (meta and meta.created_at)
+    end
+    table.sort(found, function(a, b)
+                        local ca=get_created(a)
+                        local cb=get_created(b)
+                        if not ca then
+                            return false
+                        elseif not cb then
+                            return true
+                        else
+                            return ca > cb
+                        end
+                      end)
+    
+    if not opts.count then
+        return found
+    else
+        local results={}
+        for i=1,math.min(opts.count, #found) do
+            results[i]=found[i]
+        end
+        return results
+    end
+end
+
+function render(env, opts, pages_)
+    local inlinepages={}
+    local page=env.page
+    local to_root=path.to_root(page.destination)
+    
+    for i, file in ipairs(pages_) do
+        local inlinepage=env.pages[file]
+        local relative_location, path_prefix
+        
+        if opts.absolute then
+            location=opts.absolute..inlinepage.destination
+            path_prefix=opts.absolute..path.dirname_slash(inlinepage.destination)
+        else
+            location=path.simplify(to_root..inlinepage.destination)
+            path_prefix=path.dirname_slash(location)
+        end
+        -- clean away index
+        if string.match(location, "^index%.[^.]*$") then
+            location=""
+        else
+            location=string.gsub(location, "/index%.[^.]*$", "/")
+        end
+        
+        inlinepages[i]=table.copy(inlinepage)
+        -- TODO: env väärin?
+        if not opts.no_content then
+            inlinepages[i].content=handlers.render.render(file, env, path_prefix)
+        end
+        inlinepages[i].location=location
+    end
+    
+    local tmplfile=path.join(env.paths.tmpl, opts.template or "inline.template")
+    local inline_template=filecache.get(tmplfile)
+
+    local newenv=table.join(env, { inlinepages=inlinepages })
+    return handlers.render.process_lua(inline_template, newenv)
+end
+
+function inline(env, opts)
+    return render(env, opts, find(env, opts))
+end

mercurial