diff options
| author | Volpeon <git@volpeon.ink> | 2022-08-02 20:56:45 +0200 |
|---|---|---|
| committer | Volpeon <git@volpeon.ink> | 2022-08-02 20:56:45 +0200 |
| commit | 944a735c39a27fc86569c585f2805837b61adb60 (patch) | |
| tree | c35f9e0c834c35a395aa58edbcebd8b5e9c83d70 /scripts | |
| parent | Style fix for card list 'list' layout gap on <=sm (diff) | |
| download | volpeon.ink-944a735c39a27fc86569c585f2805837b61adb60.tar.gz volpeon.ink-944a735c39a27fc86569c585f2805837b61adb60.tar.bz2 volpeon.ink-944a735c39a27fc86569c585f2805837b61adb60.zip | |
Slightly tidied up metadata handling
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/lib/common.lua | 54 | ||||
| -rw-r--r-- | scripts/metadata.lua | 71 |
2 files changed, 75 insertions, 50 deletions
diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua new file mode 100644 index 0000000..80b81ba --- /dev/null +++ b/scripts/lib/common.lua | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | function dump(o) | ||
| 2 | if type(o) == 'table' then | ||
| 3 | local s = '{ ' | ||
| 4 | for k, v in pairs(o) do | ||
| 5 | if type(k) ~= 'number' then k = '"' .. k .. '"' end | ||
| 6 | s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' | ||
| 7 | end | ||
| 8 | return s .. '} ' | ||
| 9 | else | ||
| 10 | return tostring(o) | ||
| 11 | end | ||
| 12 | end | ||
| 13 | |||
| 14 | function string.split(str, sep) | ||
| 15 | sep = sep or '%s' | ||
| 16 | |||
| 17 | local parts = pandoc.List() | ||
| 18 | |||
| 19 | for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do | ||
| 20 | parts:insert(field) | ||
| 21 | if s == "" then return parts end | ||
| 22 | end | ||
| 23 | end | ||
| 24 | |||
| 25 | function pandoc.List:flatten() | ||
| 26 | local result = pandoc.List() | ||
| 27 | |||
| 28 | for i = 1, #self do result:extend(self[i]) end | ||
| 29 | |||
| 30 | return result | ||
| 31 | end | ||
| 32 | |||
| 33 | function pandoc.List:flatMap(fn) | ||
| 34 | local mapped = self:map(fn) | ||
| 35 | local result = pandoc.List() | ||
| 36 | |||
| 37 | for i = 1, #mapped do result:extend(mapped[i]) end | ||
| 38 | |||
| 39 | return result | ||
| 40 | end | ||
| 41 | |||
| 42 | function pandoc.List:take(n) | ||
| 43 | if n >= #self then return self end | ||
| 44 | |||
| 45 | local result = pandoc.List() | ||
| 46 | |||
| 47 | for i = 1, n do result:insert(self[i]) end | ||
| 48 | |||
| 49 | return result | ||
| 50 | end | ||
| 51 | |||
| 52 | return { | ||
| 53 | dump = dump | ||
| 54 | } | ||
diff --git a/scripts/metadata.lua b/scripts/metadata.lua index a451040..cb6c6e6 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua | |||
| @@ -1,44 +1,5 @@ | |||
| 1 | local path = require 'pandoc.path' | 1 | local path = require 'pandoc.path' |
| 2 | 2 | local common = require 'scripts.lib.common' | |
| 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 | |||
| 11 | function pandoc.List:flatMap(fn) | ||
| 12 | local mapped = self:map(fn) | ||
| 13 | local result = pandoc.List() | ||
| 14 | |||
| 15 | for i = 1, #mapped do result:extend(mapped[i]) end | ||
| 16 | |||
| 17 | return result | ||
| 18 | end | ||
| 19 | |||
| 20 | function pandoc.List:take(n) | ||
| 21 | if n >= #self then return self end | ||
| 22 | |||
| 23 | local result = pandoc.List() | ||
| 24 | |||
| 25 | for i = 1, n do result:insert(self[i]) end | ||
| 26 | |||
| 27 | return result | ||
| 28 | end | ||
| 29 | |||
| 30 | function dump(o) | ||
| 31 | if type(o) == 'table' then | ||
| 32 | local s = '{ ' | ||
| 33 | for k, v in pairs(o) do | ||
| 34 | if type(k) ~= 'number' then k = '"' .. k .. '"' end | ||
| 35 | s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' | ||
| 36 | end | ||
| 37 | return s .. '} ' | ||
| 38 | else | ||
| 39 | return tostring(o) | ||
| 40 | end | ||
| 41 | end | ||
| 42 | 3 | ||
| 43 | function page_sort(order) | 4 | function page_sort(order) |
| 44 | return function(p1, p2) | 5 | return function(p1, p2) |
| @@ -145,13 +106,13 @@ function prep_menu(active_id, main_menu) | |||
| 145 | return { items = items, active = active_item } | 106 | return { items = items, active = active_item } |
| 146 | end | 107 | end |
| 147 | 108 | ||
| 148 | function process_pages(global, order, pages_by_id) | 109 | function process_pages(global, build, order, pages_by_id) |
| 149 | local pages_all = pandoc.List() | 110 | local pages_all = pandoc.List() |
| 150 | local pages_date_desc = pandoc.List() | 111 | local pages_date_desc = pandoc.List() |
| 151 | 112 | ||
| 152 | if pages_by_id then | 113 | if pages_by_id then |
| 153 | for _, page in pairs(pages_by_id) do | 114 | for _, page in pairs(pages_by_id) do |
| 154 | local p = process(global, page) | 115 | local p = process(global, build, page) |
| 155 | if not p.unlisted then | 116 | if not p.unlisted then |
| 156 | pages_all:insert(p) | 117 | pages_all:insert(p) |
| 157 | if p.last_update then pages_date_desc:insert(p) end | 118 | if p.last_update then pages_date_desc:insert(p) end |
| @@ -286,13 +247,13 @@ function generate_list(meta) | |||
| 286 | end | 247 | end |
| 287 | end | 248 | end |
| 288 | 249 | ||
| 289 | function process(global, meta) | 250 | function process(global, build, meta) |
| 290 | meta.namespace = resolve_namespace(meta.namespace) | 251 | meta.namespace = resolve_namespace(meta.namespace) |
| 291 | meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") | 252 | meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") |
| 292 | meta.unlisted = meta.unlisted or (meta.draft and global.mode ~= "dev") | 253 | meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") |
| 293 | meta.redirect = meta.url and true | 254 | meta.redirect = meta.url and true |
| 294 | meta.url = meta.url and pandoc.utils.stringify(meta.url) | 255 | meta.url = meta.url and pandoc.utils.stringify(meta.url) |
| 295 | meta.url = resolve_url(global.site.url, global.file_out, meta.url or meta.file_out) | 256 | meta.url = resolve_url(global.site.url, build.file_out, meta.url or meta.file_out) |
| 296 | meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" | 257 | meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" |
| 297 | meta.slug = slug(meta.title) | 258 | meta.slug = slug(meta.title) |
| 298 | meta.schema_type = (meta.schema_type and pandoc.utils.stringify(meta.schema_type)) or "CreativeWork" | 259 | meta.schema_type = (meta.schema_type and pandoc.utils.stringify(meta.schema_type)) or "CreativeWork" |
| @@ -304,11 +265,11 @@ function process(global, meta) | |||
| 304 | if meta.feed then | 265 | if meta.feed then |
| 305 | if meta.file_out:match(".html$") then | 266 | if meta.file_out:match(".html$") then |
| 306 | meta.feed = { | 267 | meta.feed = { |
| 307 | url = resolve_url(global.site.url, global.file_out, meta.file_out:gsub(".html$", ".xml")), | 268 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), |
| 308 | } | 269 | } |
| 309 | else | 270 | else |
| 310 | meta.page = { | 271 | meta.page = { |
| 311 | url = resolve_url(global.site.url, global.file_out, meta.file_out:gsub(".xml$", ".html")), | 272 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), |
| 312 | } | 273 | } |
| 313 | end | 274 | end |
| 314 | end | 275 | end |
| @@ -316,14 +277,14 @@ function process(global, meta) | |||
| 316 | if meta.thumbnail then | 277 | if meta.thumbnail then |
| 317 | meta.thumbnail = make_absolute("thumbnail." .. pandoc.utils.stringify(meta.thumbnail), | 278 | meta.thumbnail = make_absolute("thumbnail." .. pandoc.utils.stringify(meta.thumbnail), |
| 318 | meta.file_out) | 279 | meta.file_out) |
| 319 | meta.thumbnail = resolve_url(global.site.url, global.file_out, meta.thumbnail) | 280 | meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) |
| 320 | end | 281 | end |
| 321 | 282 | ||
| 322 | if meta.menus and meta.menus.main then | 283 | if meta.menus and meta.menus.main then |
| 323 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) | 284 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) |
| 324 | end | 285 | end |
| 325 | 286 | ||
| 326 | meta.pages = process_pages(global, meta.list_order, meta.pages) | 287 | meta.pages = process_pages(global, build, meta.list_order, meta.pages) |
| 327 | meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) | 288 | meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) |
| 328 | 289 | ||
| 329 | if meta.last_update then | 290 | if meta.last_update then |
| @@ -348,8 +309,18 @@ function process(global, meta) | |||
| 348 | end | 309 | end |
| 349 | 310 | ||
| 350 | function Meta(meta) | 311 | function Meta(meta) |
| 312 | local build = {} | ||
| 313 | |||
| 314 | for key, value in pairs(meta) do | ||
| 315 | local suffix = key:match('^build.(.*)$') | ||
| 316 | |||
| 317 | if suffix then | ||
| 318 | build[suffix] = value | ||
| 319 | end | ||
| 320 | end | ||
| 321 | |||
| 351 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") | 322 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") |
| 352 | 323 | ||
| 353 | find_depth(meta) | 324 | find_depth(meta) |
| 354 | return process(meta, meta) | 325 | return process(meta, build, meta) |
| 355 | end | 326 | end |
