|
1 |
|
2 module("plugin.inline", package.seeall) |
|
3 |
|
4 require('handlers.render') |
|
5 |
|
6 function find(env, opts) |
|
7 local found={} |
|
8 for name, _ in pairs(env.pages) do |
|
9 if string.match(name, opts.pattern) then |
|
10 table.insert(found, name) |
|
11 end |
|
12 end |
|
13 local function get_created(name) |
|
14 local meta=env.pages[name].meta |
|
15 return (meta and meta.created_at) |
|
16 end |
|
17 table.sort(found, function(a, b) |
|
18 local ca=get_created(a) |
|
19 local cb=get_created(b) |
|
20 if not ca then |
|
21 return false |
|
22 elseif not cb then |
|
23 return true |
|
24 else |
|
25 return ca > cb |
|
26 end |
|
27 end) |
|
28 |
|
29 if not opts.count then |
|
30 return found |
|
31 else |
|
32 local results={} |
|
33 for i=1,math.min(opts.count, #found) do |
|
34 results[i]=found[i] |
|
35 end |
|
36 return results |
|
37 end |
|
38 end |
|
39 |
|
40 function render(env, opts, pages_) |
|
41 local inlinepages={} |
|
42 local page=env.page |
|
43 local to_root=path.to_root(page.destination) |
|
44 |
|
45 for i, file in ipairs(pages_) do |
|
46 local inlinepage=env.pages[file] |
|
47 local relative_location, path_prefix |
|
48 |
|
49 if opts.absolute then |
|
50 location=opts.absolute..inlinepage.destination |
|
51 path_prefix=opts.absolute..path.dirname_slash(inlinepage.destination) |
|
52 else |
|
53 location=path.simplify(to_root..inlinepage.destination) |
|
54 path_prefix=path.dirname_slash(location) |
|
55 end |
|
56 -- clean away index |
|
57 if string.match(location, "^index%.[^.]*$") then |
|
58 location="" |
|
59 else |
|
60 location=string.gsub(location, "/index%.[^.]*$", "/") |
|
61 end |
|
62 |
|
63 inlinepages[i]=table.copy(inlinepage) |
|
64 -- TODO: env väärin? |
|
65 if not opts.no_content then |
|
66 inlinepages[i].content=handlers.render.render(file, env, path_prefix) |
|
67 end |
|
68 inlinepages[i].location=location |
|
69 end |
|
70 |
|
71 local tmplfile=path.join(env.paths.tmpl, opts.template or "inline.template") |
|
72 local inline_template=filecache.get(tmplfile) |
|
73 |
|
74 local newenv=table.join(env, { inlinepages=inlinepages }) |
|
75 return handlers.render.process_lua(inline_template, newenv) |
|
76 end |
|
77 |
|
78 function inline(env, opts) |
|
79 return render(env, opts, find(env, opts)) |
|
80 end |