plugin/inline.lua

Tue, 16 Jan 2018 17:02:22 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 16 Jan 2018 17:02:22 +0000
changeset 8
836dac92eced
parent 3
b2df1b3f2c83
child 9
751216807683
permissions
-rwxr-xr-x

Update markdown; now Lua 5.3 compatible

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

mercurial