summaryrefslogtreecommitdiffstats
path: root/scripts/page.lua
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/page.lua')
-rw-r--r--scripts/page.lua41
1 files changed, 22 insertions, 19 deletions
diff --git a/scripts/page.lua b/scripts/page.lua
index dcbb36f..7378bfc 100644
--- a/scripts/page.lua
+++ b/scripts/page.lua
@@ -130,32 +130,35 @@ function generate_list(meta)
130 end 130 end
131end 131end
132 132
133function deref_page(pages)
134 return function(ref)
135 return pages[utils.stringify(ref)]
136 end
137end
138
133function generate_related(meta) 139function generate_related(meta)
134 if not meta.parent then return nil end 140 if not meta.parent then return nil end
135 141
136 meta = meta.parent 142 local deref = deref_page(meta.parent.pages.by_id)
137 143
138 local items = meta.pages.random:take(2):map(function (p) return d1_page_to_list_item(p) end) 144 meta.prev = meta.prev and d1_page_to_list_item(deref(meta.prev))
145 meta.next = meta.next and d1_page_to_list_item(deref(meta.next))
139 146
140 if #items == 0 then return nil end 147 if not meta.prev and not meta.next then return nil end
141 148
142 return { 149 return {
143 url = meta.url, 150 url = meta.parent.url,
144 layout = meta.list_layout, 151 layout = meta.parent.list_layout,
145 items = items 152 prev = meta.prev,
153 next = meta.next
146 } 154 }
147end 155end
148 156
149function deref_page(pages)
150 return function(ref)
151 return pages[utils.stringify(ref)]
152 end
153end
154
155function process_pages(meta) 157function process_pages(meta)
156 meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) 158 local deref = deref_page(meta.pages.by_id)
157 meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) 159
158 meta.pages.random = meta.pages.all :clone():shuffle() 160 meta.pages.all = meta.pages.all:filterMap(deref)
161 meta.pages.date_desc = meta.pages.date_desc:filterMap(deref)
159end 162end
160 163
161function resolve_urls(global, build, meta) 164function resolve_urls(global, build, meta)
@@ -179,6 +182,7 @@ function resolve_urls(global, build, meta)
179end 182end
180 183
181function process_base(meta) 184function process_base(meta)
185 meta.id = utils.stringify(meta.id)
182 meta.depth = tonumber(utils.stringify(meta.depth)) 186 meta.depth = tonumber(utils.stringify(meta.depth))
183 meta.file_out = utils.stringify(meta.file_out) 187 meta.file_out = utils.stringify(meta.file_out)
184 meta.url = utils.stringify(meta.url) 188 meta.url = utils.stringify(meta.url)
@@ -187,6 +191,8 @@ function process_base(meta)
187 meta.list_order = meta.list_order and utils.stringify(meta.list_order) 191 meta.list_order = meta.list_order and utils.stringify(meta.list_order)
188 meta.position = meta.position and tonumber(utils.stringify(meta.position)) 192 meta.position = meta.position and tonumber(utils.stringify(meta.position))
189 meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) 193 meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail)
194 meta.prev = meta.prev and utils.stringify(meta.prev)
195 meta.next = meta.next and utils.stringify(meta.next)
190end 196end
191 197
192function process(global, build, meta, dir) 198function process(global, build, meta, dir)
@@ -207,7 +213,6 @@ function process(global, build, meta, dir)
207end 213end
208 214
209function prune_pages(meta, up, down) 215function prune_pages(meta, up, down)
210 meta.pages.random = pandoc.List()
211 meta.pages.by_id = {} 216 meta.pages.by_id = {}
212 217
213 if down == 0 then 218 if down == 0 then
@@ -231,8 +236,6 @@ function prune(meta, up, down)
231end 236end
232 237
233function Meta(meta) 238function Meta(meta)
234 math.randomseed(os.time())
235
236 local build = {} 239 local build = {}
237 240
238 for key, value in pairs(meta) do 241 for key, value in pairs(meta) do
@@ -254,7 +257,7 @@ function Meta(meta)
254 257
255 meta = meta.tree 258 meta = meta.tree
256 259
257 local parts = utils.stringify(build.namespace):split('/'):skip(1) 260 local parts = build.namespace:split('/'):skip(1)
258 for i = 1, #parts do 261 for i = 1, #parts do
259 local part = parts[i] 262 local part = parts[i]
260 local p = meta.pages.by_id[part] 263 local p = meta.pages.by_id[part]