Module:Sandbox: Difference between revisions

From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search
mNo edit summary
No edit summary
Line 1: Line 1:
local LANGUAGE_CODES = {
cs = "Czech",
da = "Danish",
de = "German",
en = "English",
es = "Spanish",
fi = "Finnish",
fr = "French",
he = "Hebrew",
hu = "Hungarian",
it = "Italian",
ja = "Japanese",
ko = "Korean",
nl = "Dutch",
no = "Norwegian",
pl = "Polish",
pt = "Portuguese",
ro = "Romanian",
ru = "Russian",
sv = "Swedish",
zh = "Chinese",
["zh-Hans"] = "Simplified Chinese",
["zh-Hant"] = "Traditional Chinese",
}
local p = {}
local p = {}


function p.hello( frame )
local function split( value )
     return "Hello, world!"
local pos = string.find(value, "=")
return string.gsub(string.sub(value, 0, pos - 1), '^%s*(.-)%s*$', '%1'),
string.gsub(string.sub(value, pos + 1, -1), '^%s*(.-)%s*$', '%1')
end
 
local function getOrCreateMember ( t, f, m )
for _, value in ipairs(t) do
if f(value) then
return value
end
end
table.insert(t, m)
return m
end
 
local function processLangs( arg )
local out = {}
local lang = {}
local word = {}
for _, parameter in ipairs(arg) do
    local name, value = split(parameter)
    if name == "l" then
    decodedValue = LANGUAGE_CODES[value]
    lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, {["l"] = decodedValue})
    elseif name == "w" then
    word = getOrCreateMember(lang, function(f) return f.w == value end, {["w"] = value})
    else
    word[name] = value
    end
end
return out
end
 
local function processWord(word)
out = "<td>" .. word.w
if word.c then
out = out .. " <small>(" .. word.c .. ")</small>"
end
if word.r then
out = out .. "<br>''" .. word.r .. "''"
end
out = out .. "</td>"
 
out = out .. "<td>"
if word.m then
out = out .. word.m
end
out = out .. "</td></tr><tr>"
return out
 
end
 
local function processLang(lang)
out = "<tr><td rowspan=" .. #lang .. ">" .. lang.l .. "</td>"
for _, value in ipairs(lang) do
out = out .. processWord(value)
end
out = out .. "</tr>"
return out
end
 
function p.main( ... )
   
    local langs = processLangs(arg)
      
    out = [[<table id="foreignNames" cellspacing=0 cellpadding=4 border=1 style="border-collapse:collapse;border:1px solid black;margin-bottom:5px;background:white;{{#if:{{{float|}}}|float:left}}>"
<tr><th style="background:white">Language</th>
<th style="background:white">Name</th>
<th style="background:white">Meaning</th>]]
   
    for _, value in ipairs(langs) do
    out = out .. processLang(value)
    end
   
    out = out .. "</table>"
   
    return out
   
end
end


return p
return p

Revision as of 23:38, February 9, 2021

The sandbox (Module:Sandbox) is a module namespace page designed for testing and experimenting with Lua syntax. Feel free to try your skills at programming here: Click on edit, make your changes, then click "Save changes" when you are finished. Code added here will not stay permanently. Feel free to revert the page to its hello world function when you are done using it. This is not a page to chat.

If you need further help with modules, see the Lua reference manual.


local LANGUAGE_CODES = {
	cs = "Czech",
	da = "Danish",
	de = "German",
	en = "English",
	es = "Spanish",
	fi = "Finnish",
	fr = "French",
	he = "Hebrew",
	hu = "Hungarian",
	it = "Italian",
	ja = "Japanese",
	ko = "Korean",
	nl = "Dutch",
	no = "Norwegian",
	pl = "Polish",
	pt = "Portuguese",
	ro = "Romanian",
	ru = "Russian",
	sv = "Swedish",
	zh = "Chinese",
	["zh-Hans"] = "Simplified Chinese",
	["zh-Hant"] = "Traditional Chinese",
}

local p = {}

local function split( value )
	local pos = string.find(value, "=")
	return string.gsub(string.sub(value, 0, pos - 1), '^%s*(.-)%s*$', '%1'), 
		string.gsub(string.sub(value, pos + 1, -1), '^%s*(.-)%s*$', '%1')
end

local function getOrCreateMember ( t, f, m )
	for _, value in ipairs(t) do 
		if f(value) then
			return value
		end
	end
	
	table.insert(t, m)
	return m
end

local function processLangs( arg )
	
	local out = {}
	local lang = {}
	local word = {}
	
	for _, parameter in ipairs(arg) do
    	local name, value = split(parameter)
    	if name == "l" then
    		decodedValue = LANGUAGE_CODES[value]
    		lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, {["l"] = decodedValue})
    	elseif name == "w" then
    		word = getOrCreateMember(lang, function(f) return f.w == value end, {["w"] = value})
    	else
    		word[name] = value
    	end
	end
	
	return out
		
end

local function processWord(word)
	out = "<td>" .. word.w
	if word.c then
			out = out .. " <small>(" .. word.c .. ")</small>"
	end
	if word.r then
		out = out .. "<br>''" .. word.r .. "''"
	end
	out = out .. "</td>"

	out = out .. "<td>"
	if word.m then
		out = out .. word.m 
	end
	out = out .. "</td></tr><tr>"
	
	return out

end

local function processLang(lang) 
	out = "<tr><td rowspan=" .. #lang .. ">" .. lang.l .. "</td>"
	for _, value in ipairs(lang) do
		out = out .. processWord(value)
	end
	out = out .. "</tr>"
	return out
end

function p.main( ... )
    
    local langs = processLangs(arg)
    
    out = [[<table id="foreignNames" cellspacing=0 cellpadding=4 border=1 style="border-collapse:collapse;border:1px solid black;margin-bottom:5px;background:white;{{#if:{{{float|}}}|float:left}}>"
		<tr><th style="background:white">Language</th>
		<th style="background:white">Name</th>
		<th style="background:white">Meaning</th>]]
    
    for _, value in ipairs(langs) do
    	out = out .. processLang(value)
    end
    
    out = out .. "</table>"
    
    return out
    
end

return p