diff options
author | Volpeon <git@volpeon.ink> | 2021-05-11 08:41:47 +0200 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-05-11 08:41:47 +0200 |
commit | 73760eb54cc570fc5835bbe336a26f6fd356a48c (patch) | |
tree | 9cd940a2130d0c2c8ecfcf889dd8d628bd5c8f39 /scripts | |
parent | Makefile: Added watch and dev (= watch + serve) targets, added .PHONY (diff) | |
download | volpeon.ink-73760eb54cc570fc5835bbe336a26f6fd356a48c.tar.gz volpeon.ink-73760eb54cc570fc5835bbe336a26f6fd356a48c.tar.bz2 volpeon.ink-73760eb54cc570fc5835bbe336a26f6fd356a48c.zip |
Minor improvements in metadata processing script
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/metadata_filter.lua | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 21f7759..b8a9059 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
@@ -1,9 +1,9 @@ | |||
1 | function format_date(date) | 1 | function format_date(date) |
2 | if date == nil then return date end | 2 | if not date then return date end |
3 | 3 | ||
4 | date = pandoc.utils.normalize_date(pandoc.utils.stringify(date)) | 4 | date = pandoc.utils.normalize_date(pandoc.utils.stringify(date)) |
5 | local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)") | 5 | local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)") |
6 | if year == nil 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 pandoc.MetaMap({ |
@@ -41,9 +41,9 @@ function group_by(l, field, insert) | |||
41 | local item = l[i] | 41 | local item = l[i] |
42 | local f = field(item) | 42 | local f = field(item) |
43 | 43 | ||
44 | if f ~= nil then | 44 | if f then |
45 | local out = insert(groups[f], f, item) | 45 | local out = insert(groups[f], f, item) |
46 | if out ~= nil then groups[f] = out end | 46 | if out then groups[f] = out end |
47 | end | 47 | end |
48 | end | 48 | end |
49 | 49 | ||
@@ -86,7 +86,7 @@ function relative_to(dir, target) | |||
86 | end | 86 | end |
87 | 87 | ||
88 | function make_absolute(rel, base) | 88 | function make_absolute(rel, base) |
89 | return rel:find("^/") ~= nil and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel | 89 | return rel:find("^/") and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel |
90 | end | 90 | end |
91 | 91 | ||
92 | function resolve_url(site_url, ref_file, target_file) | 92 | function resolve_url(site_url, ref_file, target_file) |
@@ -100,7 +100,7 @@ function resolve_url(site_url, ref_file, target_file) | |||
100 | end | 100 | end |
101 | 101 | ||
102 | function resolve_layout(layout) | 102 | function resolve_layout(layout) |
103 | if layout ~= nil then | 103 | if layout then |
104 | layout = pandoc.utils.stringify(layout) | 104 | layout = pandoc.utils.stringify(layout) |
105 | return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) | 105 | return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) |
106 | end | 106 | end |
@@ -119,7 +119,7 @@ function resolve_namespace(namespace) | |||
119 | end | 119 | end |
120 | 120 | ||
121 | function resolve_category(categories, category) | 121 | function resolve_category(categories, category) |
122 | if categories ~= nil and category ~= nil then | 122 | if categories and category then |
123 | category = pandoc.utils.stringify(category) | 123 | category = pandoc.utils.stringify(category) |
124 | data = pandoc.MetaMap(categories[category]) | 124 | data = pandoc.MetaMap(categories[category]) |
125 | data.id = category | 125 | data.id = category |
@@ -144,7 +144,7 @@ function prep_menu(active_id, main_menu) | |||
144 | end | 144 | end |
145 | 145 | ||
146 | function process_pages(global, pages_by_id) | 146 | function process_pages(global, pages_by_id) |
147 | if pages_by_id == nil then pages_by_id = {} end | 147 | if not pages_by_id then pages_by_id = {} end |
148 | 148 | ||
149 | local pages_list = pandoc.List() | 149 | local pages_list = pandoc.List() |
150 | 150 | ||
@@ -153,13 +153,25 @@ function process_pages(global, pages_by_id) | |||
153 | pages_list:insert(page) | 153 | pages_list:insert(page) |
154 | end | 154 | end |
155 | 155 | ||
156 | pages_list:sort(function(p1, p2) | ||
157 | if p1.date then | ||
158 | if p2.date then | ||
159 | return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd | ||
160 | else | ||
161 | return true | ||
162 | end | ||
163 | elseif p2.date then | ||
164 | return false | ||
165 | else | ||
166 | return p1.title < p2.title | ||
167 | end | ||
168 | end) | ||
169 | |||
156 | local pages_categorized = pages_list:filter(function(p) return p.category ~= nil end) | 170 | local pages_categorized = pages_list:filter(function(p) return p.category ~= nil end) |
157 | pages_categorized:sort(function(p1, p2) return p1.title < p2.title end) | ||
158 | 171 | ||
159 | local pages_by_category = group_by(pages_categorized, | 172 | local pages_by_category = group_by(pages_categorized, function(p) return p.category.id end) |
160 | function(p) return pandoc.utils.stringify(p.category.id) end) | 173 | pages_by_category = |
161 | pages_by_category = pandoc.MetaList(table_to_list(pages_by_category, | 174 | table_to_list(pages_by_category, function(i1, i2) return i1.key < i2.key end) |
162 | function(i1, i2) return i1.key < i2.key end)) | ||
163 | 175 | ||
164 | local pages_data = pandoc.MetaMap({ | 176 | local pages_data = pandoc.MetaMap({ |
165 | all = pages_list, | 177 | all = pages_list, |
@@ -167,16 +179,15 @@ function process_pages(global, pages_by_id) | |||
167 | by_category = pages_by_category, | 179 | by_category = pages_by_category, |
168 | }) | 180 | }) |
169 | 181 | ||
170 | local categories_data = group_by(pages_categorized, function(p) | 182 | local categories_data = group_by(pages_categorized, |
171 | return p.category and pandoc.utils.stringify(p.category.id) | 183 | function(p) return p.category and p.category.id end, function(stats, _, p) |
172 | end, function(stats, _, p) | 184 | if not stats then |
173 | if not stats then | 185 | return {name = p.category.name, count = 1} |
174 | return {name = p.category.name, count = 1} | 186 | else |
175 | else | 187 | stats.count = stats.count + 1 |
176 | stats.count = stats.count + 1 | 188 | end |
177 | end | 189 | end) |
178 | end) | 190 | categories_data = table_to_list(categories_data) |
179 | categories_data = pandoc.MetaList(table_to_list(categories_data)) | ||
180 | 191 | ||
181 | for i = 1, #categories_data do | 192 | for i = 1, #categories_data do |
182 | categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) | 193 | categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) |
@@ -190,21 +201,16 @@ function process(global, meta) | |||
190 | meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") | 201 | meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") |
191 | meta.layout = resolve_layout(meta.layout) | 202 | meta.layout = resolve_layout(meta.layout) |
192 | meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) | 203 | meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) |
204 | meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" | ||
193 | 205 | ||
194 | if meta.title ~= nil then | 206 | if meta.preview then |
195 | meta.title = pandoc.utils.stringify(meta.title) | ||
196 | else | ||
197 | meta.title = "" | ||
198 | end | ||
199 | |||
200 | if meta.preview ~= nil then | ||
201 | meta.preview = make_absolute(pandoc.utils.stringify(meta.preview), meta.file_out) | 207 | meta.preview = make_absolute(pandoc.utils.stringify(meta.preview), meta.file_out) |
202 | meta.preview = resolve_url(global.site.url, global.file_out, meta.preview) | 208 | meta.preview = resolve_url(global.site.url, global.file_out, meta.preview) |
203 | end | 209 | end |
204 | 210 | ||
205 | if meta.date ~= nil then meta.date = format_date(meta.date) end | 211 | if meta.date then meta.date = format_date(meta.date) end |
206 | 212 | ||
207 | if meta.last_update ~= nil then | 213 | if meta.last_update then |
208 | meta.last_update = format_date(meta.last_update) | 214 | meta.last_update = format_date(meta.last_update) |
209 | else | 215 | else |
210 | meta.last_update = meta.date | 216 | meta.last_update = meta.date |
@@ -212,7 +218,7 @@ function process(global, meta) | |||
212 | 218 | ||
213 | meta.category = resolve_category(global.categories[meta.namespace.root.id], meta.category) | 219 | meta.category = resolve_category(global.categories[meta.namespace.root.id], meta.category) |
214 | 220 | ||
215 | if meta.menus ~= nil and meta.menus.main ~= nil then | 221 | if meta.menus and meta.menus.main then |
216 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) | 222 | meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) |
217 | end | 223 | end |
218 | 224 | ||