plugin/inline.lua

Tue, 16 Jan 2018 17:25:50 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 16 Jan 2018 17:25:50 +0000
changeset 13
5886bf51708b
parent 9
751216807683
child 26
77cd7b8fb6a6
permissions
-rwxr-xr-x

README typofixes etc.

3
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 local found={}
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 for name, _ in pairs(env.pages) do
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 if string.match(name, opts.pattern) then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 table.insert(found, name)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 local function get_created(name)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 local meta=env.pages[name].meta
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 return (meta and meta.created_at)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 table.sort(found, function(a, b)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 local ca=get_created(a)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 local cb=get_created(b)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 if not ca then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 return false
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 elseif not cb then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 return true
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 return ca > cb
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 end)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 if not opts.count then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 return found
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 local results={}
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 for i=1,math.min(opts.count, #found) do
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 results[i]=found[i]
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 return results
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 local inlinepages={}
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 local page=env.page
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 local to_root=path.to_root(page.destination)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 for i, file in ipairs(pages_) do
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 local inlinepage=env.pages[file]
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 local relative_location, path_prefix
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 if opts.absolute then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 location=opts.absolute..inlinepage.destination
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 path_prefix=opts.absolute..path.dirname_slash(inlinepage.destination)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 location=path.simplify(to_root..inlinepage.destination)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 path_prefix=path.dirname_slash(location)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 -- clean away index
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 if string.match(location, "^index%.[^.]*$") then
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 location=""
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 else
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 location=string.gsub(location, "/index%.[^.]*$", "/")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 inlinepages[i]=table.copy(inlinepage)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 -- TODO: env väärin?
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 inlinepages[i].location=location
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 local tmplfile=path.join(env.paths.tmpl, opts.template or "inline.template")
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 local inline_template=filecache.get(tmplfile)
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 end
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
b2df1b3f2c83 Improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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

mercurial