diff options
author | Volpeon <git@volpeon.ink> | 2021-05-11 20:43:53 +0200 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-05-11 20:43:53 +0200 |
commit | 6c20e2fd01856072cae3df727a4ef88314bffe28 (patch) | |
tree | cff2fed5663cea6d3697ab12078db3be43ebb822 /scripts | |
parent | Minor improvements in metadata processing script (diff) | |
download | volpeon.ink-6c20e2fd01856072cae3df727a4ef88314bffe28.tar.gz volpeon.ink-6c20e2fd01856072cae3df727a4ef88314bffe28.tar.bz2 volpeon.ink-6c20e2fd01856072cae3df727a4ef88314bffe28.zip |
Simplified metadata processing, improved design
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/metadata_filter.lua | 67 |
1 files changed, 28 insertions, 39 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index b8a9059..5f2d9af 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
@@ -6,20 +6,24 @@ function format_date(date) | |||
6 | if not year then return nil end | 6 | if not year then return nil end |
7 | 7 | ||
8 | local time = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) | 8 | local time = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) |
9 | return pandoc.MetaMap({ | 9 | return { |
10 | yyyy_mm_dd = os.date("%F", time), | 10 | yyyy_mm_dd = os.date("%F", time), |
11 | yyyy = os.date("%Y", time), | 11 | yyyy = os.date("%Y", time), |
12 | mm = os.date("%m", time), | 12 | mm = os.date("%m", time), |
13 | dd = os.date("%d", time), | 13 | dd = os.date("%d", time), |
14 | rfc3339 = os.date("%FT%T+00:00", time), | 14 | rfc3339 = os.date("%FT%T+00:00", time), |
15 | long = os.date("%B %d, %Y", time), | 15 | long = os.date("%B %d, %Y", time), |
16 | }) | 16 | } |
17 | end | 17 | end |
18 | 18 | ||
19 | function table_to_list(t, cmp) | 19 | function table_to_list(t, kv, cmp) |
20 | local l = pandoc.List() | 20 | local l = pandoc.List() |
21 | 21 | ||
22 | for key, value in pairs(t) do l:insert(pandoc.MetaMap({key = key, value = value})) end | 22 | if kv then |
23 | for key, value in pairs(t) do l:insert({key = key, value = value}) end | ||
24 | else | ||
25 | for _, value in pairs(t) do l:insert(value) end | ||
26 | end | ||
23 | 27 | ||
24 | l:sort(cmp or function(i1, i2) return i1.key < i2.key end) | 28 | l:sort(cmp or function(i1, i2) return i1.key < i2.key end) |
25 | 29 | ||
@@ -96,13 +100,13 @@ function resolve_url(site_url, ref_file, target_file) | |||
96 | local abs = target_file | 100 | local abs = target_file |
97 | local rel = relative_to(ref_base_dir, abs):gsub("/index%.html$", "/") | 101 | local rel = relative_to(ref_base_dir, abs):gsub("/index%.html$", "/") |
98 | 102 | ||
99 | return pandoc.MetaMap({abs = abs, rel = rel, full = (site_url .. abs)}) | 103 | return {abs = abs, rel = rel, full = (site_url .. abs)} |
100 | end | 104 | end |
101 | 105 | ||
102 | function resolve_layout(layout) | 106 | function resolve_layout(layout) |
103 | if layout then | 107 | if layout then |
104 | layout = pandoc.utils.stringify(layout) | 108 | layout = pandoc.utils.stringify(layout) |
105 | return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) | 109 | return {id = layout, ["is_" .. layout] = true} |
106 | end | 110 | end |
107 | end | 111 | end |
108 | 112 | ||
@@ -112,16 +116,13 @@ function resolve_namespace(namespace) | |||
112 | local root = "index" | 116 | local root = "index" |
113 | if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end | 117 | if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end |
114 | 118 | ||
115 | return pandoc.MetaMap({ | 119 | return {root = {id = root, ["is_" .. root] = true}, full = namespace} |
116 | root = pandoc.MetaMap({id = root, ["is_" .. root] = pandoc.MetaBool(true)}), | ||
117 | full = namespace, | ||
118 | }) | ||
119 | end | 120 | end |
120 | 121 | ||
121 | function resolve_category(categories, category) | 122 | function resolve_category(categories, category) |
122 | if categories and category then | 123 | if categories and category then |
123 | category = pandoc.utils.stringify(category) | 124 | category = pandoc.utils.stringify(category) |
124 | data = pandoc.MetaMap(categories[category]) | 125 | data = categories[category] |
125 | data.id = category | 126 | data.id = category |
126 | return data | 127 | return data |
127 | end | 128 | end |
@@ -133,14 +134,14 @@ function prep_menu(active_id, main_menu) | |||
133 | for i = 1, #main_menu do | 134 | for i = 1, #main_menu do |
134 | local item = main_menu[i] | 135 | local item = main_menu[i] |
135 | local active = pandoc.utils.stringify(item.id) == active_id | 136 | local active = pandoc.utils.stringify(item.id) == active_id |
136 | item.active = pandoc.MetaBool(active) | 137 | item.active = active |
137 | if active then active_item = item end | 138 | if active then active_item = item end |
138 | end | 139 | end |
139 | 140 | ||
140 | return pandoc.MetaMap({ | 141 | return { |
141 | items = main_menu:filter(function(item) return not item.hidden or item.active end), | 142 | items = main_menu:filter(function(item) return not item.hidden or item.active end), |
142 | active = active_item, | 143 | active = active_item, |
143 | }) | 144 | } |
144 | end | 145 | end |
145 | 146 | ||
146 | function process_pages(global, pages_by_id) | 147 | function process_pages(global, pages_by_id) |
@@ -167,33 +168,23 @@ function process_pages(global, pages_by_id) | |||
167 | end | 168 | end |
168 | end) | 169 | end) |
169 | 170 | ||
170 | local pages_categorized = pages_list:filter(function(p) return p.category ~= nil end) | 171 | local pages_by_category = pages_list:filter(function(p) return p.category ~= nil end) |
171 | 172 | pages_by_category = group_by(pages_by_category, function(p) return p.category.id end, | |
172 | local pages_by_category = group_by(pages_categorized, function(p) return p.category.id end) | 173 | function(data, _, p) |
173 | pages_by_category = | 174 | if not data then |
174 | table_to_list(pages_by_category, function(i1, i2) return i1.key < i2.key end) | 175 | local l = pandoc.List() |
175 | 176 | l:insert(p) | |
176 | local pages_data = pandoc.MetaMap({ | 177 | return {name = pandoc.utils.stringify(p.category.name), pages = l} |
177 | all = pages_list, | ||
178 | by_id = pages_by_id, | ||
179 | by_category = pages_by_category, | ||
180 | }) | ||
181 | |||
182 | local categories_data = group_by(pages_categorized, | ||
183 | function(p) return p.category and p.category.id end, function(stats, _, p) | ||
184 | if not stats then | ||
185 | return {name = p.category.name, count = 1} | ||
186 | else | 178 | else |
187 | stats.count = stats.count + 1 | 179 | data.pages:insert(p) |
188 | end | 180 | end |
189 | end) | 181 | end) |
190 | categories_data = table_to_list(categories_data) | 182 | pages_by_category = table_to_list(pages_by_category, false, |
183 | function(i1, i2) return i1.name < i2.name end) | ||
191 | 184 | ||
192 | for i = 1, #categories_data do | 185 | local pages_data = {all = pages_list, by_id = pages_by_id, by_category = pages_by_category} |
193 | categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) | ||
194 | end | ||
195 | 186 | ||
196 | return pages_data, categories_data | 187 | return pages_data |
197 | end | 188 | end |
198 | 189 | ||
199 | function process(global, meta) | 190 | function process(global, meta) |
@@ -222,9 +213,7 @@ function process(global, meta) | |||
222 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) | 213 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) |
223 | end | 214 | end |
224 | 215 | ||
225 | local pages, local_categories = process_pages(global, meta.pages) | 216 | meta.pages = process_pages(global, meta.pages) |
226 | meta.pages = pages | ||
227 | meta.local_categories = local_categories | ||
228 | 217 | ||
229 | return meta | 218 | return meta |
230 | end | 219 | end |