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
|
function format_date(date)
if date == nil 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 year == nil then return nil end
local time = os.time({
year = tonumber(year),
month = tonumber(month),
day = tonumber(day)
})
return pandoc.MetaMap({
yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)),
yyyy = pandoc.MetaString(os.date("%Y", time)),
mm_dd = pandoc.MetaString(os.date("%m-%d", time)),
rfc3339 = pandoc.MetaString(os.date("%FT%T+00:00", time))
})
end
function table_to_list(t, cmp)
local l = pandoc.List()
for key, value in pairs(t) do
l:insert(pandoc.MetaMap({key = key, value = value}))
end
l:sort(cmp or function(i1, i2) return i1.key < i2.key end)
return l
end
function group_by(l, field, insert)
insert = insert or function(group, _, item)
if not group then
group = l:new({item})
return group
end
group:insert(item)
end
local groups = {}
for i = 1, #l do
local item = l[i]
local f = field(item)
if f ~= nil then
local out = insert(groups[f], f, item)
if out ~= nil then groups[f] = out end
end
end
return groups
end
function splitstr(input, sep)
sep = sep or "%s"
local t = {}
for str in input:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end
return t
end
function relative_to(dir, target)
dir = splitstr(dir, "/")
target = splitstr(target, "/")
local prefix = true
local path = ""
for i = 1, math.min(#dir, #target) do
local t = target[i]
if prefix then
if dir[i] ~= t then
prefix = false
path = "../" .. t
end
else
path = "../" .. path .. "/" .. t
end
end
if #dir < #target then
for i = #dir + 1, #target do
path = path .. (path == "" and "" or "/") .. target[i]
end
elseif #dir > #target then
for _ = #target + 1, #dir do path = "../" .. path end
end
return path
end
function resolve_url(page_type, site_url, content_dir, base_dir, cur_file)
if page_type == "page" then
cur_file = cur_file:gsub("%.md$", ".html")
elseif page_type == "feed" then
cur_file = cur_file:gsub("%.md$", ".xml")
end
local abs = cur_file:gsub("^" .. content_dir, ""):gsub("/index.html$", "/")
local rel = relative_to(base_dir, cur_file):gsub("/index.html$", "/")
return pandoc.MetaMap({
abs = pandoc.MetaString(abs),
rel = pandoc.MetaString(rel),
full = pandoc.MetaString(site_url .. abs)
})
end
function resolve_layout(layout)
if layout then
layout = pandoc.utils.stringify(layout)
return pandoc.MetaMap({
id = pandoc.MetaString(layout),
["is_" .. layout] = pandoc.MetaBool(true)
})
end
end
function resolve_section(abs_url)
local section = abs_url:match("^/(.-)[/.]") or "index"
return pandoc.MetaMap({
id = pandoc.MetaString(section),
["is_" .. section] = pandoc.MetaBool(true)
})
end
function resolve_category(categories, category)
if categories and category then
category = pandoc.utils.stringify(category)
return pandoc.MetaMap({
id = pandoc.MetaString(category),
name = pandoc.MetaString(categories[category] or category)
})
end
end
function create_main_menu_state(section, main_menu)
for i = 1, #main_menu do
local item = main_menu[i]
local active = pandoc.utils.stringify(item.id) == section.id
item.active = pandoc.MetaBool(active)
end
return main_menu:filter(function(item)
return not item.hidden or item.active
end)
end
function organize_subpages(pages)
local grouped_pages = group_by(pages, function(p) return not p.date end)
local pages_undated = grouped_pages[true] or pandoc.MetaList({})
pages_undated:sort(function(p1, p2)
return pandoc.utils.stringify(p1.title) <
pandoc.utils.stringify(p2.title)
end)
local pages_dated = grouped_pages[false] or pandoc.MetaList({})
pages_dated:sort(function(p1, p2)
return pandoc.utils.stringify(p1.date.yyyy_mm_dd) >
pandoc.utils.stringify(p2.date.yyyy_mm_dd)
end)
local pages_by_year = group_by(pages_dated, function(p)
return pandoc.utils.stringify(p.date.yyyy)
end)
pages_by_year = pandoc.MetaList(table_to_list(pages_by_year,
function(i1, i2)
return i1.key > i2.key
end))
local pages_data = pandoc.MetaMap({
all_dated = pages_dated,
all_undated = pages_undated,
by_year = pages_by_year,
last_update = #pages_dated ~= 0 and pages_dated[1].last_update
})
local categories_data = group_by(pages_dated, function(p)
return p.category and pandoc.utils.stringify(p.category.id)
end, function(stats, _, p)
if not stats then
return {name = pandoc.MetaString(p.category.name), count = 1}
else
stats.count = stats.count + 1
end
end)
categories_data = pandoc.MetaList(table_to_list(categories_data))
for i = 1, #categories_data do
categories_data[i].value.count =
pandoc.MetaString(("%d"):format(categories_data[i].value.count))
end
return pages_data, categories_data
end
function Meta(meta)
meta.content_dir = meta.content_dir:gsub("/$", "")
meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "")
meta.base_file = meta.base_file or meta.file
meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1")
meta.page_type = meta.page_type or "page"
meta.layout = resolve_layout(meta.layout)
meta.date = format_date(meta.date)
if meta.last_update ~= nil then
meta.last_update = format_date(meta.last_update)
else
meta.last_update = meta.date
end
if meta.page_type == "feed" then
meta.page = pandoc.MetaMap({
url = resolve_url("page", meta.site.url, meta.content_dir,
meta.base_dir, meta.file)
})
end
meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir,
meta.base_dir, meta.file)
meta.section = resolve_section(meta.url.abs)
meta.category = resolve_category(meta.categories[meta.section.id],
meta.category)
meta.categories = nil
if meta.menus and meta.menus.main then
meta.menus.main = create_main_menu_state(meta.section, meta.menus.main)
end
if meta.pages then
local pages, categories = organize_subpages(meta.pages)
meta.pages = pages
meta.categories = categories
end
return meta
end
|