diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/metadata_filter.lua | 229 |
1 files changed, 121 insertions, 108 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 4ac4a4d..fcceffa 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
| @@ -1,5 +1,13 @@ | |||
| 1 | local path = require 'pandoc.path' | 1 | local path = require 'pandoc.path' |
| 2 | 2 | ||
| 3 | function pandoc.List:flatten() | ||
| 4 | local result = pandoc.List() | ||
| 5 | |||
| 6 | for i = 1, #self do result:extend(self[i]) end | ||
| 7 | |||
| 8 | return result | ||
| 9 | end | ||
| 10 | |||
| 3 | function pandoc.List:flatMap(fn) | 11 | function pandoc.List:flatMap(fn) |
| 4 | local mapped = self:map(fn) | 12 | local mapped = self:map(fn) |
| 5 | local result = pandoc.List() | 13 | local result = pandoc.List() |
| @@ -19,6 +27,32 @@ function pandoc.List:take(n) | |||
| 19 | return result | 27 | return result |
| 20 | end | 28 | end |
| 21 | 29 | ||
| 30 | function page_sort(order) | ||
| 31 | return function(p1, p2) | ||
| 32 | if p1.position then | ||
| 33 | if p2.position then | ||
| 34 | return tonumber(p1.position) < tonumber(p2.position) | ||
| 35 | else | ||
| 36 | return true | ||
| 37 | end | ||
| 38 | elseif p2.position then | ||
| 39 | return false | ||
| 40 | elseif order == "date_desc" then | ||
| 41 | if p1.date then | ||
| 42 | if p2.date then | ||
| 43 | return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd | ||
| 44 | else | ||
| 45 | return true | ||
| 46 | end | ||
| 47 | else | ||
| 48 | return false | ||
| 49 | end | ||
| 50 | else | ||
| 51 | return p1.title:upper() < p2.title:upper() | ||
| 52 | end | ||
| 53 | end | ||
| 54 | end | ||
| 55 | |||
| 22 | function slug(str) | 56 | function slug(str) |
| 23 | return str:lower():gsub("[^ a-z]", ""):gsub("[ ]+", "-") | 57 | return str:lower():gsub("[^ a-z]", ""):gsub("[ ]+", "-") |
| 24 | end | 58 | end |
| @@ -135,41 +169,8 @@ function process_pages(global, order, pages_by_id) | |||
| 135 | end | 169 | end |
| 136 | end | 170 | end |
| 137 | 171 | ||
| 138 | pages_all:sort(function(p1, p2) | 172 | pages_all:sort(page_sort(order)) |
| 139 | if p1.position then | 173 | pages_date_desc:sort(page_sort("date_desc")) |
| 140 | if p2.position then | ||
| 141 | return tonumber(p1.position) < tonumber(p2.position) | ||
| 142 | else | ||
| 143 | return true | ||
| 144 | end | ||
| 145 | elseif p2.position then | ||
| 146 | return false | ||
| 147 | elseif order == "date_desc" then | ||
| 148 | if p1.date then | ||
| 149 | if p2.date then | ||
| 150 | return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd | ||
| 151 | else | ||
| 152 | return true | ||
| 153 | end | ||
| 154 | else | ||
| 155 | return false | ||
| 156 | end | ||
| 157 | else | ||
| 158 | return p1.title:upper() < p2.title:upper() | ||
| 159 | end | ||
| 160 | end) | ||
| 161 | |||
| 162 | pages_date_desc:sort(function(p1, p2) | ||
| 163 | if p1.position then | ||
| 164 | if p2.position then | ||
| 165 | return tonumber(p1.position) < tonumber(p2.position) | ||
| 166 | else | ||
| 167 | return true | ||
| 168 | end | ||
| 169 | else | ||
| 170 | return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd | ||
| 171 | end | ||
| 172 | end) | ||
| 173 | 174 | ||
| 174 | local pages_data = { all = pages_all, date_desc = pages_date_desc, by_id = pages_by_id } | 175 | local pages_data = { all = pages_all, date_desc = pages_date_desc, by_id = pages_by_id } |
| 175 | 176 | ||
| @@ -199,6 +200,7 @@ function generate_list(meta) | |||
| 199 | title = p.title, | 200 | title = p.title, |
| 200 | subtitle = p.subtitle, | 201 | subtitle = p.subtitle, |
| 201 | date = p.date, | 202 | date = p.date, |
| 203 | position = p.position, | ||
| 202 | url = p.url, | 204 | url = p.url, |
| 203 | slug = p.slug, | 205 | slug = p.slug, |
| 204 | thumbnail = p.thumbnail, | 206 | thumbnail = p.thumbnail, |
| @@ -208,85 +210,96 @@ function generate_list(meta) | |||
| 208 | } | 210 | } |
| 209 | end) | 211 | end) |
| 210 | elseif meta.depth == "2" then | 212 | elseif meta.depth == "2" then |
| 211 | return meta.pages.all:map(function(cat) | 213 | return meta.pages.all |
| 212 | local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or | 214 | :map(function(cat) |
| 213 | 9999 | 215 | local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or |
| 214 | local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) | 216 | 9999 |
| 217 | local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) | ||
| 218 | return { | ||
| 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 | |||
| 215 | return { | 234 | return { |
| 216 | title = p.title, | 235 | title = cat.title, |
| 217 | subtitle = p.subtitle, | 236 | description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or |
| 218 | date = p.date, | 237 | (not cat.no_description and cat.content), |
| 219 | url = p.url, | 238 | url = cat.url, |
| 220 | slug = p.slug, | 239 | slug = cat.slug, |
| 221 | thumbnail = p.thumbnail, | 240 | layout = cat.list_layout, |
| 222 | icon = p.icon or cat.icon, | 241 | items = items, |
| 223 | post_icon = p.post_icon or cat.list_post_icon or meta.list_post_icon, | 242 | total = tostring(#allItems), |
| 224 | indicator = cat.list_read_indicators, | 243 | omitted = omitted ~= 0 and tostring(omitted), |
| 225 | } | 244 | } |
| 226 | end) | 245 | end) |
| 227 | local items = allItems:take(limit) | 246 | :filter(function(cat) return #cat.items ~= 0 end) |
| 228 | local omitted = #allItems - #items | ||
| 229 | |||
| 230 | return { | ||
| 231 | title = cat.title, | ||
| 232 | description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or | ||
| 233 | (not cat.no_description and cat.content), | ||
| 234 | url = cat.url, | ||
| 235 | slug = cat.slug, | ||
| 236 | layout = cat.list_layout, | ||
| 237 | items = items, | ||
| 238 | total = tostring(#allItems), | ||
| 239 | omitted = omitted ~= 0 and tostring(omitted), | ||
| 240 | } | ||
| 241 | end):filter(function(cat) return #cat.items ~= 0 end) | ||
| 242 | elseif meta.depth == "3" then | 247 | elseif meta.depth == "3" then |
| 243 | return meta.pages.all:map(function(cat) | 248 | return meta.pages.all |
| 244 | local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or | 249 | :map(function(cat) |
| 245 | 9999 | 250 | local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or |
| 246 | local allItems = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c) | 251 | 9999 |
| 247 | if c.pages then | 252 | local allItems = (cat.pages and cat.pages.all or pandoc.List()) |
| 248 | return c.pages.all:map(function(p) | 253 | :flatMap(function(c) |
| 249 | return { | 254 | if c.pages then |
| 250 | title = p.title, | 255 | return c.pages.all:map(function(p) |
| 251 | subtitle = p.subtitle, | 256 | return { |
| 252 | category = c.title, | 257 | title = p.title, |
| 253 | url = p.url, | 258 | subtitle = p.subtitle, |
| 254 | slug = p.slug, | 259 | date = p.date, |
| 255 | thumbnail = p.thumbnail, | 260 | position = p.position, |
| 256 | icon = p.icon or c.icon, | 261 | category = c.title, |
| 257 | post_icon = p.post_icon or c.list_post_icon or cat.list_post_icon, | 262 | url = p.url, |
| 258 | indicator = c.list_read_indicators, | 263 | slug = p.slug, |
| 259 | } | 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 | ||
| 260 | end) | 285 | end) |
| 261 | else | 286 | allItems:sort(page_sort(cat.list_order)) |
| 262 | local l = pandoc.List() | 287 | local items = allItems:take(limit) |
| 263 | l:insert({ | 288 | local omitted = #allItems - #items |
| 264 | title = c.title, | ||
| 265 | subtitle = c.subtitle, | ||
| 266 | url = c.url, | ||
| 267 | slug = c.slug, | ||
| 268 | icon = c.icon or cat.icon, | ||
| 269 | post_icon = c.post_icon or cat.list_post_icon, | ||
| 270 | indicator = cat.list_read_indicators, | ||
| 271 | }) | ||
| 272 | return l | ||
| 273 | end | ||
| 274 | end) | ||
| 275 | local items = allItems:take(limit) | ||
| 276 | local omitted = #allItems - #items | ||
| 277 | 289 | ||
| 278 | return { | 290 | return { |
| 279 | title = cat.title, | 291 | title = cat.title, |
| 280 | description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or | 292 | description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or |
| 281 | (not cat.no_description and cat.content), | 293 | (not cat.no_description and cat.content), |
| 282 | url = cat.url, | 294 | url = cat.url, |
| 283 | slug = cat.slug, | 295 | slug = cat.slug, |
| 284 | layout = cat.list_layout, | 296 | layout = cat.list_layout, |
| 285 | items = items, | 297 | items = items, |
| 286 | total = tostring(#allItems), | 298 | total = tostring(#allItems), |
| 287 | omitted = omitted ~= 0 and tostring(omitted), | 299 | omitted = omitted ~= 0 and tostring(omitted), |
| 288 | } | 300 | } |
| 289 | end):filter(function(cat) return #cat.items ~= 0 end) | 301 | end) |
| 302 | :filter(function(cat) return #cat.items ~= 0 end) | ||
| 290 | end | 303 | end |
| 291 | end | 304 | end |
| 292 | 305 | ||
