Module:TitleCase
From Nookipedia, the Animal Crossing wiki
Revision as of 14:05, November 23, 2022 by PanchamBro (talk | contribs)
Documentation for this module may be created at Module:TitleCase/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
function split(str, pattern)
local out = {}
for m in string.gmatch(str, "[^" .. pattern .. "]+") do
table.insert(out, m)
end
return out
end
function isEmpty(s)
return s == nil or s == ''
end
function search(category, substrings)
for k,v in pairs(substrings) do
if string.find(category, v) then
return true
end
end
return false
end
-- From Lua-User Wiki: http://lua-users.org/wiki/SciteTitleCase
function titlecase(str)
local titleName = ''
local buf = {}
local inWord = false
for i = 1, #str do
local c = string.sub(str, i, i)
if inWord then
table.insert(buf, c)
if string.find(c, '%s') or string.find(c, '-') then
inWord = false
end
else
table.insert(buf, string.upper(c))
inWord = true
end
end
return table.concat(buf)
end
function p.main(frame)
local args = getArgs(frame)
local name = args['1'] or ''
local titleCase = titlecase(name)
return p.keepLowercase(titleCase)
end
function p.keepLowercase(str)
local small = {"Al", "And", "An", "As", "At", "By", "En", "De", "Di", "For", "If", "In", "N", "Nor", "O", "Of", "On", "Or", "Only", "Over", "Per", "So", "Some", "That", "Than", "The", "To", "Upon", "Vs", "Versus", "Via", "Where", "When", "With", "Without", "Yet"}
if search(str:gsub("^%l", string.upper), small) == true then
for k,v in pairs(small) do
if string.find(str, "%p" .. v .. "%p") then
str = str:gsub("%p" .. v .. "%p", string.lower)
elseif string.find(str, "%s" .. v .. "%s") then
str = str:gsub("%s" .. v .. "%s", string.lower)
elseif string.find(str, "%s" .. v .. "%p") then
str = str:gsub("%s" .. v .. "%p", string.lower)
end
end
end
return str
end
return p