summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/metadata_filter.lua217
1 files changed, 84 insertions, 133 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua
index fcceffa..314f2b0 100644
--- a/scripts/metadata_filter.lua
+++ b/scripts/metadata_filter.lua
@@ -57,15 +57,6 @@ function slug(str)
57 return str:lower():gsub("[^ a-z]", ""):gsub("[ ]+", "-") 57 return str:lower():gsub("[^ a-z]", ""):gsub("[ ]+", "-")
58end 58end
59 59
60function file_exists(name)
61 local f = io.open(name, "r")
62 if f ~= nil then
63 io.close(f)
64 return true
65 end
66 return false
67end
68
69function format_date(date) 60function format_date(date)
70 if not date then return date end 61 if not date then return date end
71 62
@@ -85,20 +76,6 @@ function format_date(date)
85 } 76 }
86end 77end
87 78
88function table_to_list(t, kv, cmp)
89 local l = pandoc.List()
90
91 if kv then
92 for key, value in pairs(t) do l:insert({ key = key, value = value }) end
93 else
94 for _, value in pairs(t) do l:insert(value) end
95 end
96
97 l:sort(cmp or function(i1, i2) return i1.key < i2.key end)
98
99 return l
100end
101
102function make_absolute(rel, base) 79function make_absolute(rel, base)
103 return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel }) 80 return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel })
104end 81end
@@ -117,9 +94,9 @@ end
117function resolve_layout(depth) 94function resolve_layout(depth)
118 local layout = "categorized_list" 95 local layout = "categorized_list"
119 96
120 if depth == "0" then 97 if depth == 0 then
121 layout = "page" 98 layout = "page"
122 elseif depth == "1" then 99 elseif depth == 1 then
123 layout = "list" 100 layout = "list"
124 end 101 end
125 102
@@ -180,124 +157,98 @@ end
180function find_depth(pages) 157function find_depth(pages)
181 local depth = 0 158 local depth = 0
182 159
183 for i = 1, #pages do 160 if pages then
184 local p = pages[i] 161 for i = 1, #pages.all do
185 local d = tonumber(p.depth) 162 local p = pages.all[i]
186 if d > depth then depth = d end 163 local d = tonumber(p.depth)
164 if d > depth then depth = d end
165 end
166
167 depth = depth + 1
187 end 168 end
188 169
189 depth = depth + 1 170 return depth
171end
190 172
191 return tostring(depth) 173function d1_page_to_list_item(meta, p)
174 return {
175 title = p.title,
176 subtitle = p.subtitle,
177 date = p.date,
178 position = p.position,
179 url = p.url,
180 slug = p.slug,
181 thumbnail = p.thumbnail,
182 icon = p.icon or meta.icon,
183 post_icon = p.post_icon or meta.list_post_icon,
184 indicator = meta.list_read_indicators,
185 }
186end
187
188function d2_page_to_list_item(meta, cat, p)
189 return {
190 title = p.title,
191 subtitle = p.subtitle,
192 date = p.date,
193 position = p.position,
194 url = p.url,
195 slug = p.slug,
196 thumbnail = p.thumbnail,
197 icon = p.icon or cat.icon,
198 post_icon = p.post_icon or cat.list_post_icon or meta.list_post_icon,
199 indicator = cat.list_read_indicators,
200 }
201end
202
203function cat_to_list_cat(cat, allItems)
204 local limit = cat.list_limit or 9999
205 local items = allItems:take(limit)
206 local omitted = #allItems - #items
207
208 return {
209 title = cat.title,
210 description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or
211 (not cat.no_description and cat.content),
212 url = cat.url,
213 slug = cat.slug,
214 layout = cat.list_layout,
215 items = items,
216 total = tostring(#allItems),
217 omitted = omitted ~= 0 and tostring(omitted),
218 }
192end 219end
193 220
194function generate_list(meta) 221function generate_list(meta)
195 if not meta.pages then return nil end 222 if meta.depth < 1 then return nil end
196 223
197 if meta.depth == "1" then 224 if meta.depth == 1 then
198 return meta.pages.all:map(function(p) 225 return meta.pages.all:map(function(p) return d1_page_to_list_item(meta, p) end)
199 return { 226 end
200 title = p.title, 227
201 subtitle = p.subtitle, 228 if meta.depth == 2 then
202 date = p.date,
203 position = p.position,
204 url = p.url,
205 slug = p.slug,
206 thumbnail = p.thumbnail,
207 icon = p.icon or meta.icon,
208 post_icon = p.post_icon or meta.list_post_icon,
209 indicator = meta.list_read_indicators,
210 }
211 end)
212 elseif meta.depth == "2" then
213 return meta.pages.all 229 return meta.pages.all
214 :map(function(cat) 230 :map(function(cat)
215 local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or 231 local allItems = ((cat.pages and cat.pages.all) or pandoc.List())
216 9999 232 :map(function(p) return d2_page_to_list_item(meta, cat, p) end)
217 local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) 233
218 return { 234 return cat_to_list_cat(cat, allItems)
219 title = p.title,
220 subtitle = p.subtitle,
221 date = p.date,
222 position = p.position,
223 url = p.url,
224 slug = p.slug,
225 thumbnail = p.thumbnail,
226 icon = p.icon or cat.icon,
227 post_icon = p.post_icon or cat.list_post_icon or meta.list_post_icon,
228 indicator = cat.list_read_indicators,
229 }
230 end)
231 local items = allItems:take(limit)
232 local omitted = #allItems - #items
233
234 return {
235 title = cat.title,
236 description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or
237 (not cat.no_description and cat.content),
238 url = cat.url,
239 slug = cat.slug,
240 layout = cat.list_layout,
241 items = items,
242 total = tostring(#allItems),
243 omitted = omitted ~= 0 and tostring(omitted),
244 }
245 end) 235 end)
246 :filter(function(cat) return #cat.items ~= 0 end) 236 :filter(function(cat) return #cat.items ~= 0 end)
247 elseif meta.depth == "3" then 237 end
238
239 if meta.depth == 3 then
248 return meta.pages.all 240 return meta.pages.all
249 :map(function(cat) 241 :map(function(cat)
250 local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or 242 local allItems = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c)
251 9999 243 if c.pages then
252 local allItems = (cat.pages and cat.pages.all or pandoc.List()) 244 return c.pages.all:map(function(p) return d2_page_to_list_item(cat, c, p) end)
253 :flatMap(function(c) 245 else
254 if c.pages then 246 return pandoc.List({ d1_page_to_list_item(cat, c) })
255 return c.pages.all:map(function(p) 247 end
256 return { 248 end)
257 title = p.title,
258 subtitle = p.subtitle,
259 date = p.date,
260 position = p.position,
261 category = c.title,
262 url = p.url,
263 slug = p.slug,
264 thumbnail = p.thumbnail,
265 icon = p.icon or c.icon,
266 post_icon = p.post_icon or c.list_post_icon or cat.list_post_icon,
267 indicator = c.list_read_indicators,
268 }
269 end)
270 else
271 local l = pandoc.List()
272 l:insert({
273 title = c.title,
274 subtitle = c.subtitle,
275 date = c.date,
276 position = c.position,
277 url = c.url,
278 slug = c.slug,
279 icon = c.icon or cat.icon,
280 post_icon = c.post_icon or cat.list_post_icon,
281 indicator = cat.list_read_indicators,
282 })
283 return l
284 end
285 end)
286 allItems:sort(page_sort(cat.list_order)) 249 allItems:sort(page_sort(cat.list_order))
287 local items = allItems:take(limit) 250
288 local omitted = #allItems - #items 251 return cat_to_list_cat(cat, allItems)
289
290 return {
291 title = cat.title,
292 description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or
293 (not cat.no_description and cat.content),
294 url = cat.url,
295 slug = cat.slug,
296 layout = cat.list_layout,
297 items = items,
298 total = tostring(#allItems),
299 omitted = omitted ~= 0 and tostring(omitted),
300 }
301 end) 252 end)
302 :filter(function(cat) return #cat.items ~= 0 end) 253 :filter(function(cat) return #cat.items ~= 0 end)
303 end 254 end
@@ -313,6 +264,7 @@ function process(global, meta)
313 meta.slug = slug(meta.title) 264 meta.slug = slug(meta.title)
314 if meta.list_order then meta.list_order = pandoc.utils.stringify(meta.list_order) end 265 if meta.list_order then meta.list_order = pandoc.utils.stringify(meta.list_order) end
315 meta.list_layout = meta.list_layout and prep_layout(meta.list_layout) 266 meta.list_layout = meta.list_layout and prep_layout(meta.list_layout)
267 if meta.list_limit then meta.list_limit = tonumber(pandoc.utils.stringify(meta.list_limit)) end
316 if meta.position then meta.position = pandoc.utils.stringify(meta.position) end 268 if meta.position then meta.position = pandoc.utils.stringify(meta.position) end
317 269
318 if meta.feed then 270 if meta.feed then
@@ -340,9 +292,8 @@ function process(global, meta)
340 end 292 end
341 293
342 meta.pages = process_pages(global, meta.list_order, meta.pages) 294 meta.pages = process_pages(global, meta.list_order, meta.pages)
343 meta.depth = (meta.pages and find_depth(meta.pages.all)) or "0" 295 meta.depth = find_depth(meta.pages)
344 meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or 296 meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth))
345 resolve_layout(meta.depth))
346 297
347 if meta.date then 298 if meta.date then
348 meta.date = format_date(meta.date) 299 meta.date = format_date(meta.date)