Difference between revisions of "Module:TitleCase"

From Nookipedia, the Animal Crossing wiki
m
(Added blue and pink pj bottoms for HHD. Rearranged items.)
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
local getArgs = require('Module:Arguments').getArgs
 
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)
 
function search(category, substrings)
Line 35: Line 23:
 
inWord = false
 
inWord = false
 
end
 
end
 +
elseif string.find(c, '%(') or string.find(c, '\'') then
 +
table.insert(buf, c)
 +
inWord = false
 
else
 
else
 
table.insert(buf, string.upper(c))
 
table.insert(buf, string.upper(c))
Line 51: Line 42:
  
 
function p.keepLowercase(str)
 
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"}
+
local small = {"A", "Al", "And", "An", "As", "At", "By", "En", "De", "Di", "For", "If", "In", "IQue", "IQueGBA", "N", "No", "Nor", "O", "Of", "On", "Or", "Only", "Over", "Per", "So", "Some", "That", "Than", "The", "To", "Upon", "Vs", "Versus", "Via", "Where", "With", "Without", "Yet"}
 
if search(str:gsub("^%l", string.upper), small) == true then
 
if search(str:gsub("^%l", string.upper), small) == true then
 
for k,v in pairs(small) do
 
for k,v in pairs(small) do
Line 62: Line 53:
 
         end
 
         end
 
     end
 
     end
 +
end
 +
-- Special fixes
 +
if str == "Grand Q. a. Birdwing Model" then
 +
str = "Grand Q. A. Birdwing Model"
 +
elseif str == "Retro a-Line Dress" then
 +
str = "Retro A-Line Dress"
 +
elseif str == "Ultra no-Show Socks" then
 +
str = "Ultra No-Show Socks"
 +
elseif str == "Custom Fencing in A Flash" then
 +
str = "Custom Fencing in a Flash"
 +
elseif str == "What Was This Again? (Book)" then
 +
str = "What Was This Again? (book)"
 +
elseif str == "What Was This Again? (Planner)" then
 +
str = "What Was This Again? (planner)"
 +
elseif str == "What Was This Again? (Pouch)" then
 +
str = "What Was This Again? (pouch)"
 +
elseif str == "What Was This Again? (Present)" then
 +
str = "What Was This Again? (present)"
 +
elseif str == "Bunny o. Balloon" then
 +
str = "Bunny O. Balloon"
 +
elseif str == "Heart o. Balloon" then
 +
str = "Heart O. Balloon"
 +
elseif str == "Blue Pj Shirt" then
 +
str = "Blue PJ Shirt"
 +
elseif str == "Pink Pj Shirt" then
 +
str = "Pink PJ Shirt"
 +
elseif str == "Blue Pj Bottoms" then
 +
str = "Blue PJ Bottoms"
 +
elseif str == "Pink Pj Bottoms" then
 +
str = "Pink PJ Bottoms"
 
end
 
end
 
return str
 
return str

Latest revision as of 18:54, March 14, 2023

Documentation for this module may be created at Module:TitleCase/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

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
		elseif string.find(c, '%(') or string.find(c, '\'') then
			table.insert(buf, c)
			inWord = false
		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 = {"A", "Al", "And", "An", "As", "At", "By", "En", "De", "Di", "For", "If", "In", "IQue", "IQueGBA", "N", "No", "Nor", "O", "Of", "On", "Or", "Only", "Over", "Per", "So", "Some", "That", "Than", "The", "To", "Upon", "Vs", "Versus", "Via", "Where", "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
	-- Special fixes
	if str == "Grand Q. a. Birdwing Model" then
		str = "Grand Q. A. Birdwing Model"
	elseif str == "Retro a-Line Dress" then
		str = "Retro A-Line Dress"
	elseif str == "Ultra no-Show Socks" then
		str = "Ultra No-Show Socks"
	elseif str == "Custom Fencing in A Flash" then
		str = "Custom Fencing in a Flash"
	elseif str == "What Was This Again? (Book)" then
		str = "What Was This Again? (book)"
	elseif str == "What Was This Again? (Planner)" then
		str = "What Was This Again? (planner)"
	elseif str == "What Was This Again? (Pouch)" then
		str = "What Was This Again? (pouch)"
	elseif str == "What Was This Again? (Present)" then
		str = "What Was This Again? (present)"
	elseif str == "Bunny o. Balloon" then
		str = "Bunny O. Balloon"
	elseif str == "Heart o. Balloon" then
		str = "Heart O. Balloon"
	elseif str == "Blue Pj Shirt" then
		str = "Blue PJ Shirt"
	elseif str == "Pink Pj Shirt" then
		str = "Pink PJ Shirt"
	elseif str == "Blue Pj Bottoms" then
		str = "Blue PJ Bottoms"
	elseif str == "Pink Pj Bottoms" then
		str = "Pink PJ Bottoms"
	end
	return str
end

return p