From b26abc90439e5e1d25176ffa80f7ba0b34b3fa43 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Wed, 6 Jan 2021 14:49:43 +0100 Subject: Improved subpage organization code in metadata filter --- scripts/build_content.sh | 21 +++++-- scripts/metadata_filter.lua | 134 ++++++++++++++++++++++---------------------- templates/base.html | 1 + templates/feed.xml | 58 +++++++++---------- templates/list.html | 18 +++--- 5 files changed, 118 insertions(+), 114 deletions(-) diff --git a/scripts/build_content.sh b/scripts/build_content.sh index c88ea86..0b2ce2e 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh @@ -48,7 +48,7 @@ get_metadata() { get_content() { pandoc "$2" \ -f markdown \ - -t plain \ + -t markdown \ --metadata content_dir="$CONTENT_DIR" \ --metadata base_file="$1" \ --metadata file="$2" \ @@ -131,13 +131,11 @@ handle () { --template "${TEMPLATES_DIR}feed.xml" \ -o "${target_file%.html}.xml" \ --metadata content_dir="$CONTENT_DIR" \ - --metadata base_file="$1" \ --metadata file="$1" \ - --metadata type=feed \ + --metadata page_type=feed \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ - --lua-filter scripts/metadata_filter.lua \ - $FILTERS + --lua-filter scripts/metadata_filter.lua fi echo -e "\033[0;90m[COMPILE ]\033[0m Creating page" @@ -149,13 +147,24 @@ handle () { --template "${TEMPLATES_DIR}base.html" \ -o "$target_file" \ --metadata content_dir="$CONTENT_DIR" \ - --metadata base_file="$1" \ --metadata file="$1" \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ --lua-filter scripts/metadata_filter.lua \ $FILTERS + # echo "$(pandoc "$1" \ + # -f markdown \ + # -t plain \ + # --no-highlight \ + # --template scripts/metadata_tpl.json \ + # --metadata content_dir="$CONTENT_DIR" \ + # --metadata file="$1" \ + # --metadata-file metadata/metadata.yaml \ + # --metadata-file "$meta_file" \ + # --lua-filter scripts/metadata_filter.lua \ + # $FILTERS)" + rm "$meta_file" # echo "$included_metadata" diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index f31bc1f..d6379b9 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -23,7 +23,7 @@ function format_date(date) end function table_to_list(t, cmp) - local l = pandoc.MetaList({}) + local l = pandoc.List() for key, value in pairs(t) do l:insert(pandoc.MetaMap({ @@ -39,6 +39,32 @@ function table_to_list(t, cmp) 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 = {} @@ -106,77 +132,50 @@ function resolve_section(abs_url) end function organize_subpages(site_url, content_dir, base_dir, pages, categories) - local categories_data = pandoc.MetaList({}) - - pages:sort(function(p1, p2) - if p1.date and p2.date then - return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > pandoc.utils.stringify(p2.date.yyyy_mm_dd) - elseif p2.date then - return true - elseif p1.date then - return false - else - return pandoc.utils.stringify(p1.title) < pandoc.utils.stringify(p2.title) - end + local grouped_pages = group_by(pages, function(p) + return not p.date end) - local pages_data = pandoc.MetaMap({ - all = pages, - by_year = pandoc.MetaList({}), - last_update = nil - }) - - if pages then - local pages_by_year_map = {} - local categories_map = {} - - for i = 1, #pages do - local page = pages[i] - - if page.date then - local yyyy = pandoc.utils.stringify(page.date.yyyy) - local pages_by_yyyy = pages_by_year_map[yyyy] - - if not pages_by_yyyy then - pages_by_yyyy = pandoc.MetaList(pandoc.List()) - pages_by_year_map[yyyy] = pages_by_yyyy - end - - pages_by_yyyy:insert(page) - end - - if page.category and categories then - local category = pandoc.utils.stringify(page.category) - - if categories[category] then - local current_category = categories_map[category] - - if not current_category then - current_category = pandoc.MetaMap({ - name = pandoc.MetaString(categories[category]), - count = 0 - }) - categories_map[category] = current_category - 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) - current_category.count = current_category.count + 1 - end - end - 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) - pages_data.by_year = table_to_list(pages_by_year_map, function(i1, i2) - return i1.key > i2.key - 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)) - categories_data = table_to_list(categories_map) + 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 + }) - for _, item in ipairs(categories_data) do - item.value.count = pandoc.MetaString(("%d"):format(item.value.count)) + local categories_data = group_by(pages_dated, function(p) + return p.category and pandoc.utils.stringify(p.category) + end, function(stats, category) + if not stats then + return { + name = pandoc.MetaString(categories[category] or category), + count = 1 + } + else + stats.count = stats.count + 1 end + end) + categories_data = pandoc.MetaList(table_to_list(categories_data)) - if #pages_data.by_year ~= 0 then - pages_data.last_update = pages_data.by_year[1].value[1].last_update - end + 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 @@ -185,8 +184,9 @@ 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.type = meta.type or "page" + meta.page_type = meta.page_type or "page" meta.date = format_date(meta.date) if meta.last_update ~= nil then @@ -195,13 +195,13 @@ function Meta(meta) meta.last_update = meta.date end - if meta.type == "feed" then + 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.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] diff --git a/templates/base.html b/templates/base.html index 3e079a1..b4a2953 100644 --- a/templates/base.html +++ b/templates/base.html @@ -6,6 +6,7 @@ + $for(author)$ diff --git a/templates/feed.xml b/templates/feed.xml index 083049e..a968205 100644 --- a/templates/feed.xml +++ b/templates/feed.xml @@ -14,37 +14,33 @@ $endfor$ - $for(pages)$ - $for(it.all)$ - $if(it.date)$ - - $it.url.full$ - <![CDATA[$it.title$]]> - $if(it.author)$ - $for(it.author)$ - - $it.name$ - $if(it.email)$ - $it.email$ - $endif$ - - $endfor$ - $else$ - $for(author)$ - - $author.name$ - $if(author.email)$ - $author.email$ - $endif$ - - $endfor$ - $endif$ - - $it.date.rfc3339$ - $it.last_update.rfc3339$ - - + $for(pages.all_dated)$ + + $it.url.full$ + <![CDATA[$it.title$]]> + $if(it.author)$ + $for(it.author)$ + + $it.name$ + $if(it.email)$ + $it.email$ + $endif$ + + $endfor$ + $else$ + $for(author)$ + + $author.name$ + $if(author.email)$ + $author.email$ + $endif$ + + $endfor$ $endif$ - $endfor$ + + $it.date.rfc3339$ + $it.last_update.rfc3339$ + + $endfor$ diff --git a/templates/list.html b/templates/list.html index 2d663e0..d74681c 100644 --- a/templates/list.html +++ b/templates/list.html @@ -8,15 +8,13 @@ $if(categories)$ $endif$ -$for(pages)$ - $for(it.by_year)$ - - $endfor$ +$for(pages.by_year)$ + $endfor$ -- cgit v1.2.3-70-g09d2