summaryrefslogtreecommitdiffstats
path: root/scripts/metadata_filter.lua
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/metadata_filter.lua')
-rw-r--r--scripts/metadata_filter.lua76
1 files changed, 42 insertions, 34 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua
index 7039a2c..ebd69f1 100644
--- a/scripts/metadata_filter.lua
+++ b/scripts/metadata_filter.lua
@@ -33,30 +33,6 @@ function table_to_list(t, kv, cmp)
33 return l 33 return l
34end 34end
35 35
36function group_by(l, field, insert)
37 insert = insert or function(group, _, item)
38 if not group then
39 group = l:new({ item })
40 return group
41 end
42 group:insert(item)
43 end
44
45 local groups = {}
46
47 for i = 1, #l do
48 local item = l[i]
49 local f = field(item)
50
51 if f then
52 local out = insert(groups[f], f, item)
53 if out then groups[f] = out end
54 end
55 end
56
57 return groups
58end
59
60function make_absolute(rel, base) 36function make_absolute(rel, base)
61 return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel }) 37 return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel })
62end 38end
@@ -67,15 +43,28 @@ function resolve_url(site_url, ref_file, target_file)
67 local ref_base_dir = path.directory(ref_file) 43 local ref_base_dir = path.directory(ref_file)
68 local abs = target_file 44 local abs = target_file
69 local rel = path.make_relative(abs, ref_base_dir, true) 45 local rel = path.make_relative(abs, ref_base_dir, true)
46 local full = (abs[1] == "/" and (site_url .. abs)) or abs
70 47
71 return { abs = abs, rel = rel, full = (site_url .. abs) } 48 return { abs = abs, rel = rel, full = full }
72end 49end
73 50
74function resolve_layout(layout) 51function resolve_layout(depth)
75 if layout then 52 local layout = "deep_categorized_list"
76 layout = pandoc.utils.stringify(layout) 53
77 return { id = layout, ["is_" .. layout] = true } 54 if depth == "0" then
55 layout = "page"
56 elseif depth == "1" then
57 layout = "list"
58 elseif depth == "2" then
59 layout = "categorized_list"
78 end 60 end
61
62 return layout
63end
64
65function prep_layout(layout)
66 layout = pandoc.utils.stringify(layout)
67 return { id = layout, ["is_" .. layout] = true }
79end 68end
80 69
81function resolve_namespace(namespace) 70function resolve_namespace(namespace)
@@ -110,8 +99,10 @@ function process_pages(global, order, pages_by_id)
110 99
111 for _, page in pairs(pages_by_id) do 100 for _, page in pairs(pages_by_id) do
112 local p = process(global, page) 101 local p = process(global, page)
113 pages_all:insert(p) 102 if not p.unlisted then
114 if p.date then pages_date_desc:insert(p) end 103 pages_all:insert(p)
104 if p.date then pages_date_desc:insert(p) end
105 end
115 end 106 end
116 107
117 pages_all:sort(function(p1, p2) 108 pages_all:sort(function(p1, p2)
@@ -171,12 +162,27 @@ function titlecase(str)
171 end) 162 end)
172end 163end
173 164
165function find_depth(pages)
166 local depth = 0
167
168 for i = 1, #pages do
169 local p = pages[i]
170 local d = tonumber(p.depth)
171 if d > depth then depth = d end
172 end
173
174 depth = depth + 1
175
176 return tostring(depth)
177end
178
174function process(global, meta) 179function process(global, meta)
175 meta.namespace = resolve_namespace(meta.namespace) 180 meta.namespace = resolve_namespace(meta.namespace)
176 meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") 181 meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "")
177 meta.layout = resolve_layout(meta.layout) 182 meta.url = meta.url and pandoc.utils.stringify(meta.url)
178 meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) 183 meta.url = resolve_url(global.site.url, global.file_out, meta.url or meta.file_out)
179 meta.title = (meta.title and titlecase(pandoc.utils.stringify(meta.title))) or "" 184 meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or ""
185 if meta.titlecase ~= false then meta.title = titlecase(meta.title) end
180 if meta.list_order then meta.list_order = pandoc.utils.stringify(meta.list_order) end 186 if meta.list_order then meta.list_order = pandoc.utils.stringify(meta.list_order) end
181 if meta.position then meta.position = pandoc.utils.stringify(meta.position) end 187 if meta.position then meta.position = pandoc.utils.stringify(meta.position) end
182 188
@@ -204,6 +210,8 @@ function process(global, meta)
204 end 210 end
205 211
206 meta.pages = process_pages(global, meta.list_order, meta.pages) 212 meta.pages = process_pages(global, meta.list_order, meta.pages)
213 meta.depth = (meta.pages and find_depth(meta.pages.all)) or "0"
214 meta.layout = prep_layout(meta.layout or resolve_layout(meta.depth))
207 215
208 if meta.date then 216 if meta.date then
209 meta.date = format_date(meta.date) 217 meta.date = format_date(meta.date)