diff options
| author | Volpeon <git@volpeon.ink> | 2022-08-05 18:06:20 +0200 |
|---|---|---|
| committer | Volpeon <git@volpeon.ink> | 2022-08-05 18:06:20 +0200 |
| commit | 6de1ad4cdc8b1fb10cf0a5c61d5ed03893f131c1 (patch) | |
| tree | 5df16cd2a5d4938016346971a53968c398206624 /scripts/page.lua | |
| parent | Stricter pruning, don't pre-calculate description field (diff) | |
| download | volpeon.ink-6de1ad4cdc8b1fb10cf0a5c61d5ed03893f131c1.tar.gz volpeon.ink-6de1ad4cdc8b1fb10cf0a5c61d5ed03893f131c1.tar.bz2 volpeon.ink-6de1ad4cdc8b1fb10cf0a5c61d5ed03893f131c1.zip | |
Show related content
Diffstat (limited to 'scripts/page.lua')
| -rw-r--r-- | scripts/page.lua | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/scripts/page.lua b/scripts/page.lua index f3802a2..dcbb36f 100644 --- a/scripts/page.lua +++ b/scripts/page.lua | |||
| @@ -96,7 +96,7 @@ function generate_list(meta) | |||
| 96 | if meta.depth < 1 then return pandoc.List() end | 96 | if meta.depth < 1 then return pandoc.List() end |
| 97 | 97 | ||
| 98 | if meta.depth == 1 then | 98 | if meta.depth == 1 then |
| 99 | return meta.pages.all | 99 | return meta.pages.all:map(function (p) return d1_page_to_list_item(p) end) |
| 100 | end | 100 | end |
| 101 | 101 | ||
| 102 | if meta.depth == 2 then | 102 | if meta.depth == 2 then |
| @@ -130,6 +130,22 @@ function generate_list(meta) | |||
| 130 | end | 130 | end |
| 131 | end | 131 | end |
| 132 | 132 | ||
| 133 | function generate_related(meta) | ||
| 134 | if not meta.parent then return nil end | ||
| 135 | |||
| 136 | meta = meta.parent | ||
| 137 | |||
| 138 | local items = meta.pages.random:take(2):map(function (p) return d1_page_to_list_item(p) end) | ||
| 139 | |||
| 140 | if #items == 0 then return nil end | ||
| 141 | |||
| 142 | return { | ||
| 143 | url = meta.url, | ||
| 144 | layout = meta.list_layout, | ||
| 145 | items = items | ||
| 146 | } | ||
| 147 | end | ||
| 148 | |||
| 133 | function deref_page(pages) | 149 | function deref_page(pages) |
| 134 | return function(ref) | 150 | return function(ref) |
| 135 | return pages[utils.stringify(ref)] | 151 | return pages[utils.stringify(ref)] |
| @@ -139,7 +155,7 @@ end | |||
| 139 | function process_pages(meta) | 155 | function process_pages(meta) |
| 140 | meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) | 156 | meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) |
| 141 | meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) | 157 | meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) |
| 142 | meta.pages.random = meta.pages.random:filterMap(deref_page(meta.pages.by_id)) | 158 | meta.pages.random = meta.pages.all :clone():shuffle() |
| 143 | end | 159 | end |
| 144 | 160 | ||
| 145 | function resolve_urls(global, build, meta) | 161 | function resolve_urls(global, build, meta) |
| @@ -177,12 +193,10 @@ function process(global, build, meta, dir) | |||
| 177 | process_base(meta) | 193 | process_base(meta) |
| 178 | resolve_urls(global, build, meta) | 194 | resolve_urls(global, build, meta) |
| 179 | 195 | ||
| 180 | if dir >= 0 then | 196 | for _, p in pairs(meta.pages.by_id) do |
| 181 | for _, p in pairs(meta.pages.by_id) do | 197 | p.parent = meta |
| 182 | p.parent = meta | 198 | process(global, build, p, 1) |
| 183 | process(global, build, p, 1) | 199 | p.parent = nil |
| 184 | p.parent = nil | ||
| 185 | end | ||
| 186 | end | 200 | end |
| 187 | 201 | ||
| 188 | process_pages(meta) | 202 | process_pages(meta) |
| @@ -217,6 +231,8 @@ function prune(meta, up, down) | |||
| 217 | end | 231 | end |
| 218 | 232 | ||
| 219 | function Meta(meta) | 233 | function Meta(meta) |
| 234 | math.randomseed(os.time()) | ||
| 235 | |||
| 220 | local build = {} | 236 | local build = {} |
| 221 | 237 | ||
| 222 | for key, value in pairs(meta) do | 238 | for key, value in pairs(meta) do |
| @@ -250,6 +266,7 @@ function Meta(meta) | |||
| 250 | process(global, build, meta, 0) | 266 | process(global, build, meta, 0) |
| 251 | 267 | ||
| 252 | meta.list = generate_list(meta) | 268 | meta.list = generate_list(meta) |
| 269 | meta.related = generate_related(meta) | ||
| 253 | 270 | ||
| 254 | if global.menus and global.menus.main then | 271 | if global.menus and global.menus.main then |
| 255 | global.menus.main = prep_menu(meta.namespace.full, global.menus.main) | 272 | global.menus.main = prep_menu(meta.namespace.full, global.menus.main) |
