Difference between revisions of "Module:Sandbox"

From Nookipedia, the Animal Crossing wiki
m
(Rm main)
 
(14 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
  
function split(str, pattern)
+
function setDefault (t, d)
    local out = {}
+
local mt = {__index = function () return d end}
    for m in string.gmatch(str, "[^" .. pattern .. "]+") do
+
setmetatable(t, mt)
      table.insert(out, m)
 
    end
 
    return out
 
 
end
 
end
  
function isEmpty(s)
+
-- Imported function from https://www.lua.org/pil/13.4.3.html --
return s == nil or s == ''
+
function copyTable(t)
 +
local t2 = {}
 +
for k,v in pairs(t) do
 +
    t2[k] = v
 +
end
 +
return t2
 
end
 
end
  
function keepLowercase(str)
+
function p.outputAvailability (data, args1, args2, args3, args4, note, text, size, nb, noimages)
local small = {
+
local function isEmpty(s)
["And"] = "and",
+
return s == nil or s == ''
["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
end
 
  
function p.main(frame)
+
local game = copyTable(data.game)
    local args        = getArgs(frame)
+
local print = '<span style="font-size:.9em;">'
local name        = args['1'] or ''
+
    local titleCase    = titlecase(name)
+
if not isEmpty(args1) then
    return p.checkWords(titleCase)
+
-- Availability with a specific villager --
end
+
if (args1 == "Villager") then
 
+
setDefault(game," NH Villager Icon.png")
function titlecase(str)
+
if isEmpty(noimages) then
local titleName = ''
+
if (args2  == "Lulu (anteater)") then
local buf = {}
+
print = print .. '[[File:Lulu ' .. game[args3]
local inWord = false
+
elseif (args2 == "Carmen (mouse)") then
for i = 1, #str do
+
print = print .. '[[File:Carmen ' .. game[args3]
local c = string.sub(str, i, i)
+
else
if inWord then
+
print = print .. '[[File:' .. args2 .. game[args3]
table.insert(buf, string.lower(c))
+
end
if string.find(c, '%s') or string.find(c, '-') then
+
if not isEmpty(size) then
inWord = false
+
print = print .. '|' .. size
 +
else
 +
print = print .. '|22px'
 +
end
 +
print = print .. '|link=|alt=]]&nbsp;'
 +
end
 +
if not isEmpty(args4) then
 +
print = print .. '[[' .. args4 .. '|' .. args2 .. ']]'
 +
else
 +
if (args2 == "Snooty") then
 +
print = print .. '[[' .. args2 .. ' (villager)'
 +
else
 +
print = print .. '[[' .. args2
 +
end
 +
if not isEmpty(text) then
 +
    print = print .. '|' .. text .. ']]'
 +
elseif (args2 == "Snooty") then
 +
print = print .. '|' .. args2 .. ']]'
 +
else
 +
print = print .. ']]'
 +
end
 
end
 
end
 
else
 
else
table.insert(buf, string.upper(c))
+
-- Availability in terms of RVs --
inWord = true
+
if (args1 == "RV") then
 +
if isEmpty(noimages) then
 +
if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][1]) then
 +
print = print .. '[[File:'.. data.availabilityRV[args2][1]
 +
else
 +
print = print .. '[[File:' .. args2 .. ' NL Villager Icon.png'
 +
end
 +
if not isEmpty(size) then
 +
print = print .. '|' .. size
 +
else
 +
if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][2]) then
 +
print = print .. '|' .. data.availabilityRV[args2][2]
 +
else
 +
print = print .. '|22px'
 +
end
 +
end
 +
print = print .. '|link=|alt=]]&nbsp;'
 +
end
 +
if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][3]) then
 +
    print = print .. '[[' .. data.availabilityRV[args2][3]
 +
    if not isEmpty(text) then
 +
    print = print .. '|' .. text .. ']]'
 +
    else
 +
    print = print .. '|' .. data.availabilityRV[args2][4] .. ']]'
 +
    end
 +
    else
 +
    print = print .. '[[' .. args2 .. '#RV'
 +
    if not isEmpty(text) then
 +
    print = print .. '|' .. text .. ']]'
 +
    else
 +
    print = print .. '|' .. args2 .. '\'s RV]]'
 +
    end
 +
end
 +
else
 +
-- Main availability function --
 +
if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][1]) and isEmpty(noimages) then
 +
    print = print .. '[[File:'.. data.availability[args1][1]
 +
if not isEmpty(size) then
 +
print = print .. '|' .. size
 +
else
 +
print = print .. '|' .. data.availability[args1][2]
 +
end
 +
print = print .. '|link=|alt=]]&nbsp;'
 +
end
 +
if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][3]) then
 +
    print = print .. '[[' .. data.availability[args1][3]
 +
    if (data.availability[args1][3] == data.availability[args1][4]) then
 +
    print = print .. ']]'
 +
    else
 +
    if not isEmpty(text) then
 +
    print = print .. '|' .. text .. ']]'
 +
    else
 +
    print = print .. '|' .. data.availability[args1][4] .. ']]'
 +
    end
 +
    end
 +
    else
 +
    if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][5]) then
 +
    if not isEmpty(text) then
 +
    print = print .. text
 +
    else
 +
    print = print .. data.availability[args1][5]
 +
    end
 +
    else
 +
    if not isEmpty(text) then
 +
    print = print .. text
 +
    else
 +
    print = print .. args1
 +
    end
 +
    end
 +
end
 +
end
 +
end
 +
-- Note function --
 +
if not isEmpty(note) then
 +
print = print .. '<small> (' .. note .. ')</small>'
 +
end
 +
print = print .. '</span>'
 +
-- Line break function --
 +
if not isEmpty(nb) then
 +
print = print .. ''
 +
else
 +
print = print .. '<br/>'
 
end
 
end
 
end
 
end
return table.concat(buf)
+
return print
 
end
 
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
 
 
local root = mw.html.create()
 
root:wikitext(wordList)
 
 
return root
 
end
 
 
 
return p
 
return p

Latest revision as of 01:20, May 16, 2023

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 setDefault (t, d)
	local mt = {__index = function () return d end}
	setmetatable(t, mt)
end

-- Imported function from https://www.lua.org/pil/13.4.3.html --
function copyTable(t)
	local t2 = {}
	for k,v in pairs(t) do
    	t2[k] = v
	end
	return t2
end

function p.outputAvailability (data, args1, args2, args3, args4, note, text, size, nb, noimages)
	local function isEmpty(s)
		return s == nil or s == ''
	end

	local game = copyTable(data.game)
	local print = '<span style="font-size:.9em;">'
	
	if not isEmpty(args1) then
		-- Availability with a specific villager --
		if (args1 == "Villager") then
			setDefault(game," NH Villager Icon.png")
			if isEmpty(noimages) then
				if (args2  == "Lulu (anteater)") then
					print = print .. '[[File:Lulu ' .. game[args3]
				elseif (args2 == "Carmen (mouse)") then
					print = print .. '[[File:Carmen ' .. game[args3]
				else
					print = print .. '[[File:' .. args2 .. game[args3]
				end
				if not isEmpty(size) then
					print = print .. '|' .. size
				else
					print = print .. '|22px'
				end
				print = print .. '|link=|alt=]]&nbsp;'
			end
			if not isEmpty(args4) then
				print = print .. '[[' .. args4 .. '|' .. args2 .. ']]'
			else
				if (args2 == "Snooty") then
					print = print .. '[[' .. args2 .. ' (villager)'
				else
					print = print .. '[[' .. args2
				end
				if not isEmpty(text) then
				    print = print .. '|' .. text .. ']]'
				elseif (args2 == "Snooty") then
					print = print .. '|' .. args2 .. ']]'
				else
					print = print .. ']]'
				end
			end
		else
			-- Availability in terms of RVs --
			if (args1 == "RV") then
				if isEmpty(noimages) then
					if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][1]) then
						print = print .. '[[File:'.. data.availabilityRV[args2][1]
					else
						print = print .. '[[File:' .. args2 .. ' NL Villager Icon.png'
					end
					if not isEmpty(size) then
						print = print .. '|' .. size
					else
						if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][2]) then
							print = print .. '|' .. data.availabilityRV[args2][2]
						else
							print = print .. '|22px'
						end
					end
					print = print .. '|link=|alt=]]&nbsp;'
				end
				if not isEmpty(data.availabilityRV[args2]) and not isEmpty(data.availabilityRV[args2][3]) then
			    	print = print .. '[[' .. data.availabilityRV[args2][3]
			    	if not isEmpty(text) then
			    		print = print .. '|' .. text .. ']]'
			    	else
			    		print = print .. '|' .. data.availabilityRV[args2][4] .. ']]'
			    	end
			    else
			    	print = print .. '[[' .. args2 .. '#RV'
			    	if not isEmpty(text) then
			    		print = print .. '|' .. text .. ']]'
			    	else
			    		print = print .. '|' .. args2 .. '\'s RV]]'
			    	end
				end
			else
				-- Main availability function --
				if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][1]) and isEmpty(noimages) then
			    	print = print .. '[[File:'.. data.availability[args1][1]
					if not isEmpty(size) then
						print = print .. '|' .. size
					else
						print = print .. '|' .. data.availability[args1][2]
					end
					print = print .. '|link=|alt=]]&nbsp;'
				end
				if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][3]) then
			    	print = print .. '[[' .. data.availability[args1][3]
			    	if (data.availability[args1][3] == data.availability[args1][4]) then
			    		print = print .. ']]'
			    	else
			    		if not isEmpty(text) then
				    		print = print .. '|' .. text .. ']]'
				    	else
				    		print = print .. '|' .. data.availability[args1][4] .. ']]'
			    		end
			    	end
			    else
			    	if not isEmpty(data.availability[args1]) and not isEmpty(data.availability[args1][5]) then
			    		if not isEmpty(text) then
				    		print = print .. text
				    	else
			    			print = print .. data.availability[args1][5]
			    		end
			    	else
			    		if not isEmpty(text) then
				    		print = print .. text
				    	else
			    			print = print .. args1
			    		end
			    	end
				end
			end
		end
		-- Note function --
		if not isEmpty(note) then
			print = print .. '<small> (' .. note .. ')</small>'
		end
		print = print .. '</span>'
		-- Line break function --
		if not isEmpty(nb) then
			print = print .. ''
		else
			print = print .. '<br/>'
		end
	end
	return print
end
return p