diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/metadata_filter.lua | 108 |
1 files changed, 62 insertions, 46 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index d6379b9..4abb716 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
| @@ -1,13 +1,9 @@ | |||
| 1 | function format_date(date) | 1 | function format_date(date) |
| 2 | if date == nil then | 2 | if date == nil then return date end |
| 3 | return date | ||
| 4 | end | ||
| 5 | 3 | ||
| 6 | date = pandoc.utils.normalize_date(pandoc.utils.stringify(date)) | 4 | date = pandoc.utils.normalize_date(pandoc.utils.stringify(date)) |
| 7 | 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)") |
| 8 | if year == nil then | 6 | if year == nil then return nil end |
| 9 | return nil | ||
| 10 | end | ||
| 11 | 7 | ||
| 12 | local time = os.time({ | 8 | local time = os.time({ |
| 13 | year = tonumber(year), | 9 | year = tonumber(year), |
| @@ -26,15 +22,10 @@ function table_to_list(t, cmp) | |||
| 26 | local l = pandoc.List() | 22 | local l = pandoc.List() |
| 27 | 23 | ||
| 28 | for key, value in pairs(t) do | 24 | for key, value in pairs(t) do |
| 29 | l:insert(pandoc.MetaMap({ | 25 | l:insert(pandoc.MetaMap({key = key, value = value})) |
| 30 | key = key, | ||
| 31 | value = value | ||
| 32 | })) | ||
| 33 | end | 26 | end |
| 34 | 27 | ||
| 35 | l:sort(cmp or function(i1, i2) | 28 | l:sort(cmp or function(i1, i2) return i1.key < i2.key end) |
| 36 | return i1.key < i2.key | ||
| 37 | end) | ||
| 38 | 29 | ||
| 39 | return l | 30 | return l |
| 40 | end | 31 | end |
| @@ -56,9 +47,7 @@ function group_by(l, field, insert) | |||
| 56 | 47 | ||
| 57 | if f ~= nil then | 48 | if f ~= nil then |
| 58 | local out = insert(groups[f], f, item) | 49 | local out = insert(groups[f], f, item) |
| 59 | if out ~= nil then | 50 | if out ~= nil then groups[f] = out end |
| 60 | groups[f] = out | ||
| 61 | end | ||
| 62 | end | 51 | end |
| 63 | end | 52 | end |
| 64 | 53 | ||
| @@ -68,9 +57,7 @@ end | |||
| 68 | function splitstr(input, sep) | 57 | function splitstr(input, sep) |
| 69 | sep = sep or "%s" | 58 | sep = sep or "%s" |
| 70 | local t = {} | 59 | local t = {} |
| 71 | for str in input:gmatch("([^" .. sep .. "]+)") do | 60 | for str in input:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end |
| 72 | table.insert(t, str) | ||
| 73 | end | ||
| 74 | return t | 61 | return t |
| 75 | end | 62 | end |
| 76 | 63 | ||
| @@ -98,9 +85,7 @@ function relative_to(dir, target) | |||
| 98 | path = path .. (path == "" and "" or "/") .. target[i] | 85 | path = path .. (path == "" and "" or "/") .. target[i] |
| 99 | end | 86 | end |
| 100 | elseif #dir > #target then | 87 | elseif #dir > #target then |
| 101 | for i = #target + 1, #dir do | 88 | for _ = #target + 1, #dir do path = "../" .. path end |
| 102 | path = "../" .. path | ||
| 103 | end | ||
| 104 | end | 89 | end |
| 105 | 90 | ||
| 106 | return path | 91 | return path |
| @@ -123,6 +108,16 @@ function resolve_url(page_type, site_url, content_dir, base_dir, cur_file) | |||
| 123 | }) | 108 | }) |
| 124 | end | 109 | end |
| 125 | 110 | ||
| 111 | function resolve_layout(layout) | ||
| 112 | if layout then | ||
| 113 | layout = pandoc.utils.stringify(layout) | ||
| 114 | return pandoc.MetaMap({ | ||
| 115 | id = pandoc.MetaString(layout), | ||
| 116 | ["is_" .. layout] = pandoc.MetaBool(true) | ||
| 117 | }) | ||
| 118 | end | ||
| 119 | end | ||
| 120 | |||
| 126 | function resolve_section(abs_url) | 121 | function resolve_section(abs_url) |
| 127 | local section = abs_url:match("^/(.-)[/.]") or "index" | 122 | local section = abs_url:match("^/(.-)[/.]") or "index" |
| 128 | return pandoc.MetaMap({ | 123 | return pandoc.MetaMap({ |
| @@ -131,25 +126,48 @@ function resolve_section(abs_url) | |||
| 131 | }) | 126 | }) |
| 132 | end | 127 | end |
| 133 | 128 | ||
| 134 | function organize_subpages(site_url, content_dir, base_dir, pages, categories) | 129 | function resolve_category(categories, category) |
| 135 | local grouped_pages = group_by(pages, function(p) | 130 | if categories and category then |
| 136 | return not p.date | 131 | category = pandoc.utils.stringify(category) |
| 132 | return pandoc.MetaMap({ | ||
| 133 | id = pandoc.MetaString(category), | ||
| 134 | name = pandoc.MetaString(categories[category] or category) | ||
| 135 | }) | ||
| 136 | end | ||
| 137 | end | ||
| 138 | |||
| 139 | function create_main_menu_state(section, main_menu) | ||
| 140 | for i = 1, #main_menu do | ||
| 141 | local item = main_menu[i] | ||
| 142 | local active = pandoc.utils.stringify(item.id) == section.id | ||
| 143 | item.active = pandoc.MetaBool(active) | ||
| 144 | end | ||
| 145 | |||
| 146 | return main_menu:filter(function(item) | ||
| 147 | return not item.hidden or item.active | ||
| 137 | end) | 148 | end) |
| 149 | end | ||
| 150 | |||
| 151 | function organize_subpages(pages) | ||
| 152 | local grouped_pages = group_by(pages, function(p) return not p.date end) | ||
| 138 | 153 | ||
| 139 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) | 154 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) |
| 140 | pages_undated:sort(function(p1, p2) | 155 | pages_undated:sort(function(p1, p2) |
| 141 | return pandoc.utils.stringify(p1.title) < pandoc.utils.stringify(p2.title) | 156 | return pandoc.utils.stringify(p1.title) < |
| 157 | pandoc.utils.stringify(p2.title) | ||
| 142 | end) | 158 | end) |
| 143 | 159 | ||
| 144 | local pages_dated = grouped_pages[false] or pandoc.MetaList({}) | 160 | local pages_dated = grouped_pages[false] or pandoc.MetaList({}) |
| 145 | pages_dated:sort(function(p1, p2) | 161 | pages_dated:sort(function(p1, p2) |
| 146 | return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > pandoc.utils.stringify(p2.date.yyyy_mm_dd) | 162 | return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > |
| 163 | pandoc.utils.stringify(p2.date.yyyy_mm_dd) | ||
| 147 | end) | 164 | end) |
| 148 | 165 | ||
| 149 | local pages_by_year = group_by(pages_dated, function(p) | 166 | local pages_by_year = group_by(pages_dated, function(p) |
| 150 | return pandoc.utils.stringify(p.date.yyyy) | 167 | return pandoc.utils.stringify(p.date.yyyy) |
| 151 | end) | 168 | end) |
| 152 | pages_by_year = pandoc.MetaList(table_to_list(pages_by_year, function(i1, i2) | 169 | pages_by_year = pandoc.MetaList(table_to_list(pages_by_year, |
| 170 | function(i1, i2) | ||
| 153 | return i1.key > i2.key | 171 | return i1.key > i2.key |
| 154 | end)) | 172 | end)) |
| 155 | 173 | ||
| @@ -161,13 +179,10 @@ function organize_subpages(site_url, content_dir, base_dir, pages, categories) | |||
| 161 | }) | 179 | }) |
| 162 | 180 | ||
| 163 | local categories_data = group_by(pages_dated, function(p) | 181 | local categories_data = group_by(pages_dated, function(p) |
| 164 | return p.category and pandoc.utils.stringify(p.category) | 182 | return p.category and pandoc.utils.stringify(p.category.id) |
| 165 | end, function(stats, category) | 183 | end, function(stats, _, p) |
| 166 | if not stats then | 184 | if not stats then |
| 167 | return { | 185 | return {name = pandoc.MetaString(p.category.name), count = 1} |
| 168 | name = pandoc.MetaString(categories[category] or category), | ||
| 169 | count = 1 | ||
| 170 | } | ||
| 171 | else | 186 | else |
| 172 | stats.count = stats.count + 1 | 187 | stats.count = stats.count + 1 |
| 173 | end | 188 | end |
| @@ -175,7 +190,8 @@ function organize_subpages(site_url, content_dir, base_dir, pages, categories) | |||
| 175 | categories_data = pandoc.MetaList(table_to_list(categories_data)) | 190 | categories_data = pandoc.MetaList(table_to_list(categories_data)) |
| 176 | 191 | ||
| 177 | for i = 1, #categories_data do | 192 | for i = 1, #categories_data do |
| 178 | categories_data[i].value.count = pandoc.MetaString(("%d"):format(categories_data[i].value.count)) | 193 | categories_data[i].value.count = |
| 194 | pandoc.MetaString(("%d"):format(categories_data[i].value.count)) | ||
| 179 | end | 195 | end |
| 180 | 196 | ||
| 181 | return pages_data, categories_data | 197 | return pages_data, categories_data |
| @@ -188,6 +204,8 @@ function Meta(meta) | |||
| 188 | meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1") | 204 | meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1") |
| 189 | meta.page_type = meta.page_type or "page" | 205 | meta.page_type = meta.page_type or "page" |
| 190 | 206 | ||
| 207 | meta.layout = resolve_layout(meta.layout) | ||
| 208 | |||
| 191 | meta.date = format_date(meta.date) | 209 | meta.date = format_date(meta.date) |
| 192 | if meta.last_update ~= nil then | 210 | if meta.last_update ~= nil then |
| 193 | meta.last_update = format_date(meta.last_update) | 211 | meta.last_update = format_date(meta.last_update) |
| @@ -197,28 +215,26 @@ function Meta(meta) | |||
| 197 | 215 | ||
| 198 | if meta.page_type == "feed" then | 216 | if meta.page_type == "feed" then |
| 199 | meta.page = pandoc.MetaMap({ | 217 | meta.page = pandoc.MetaMap({ |
| 200 | url = resolve_url("page", meta.site.url, meta.content_dir, meta.base_dir, meta.file) | 218 | url = resolve_url("page", meta.site.url, meta.content_dir, |
| 219 | meta.base_dir, meta.file) | ||
| 201 | }) | 220 | }) |
| 202 | end | 221 | end |
| 203 | 222 | ||
| 204 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir, meta.base_dir, meta.file) | 223 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir, |
| 224 | meta.base_dir, meta.file) | ||
| 205 | meta.section = resolve_section(meta.url.abs) | 225 | meta.section = resolve_section(meta.url.abs) |
| 206 | meta.categories = meta.categories[meta.section.id] | 226 | meta.category = resolve_category(meta.categories[meta.section.id], |
| 227 | meta.category) | ||
| 228 | meta.categories = nil | ||
| 207 | 229 | ||
| 208 | if meta.menus and meta.menus.main then | 230 | if meta.menus and meta.menus.main then |
| 209 | for i = 1, #meta.menus.main do | 231 | meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) |
| 210 | local item = meta.menus.main[i] | ||
| 211 | item.active = pandoc.MetaBool(pandoc.utils.stringify(item.id) == meta.section.id) | ||
| 212 | end | ||
| 213 | end | 232 | end |
| 214 | 233 | ||
| 215 | if meta.pages then | 234 | if meta.pages then |
| 216 | local pages, categories = organize_subpages(meta.site.url, meta.content_dir, meta.base_dir, meta.pages, | 235 | local pages, categories = organize_subpages(meta.pages) |
| 217 | meta.categories) | ||
| 218 | meta.pages = pages | 236 | meta.pages = pages |
| 219 | meta.categories = categories | 237 | meta.categories = categories |
| 220 | else | ||
| 221 | meta.categories = nil | ||
| 222 | end | 238 | end |
| 223 | 239 | ||
| 224 | return meta | 240 | return meta |
