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 --- scripts/metadata_filter.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'scripts') 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