From 4df33046d142554c16a8dba9bfcc03c4be157dd9 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Thu, 27 May 2021 20:53:44 +0200 Subject: Automatic titlecase for page titles --- content/9thPK7O3xn/index.md | 2 -- scripts/metadata_filter.lua | 20 ++++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/content/9thPK7O3xn/index.md b/content/9thPK7O3xn/index.md index fb6160a..47e789b 100644 --- a/content/9thPK7O3xn/index.md +++ b/content/9thPK7O3xn/index.md @@ -6,5 +6,3 @@ create_feed: true Welcome to the personal section of my website! Here you will find content that doesn't belong in the public sections of my website, such as test pages for development purposes or content that's too personal in nature. - -Please avoid sharing anything from here with larger audiences. diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index ebbd6d4..6c3817a 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -146,7 +146,7 @@ function process_pages(global, pages_by_id) pages_list:sort(function(p1, p2) if p1.position then if p2.position then - return p1.position < p2.position + return tonumber(p1.position) < tonumber(p2.position) else return true end @@ -197,12 +197,28 @@ function pages_last_update(all_pages) return last_update end +local titlecase_exceptions = pandoc.List({ + "a", "an", "and", "as", "at", "but", "by", "en", "for", "if", "in", "nor", "of", "on", "or", + "per", "the", "to", "v%.?", "vs%.?", "via", +}) + +function titlecase(str) + return str:gsub("(%S)(%S*)(%s*)", function(a, b, space) + local word = a .. b + local is_exception = word:match("%u") or + titlecase_exceptions:find_if( + function(exception) return word:match("^" .. exception .. "$") end) + if is_exception then return word .. space end + return string.upper(a) .. b .. space + end) +end + function process(global, meta) meta.namespace = resolve_namespace(meta.namespace) meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") meta.layout = resolve_layout(meta.layout) meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) - meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" + meta.title = (meta.title and titlecase(pandoc.utils.stringify(meta.title))) or "" if meta.position then meta.position = pandoc.utils.stringify(meta.position) end -- cgit v1.2.3-54-g00ecf