Module:Sandbox

From Nookipedia, the Animal Crossing wiki
Revision as of 17:42, November 22, 2022 by PanchamBro (talk | contribs) (removing parameter to lower words)

Module documentation (view)


Usage

This is the general sandbox page to test out Lua modules. The module should not be implemented in a template.


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 keepLowercase(str)
	local small = {
		["And"] = "and",
		["An"] = "an",
		["As"] = "as",
		["At"] = "at",
		["By"] = "by",
		["En"] = "en",
		["De"] = "de",
		["For"] = "for",
		["If"] = "if",
		["In"] = "in",
		["N"] = "n",
		["Nor"] = "nor",
		["Of"] = "of",
		["On"] = "on",
		["Or"] = "or",
		["Only"] = "only",
		["Over"] = "over",
		["Per"] = "per",
		["So"] = "so",
		["Some"] = "some",
		["That"] = "that",
		["Than"] = "than",
		["The"] = "the",
		["To"] = "to",
		["Upon"] = "upon",
		["Vs"] = "vs",
		["Versus"] = "versus",
		["Via"] = "via",
		["Where"] = "where",
		["When"] = "when",
		["With"] = "with",
		["Without"] = "without",
		["Yet"] = "yet"
	}
	if small[str:gsub("^%l", string.upper)] then
		return small[str:gsub("^%l", string.upper)]
	else
		return str:gsub("^%l", string.upper)
	end
end

function p.main(frame)
    local args         = getArgs(frame)
	local name         = args['1'] or ''
    local titleCase    = titlecase(name)
    return p.checkWords(titleCase)
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.checkWords(name)
    local words = split( name, " " )
    local word = ""
    local wordList = ""
    local fullName = ""
    for k, w in ipairs(words) do
        finalWord = keepLowercase(w:gsub("^%s*(.-)%s*$", "%1")) .. " "
        wordList = wordList .. string.gsub(finalWord, "\s+$", "")
    end
	
	wordList = wordList:gsub("%s+$", "")
	local root = mw.html.create()
	root:wikitext(wordList)
	
	return root
end

return p