Difference between revisions of "Module:Sandbox"

From Nookipedia, the Animal Crossing wiki
m
(restructuring module: now should catch instances where a particular name should be lowercased)
Line 14: Line 14:
 
end
 
end
  
function keepLowercase(str)
+
function search(category, substrings)
local small = {
+
    for k,v in pairs(substrings) do
["Al"] = "al",
+
        if string.find(category, v) then
["And"] = "and",
+
            return true
["An"] = "an",
+
        end
["As"] = "as",
+
    end
["At"] = "at",
+
     return false
["By"] = "by",
 
["En"] = "en",
 
["De"] = "de",
 
["Di"] = "di",
 
["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
 
end
  
Line 86: Line 43:
 
end
 
end
  
function p.checkWords(name)
+
function p.main(frame)
     local words = split( name, " " )
+
     local args        = getArgs(frame)
     local word = ""
+
local name         = args['1'] or ''
     local wordList = ""
+
     local titleCase    = titlecase(name)
    local fullName = ""
+
     return p.keepLowercase(titleCase)
    for k, w in ipairs(words) do
+
end
        finalWord = keepLowercase(w:gsub("^%s*(.-)%s*$", "%1")) .. " "
+
 
        wordList = wordList .. string.gsub(finalWord, "\s+$", "")
+
function p.keepLowercase(str)
    end
+
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
wordList = wordList:gsub("%s+$", "")
+
for k,v in pairs(small) do
local root = mw.html.create()
+
        if string.find(str, "%p" .. v .. "%p") then
root:wikitext(wordList)
+
            str = str:gsub("%p" .. v .. "%p", string.lower)
+
            elseif string.find(str, "%s" .. v .. "%s") then
return root
+
            str = str:gsub("%s" .. v .. "%s", string.lower)
 +
        end
 +
    end
 +
end
 +
return str
 
end
 
end
  
 
return p
 
return p

Revision as of 13:53, November 23, 2022

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 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)
        	end
    	end
	end
	return str
end

return p