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