summaryrefslogtreecommitdiffstats
path: root/scripts/metadata_filter.lua
blob: d5a29094c123a99f2721a079cb2c656159324d8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
local path = require 'pandoc.path'

function pandoc.List:flatMap(fn)
    local mapped = self:map(fn)
    local result = pandoc.List()

    for i = 1, #mapped do result:extend(mapped[i]) end

    return result
end

function pandoc.List:take(n)
    if n >= #self then return self end

    local result = pandoc.List()

    for i = 1, n do result:insert(self[i]) end

    return result
end

function format_date(date)
    if not date then return date end

    date = pandoc.utils.normalize_date(pandoc.utils.stringify(date))
    local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)")
    if not year then return nil end

    local time = os.time({ year = tonumber(year), month = tonumber(month), day = tonumber(day) })
    return {
        yyyy_mm_dd = os.date("%F", time),
        yyyy = os.date("%Y", time),
        mm = os.date("%m", time),
        dd = os.date("%d", time),
        rfc3339 = os.date("%FT%T+00:00", time),
        long = os.date("%B %d, %Y", time),
        short = os.date("%b %d, %y", time),
    }
end

function table_to_list(t, kv, cmp)
    local l = pandoc.List()

    if kv then
        for key, value in pairs(t) do l:insert({ key = key, value = value }) end
    else
        for _, value in pairs(t) do l:insert(value) end
    end

    l:sort(cmp or function(i1, i2) return i1.key < i2.key end)

    return l
end

function make_absolute(rel, base)
    return path.is_absolute(rel) and rel or path.join({ path.directory(base), rel })
end

function resolve_url(site_url, ref_file, target_file)
    target_file = target_file:gsub("/index%.html$", "/")

    local ref_base_dir = path.directory(ref_file)
    local abs = target_file
    local rel = path.make_relative(abs, ref_base_dir, true)
    local full = (abs:sub(1, 1) == "/" and (site_url .. abs)) or abs

    return { abs = abs, rel = rel, full = full }
end

function resolve_layout(depth)
    local layout = "categorized_list"

    if depth == "0" then
        layout = "page"
    elseif depth == "1" then
        layout = "list"
    end

    return layout
end

function prep_layout(layout)
    layout = pandoc.utils.stringify(layout)
    return { id = layout, ["is_" .. layout] = true }
end

function resolve_namespace(namespace)
    namespace = pandoc.utils.stringify(namespace)

    local root = "index"
    if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end

    return { root = { id = root, ["is_" .. root] = true }, full = namespace }
end

function prep_menu(active_id, main_menu)
    local active_item = nil
    local items = pandoc.List()

    for i = 1, #main_menu do
        local item = main_menu[i]
        local active = pandoc.utils.stringify(item.id) == active_id
        item.active = active
        if active then active_item = item end
        if not item.hidden or item.active then items:insert(item) end
    end

    return { items = items, active = active_item }
end

function process_pages(global, order, pages_by_id)
    if not pages_by_id then return nil end

    local pages_all = pandoc.List()
    local pages_date_desc = pandoc.List()

    for _, page in pairs(pages_by_id) do
        local p = process(global, page)
        if not p.unlisted then
            pages_all:insert(p)
            if p.date then pages_date_desc:insert(p) end
        end
    end

    pages_all:sort(function(p1, p2)
        if p1.position then
            if p2.position then
                return tonumber(p1.position) < tonumber(p2.position)
            else
                return true
            end
        elseif p2.position then
            return false
        elseif order == "date_desc" then
            if p1.date then
                if p2.date then
                    return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd
                else
                    return true
                end
            else
                return false
            end
        else
            return p1.title:upper() < p2.title:upper()
        end
    end)

    pages_date_desc:sort(function(p1, p2)
        if p1.position then
            if p2.position then
                return tonumber(p1.position) < tonumber(p2.position)
            else
                return true
            end
        else
            return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd
        end
    end)

    local pages_data = { all = pages_all, date_desc = pages_date_desc, by_id = pages_by_id }

    return pages_data
end

function find_depth(pages)
    local depth = 0

    for i = 1, #pages do
        local p = pages[i]
        local d = tonumber(p.depth)
        if d > depth then depth = d end
    end

    depth = depth + 1

    return tostring(depth)
end

function generate_list(meta)
    if not meta.pages then return nil end

    if meta.depth == "1" then
        return meta.pages.all:map(function(p)
            return {
                title = p.title,
                subtitle = p.subtitle,
                date = p.date,
                url = p.url,
                icon = p.icon or meta.icon,
                post_icon = meta.list_post_icon,
                indicator = meta.list_read_indicators,
            }
        end)
    elseif meta.depth == "2" then
        return meta.pages.all:map(function(cat)
            local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or
                              9999
            local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p)
                return {
                    title = p.title,
                    subtitle = p.subtitle,
                    date = p.date,
                    url = p.url,
                    icon = p.icon or cat.icon,
                    post_icon = cat.list_post_icon or meta.list_post_icon,
                    indicator = cat.list_read_indicators,
                }
            end)
            local items = allItems:take(limit)
            local omitted = #allItems - #items

            return {
                title = cat.title,
                description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or
                    (not cat.no_description and cat.content),
                url = cat.url,
                grid = cat.list_grid,
                items = items,
                total = tostring(#allItems),
                omitted = omitted ~= 0 and tostring(omitted),
            }
        end):filter(function(cat) return #cat.items ~= 0 end)
    elseif meta.depth == "3" then
        return meta.pages.all:map(function(cat)
            local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or
                              9999
            local allItems = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c)
                if c.pages then
                    return c.pages.all:map(function(p)
                        return {
                            title = p.title,
                            subtitle = p.subtitle,
                            category = c.title,
                            url = p.url,
                            icon = p.icon or c.icon,
                            post_icon = c.list_post_icon or cat.list_post_icon,
                            indicator = c.list_read_indicators,
                        }
                    end)
                else
                    local l = pandoc.List()
                    l:insert({
                        title = c.title,
                        subtitle = c.subtitle,
                        url = c.url,
                        icon = c.icon or cat.icon,
                        post_icon = cat.list_post_icon,
                        indicator = cat.list_read_indicators,
                    })
                    return l
                end
            end)
            local items = allItems:take(limit)
            local omitted = #allItems - #items

            return {
                title = cat.title,
                description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or
                    (not cat.no_description and cat.content),
                url = cat.url,
                grid = cat.list_grid,
                items = items,
                total = tostring(#allItems),
                omitted = omitted ~= 0 and tostring(omitted),
            }
        end):filter(function(cat) return #cat.items ~= 0 end)
    end
end

function process(global, meta)
    meta.namespace = resolve_namespace(meta.namespace)
    meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "")
    meta.redirect = meta.url and true
    meta.url = meta.url and pandoc.utils.stringify(meta.url)
    meta.url = resolve_url(global.site.url, global.file_out, meta.url or meta.file_out)
    meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or ""
    if meta.list_order then meta.list_order = pandoc.utils.stringify(meta.list_order) end
    if meta.position then meta.position = pandoc.utils.stringify(meta.position) end

    if meta.feed then
        if meta.file_out:match(".html$") then
            meta.feed = {
                url = resolve_url(global.site.url, global.file_out,
                    meta.file_out:gsub(".html$", ".xml")),
            }
        else
            meta.page = {
                url = resolve_url(global.site.url, global.file_out,
                    meta.file_out:gsub(".xml$", ".html")),
            }
        end
    end

    if meta.preview then
        meta.preview = make_absolute(pandoc.utils.stringify(meta.preview), meta.file_out)
        meta.preview = resolve_url(global.site.url, global.file_out, meta.preview)
    end

    if meta.menus and meta.menus.main then
        meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main)
    end

    meta.pages = process_pages(global, meta.list_order, meta.pages)
    meta.depth = (meta.pages and find_depth(meta.pages.all)) or "0"
    meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or
                                  resolve_layout(meta.depth))

    if meta.date then
        meta.date = format_date(meta.date)
    elseif meta.pages and #meta.pages.date_desc ~= 0 then
        meta.date = meta.pages.date_desc[1].date
    end

    if meta.last_update then
        meta.last_update = format_date(meta.last_update)
    elseif meta.pages and #meta.pages.date_desc ~= 0 then
        meta.last_update = meta.pages.date_desc[1].last_update
    elseif meta.date then
        meta.last_update = meta.date
    end

    meta.list = generate_list(meta)

    return meta
end

function Meta(meta)
    meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "")

    return process(meta, meta)
end