From ad478954cf77842364d03e1d5ee85e366e699ee7 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Thu, 7 Jan 2021 08:50:48 +0100 Subject: Improved general structure, resolve category name in metadata filter, added hideable nav items, added header for page layout --- scripts/metadata_filter.lua | 108 +++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 46 deletions(-) (limited to 'scripts') 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 @@ function format_date(date) - if date == nil then - return date - end + 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 + if year == nil then return nil end local time = os.time({ year = tonumber(year), @@ -26,15 +22,10 @@ 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 - })) + l:insert(pandoc.MetaMap({key = key, value = value})) end - l:sort(cmp or function(i1, i2) - return i1.key < i2.key - end) + l:sort(cmp or function(i1, i2) return i1.key < i2.key end) return l end @@ -56,9 +47,7 @@ function group_by(l, field, insert) if f ~= nil then local out = insert(groups[f], f, item) - if out ~= nil then - groups[f] = out - end + if out ~= nil then groups[f] = out end end end @@ -68,9 +57,7 @@ end function splitstr(input, sep) sep = sep or "%s" local t = {} - for str in input:gmatch("([^" .. sep .. "]+)") do - table.insert(t, str) - end + for str in input:gmatch("([^" .. sep .. "]+)") do table.insert(t, str) end return t end @@ -98,9 +85,7 @@ function relative_to(dir, target) path = path .. (path == "" and "" or "/") .. target[i] end elseif #dir > #target then - for i = #target + 1, #dir do - path = "../" .. path - end + for _ = #target + 1, #dir do path = "../" .. path end end return path @@ -123,6 +108,16 @@ function resolve_url(page_type, site_url, content_dir, base_dir, cur_file) }) 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({ @@ -131,25 +126,48 @@ function resolve_section(abs_url) }) end -function organize_subpages(site_url, content_dir, base_dir, pages, categories) - local grouped_pages = group_by(pages, function(p) - return not p.date +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) + 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) + 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) + pages_by_year = pandoc.MetaList(table_to_list(pages_by_year, + function(i1, i2) return i1.key > i2.key end)) @@ -161,13 +179,10 @@ function organize_subpages(site_url, content_dir, base_dir, pages, categories) }) local categories_data = group_by(pages_dated, function(p) - return p.category and pandoc.utils.stringify(p.category) - end, function(stats, category) + return p.category and pandoc.utils.stringify(p.category.id) + end, function(stats, _, p) if not stats then - return { - name = pandoc.MetaString(categories[category] or category), - count = 1 - } + return {name = pandoc.MetaString(p.category.name), count = 1} else stats.count = stats.count + 1 end @@ -175,7 +190,8 @@ function organize_subpages(site_url, content_dir, base_dir, pages, categories) 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)) + categories_data[i].value.count = + pandoc.MetaString(("%d"):format(categories_data[i].value.count)) end return pages_data, categories_data @@ -188,6 +204,8 @@ function Meta(meta) 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) @@ -197,28 +215,26 @@ function Meta(meta) 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) + 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.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.categories = meta.categories[meta.section.id] + meta.category = resolve_category(meta.categories[meta.section.id], + meta.category) + meta.categories = nil if meta.menus and meta.menus.main then - for i = 1, #meta.menus.main do - local item = meta.menus.main[i] - item.active = pandoc.MetaBool(pandoc.utils.stringify(item.id) == meta.section.id) - end + meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) end if meta.pages then - local pages, categories = organize_subpages(meta.site.url, meta.content_dir, meta.base_dir, meta.pages, - meta.categories) + local pages, categories = organize_subpages(meta.pages) meta.pages = pages meta.categories = categories - else - meta.categories = nil end return meta -- cgit v1.2.3-70-g09d2