Module:Sandbox
From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search
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.
--[[a table that maps language codes to the english name of the corresponding
language. we should just use mw.language.fetchLanguageName for this, but i
guess it's broken...? ``mw.language.fetchLanguageName("de", "en")`` returns
"Deutsch" even though the documentation says it should return "German"]]
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",
--[[propritary language codes from template:foreign_names. most of them
to match iso 639-2, but one is also literally a racial slur, so i don't
know whether it's really worth keeping them]]
cze = "Czech",
dan = "Danish",
ger = "German",
spa = "Spanish",
fin = "Finnish",
fra = "French",
fre = "French",
heb = "Hebrew",
hun = "Hungarian",
ita = "Italian",
jap = "Japanese",
kor = "Korean",
dut = "Dutch",
nor = "Norwegian",
pol = "Polish",
por = "Portuguese",
rom = "Romanian",
rus = "Russian",
swe = "Swedish",
chi = "Chinese",
chis = "Simplified Chinese",
chit = "Traditional Chinese",
}
--allow case-insensitive language codes, show warning if no code was found
setmetatable(LANGUAGE_CODES, {
__index = function(this, key)
local code = rawget(this, string.lower(key))
if code == nil then
mw.addWarning("'''WARNING''' (Foreign names): Could not find a language code corresponding to \"" .. mw.text.nowiki(key) .. "\".")
code = key
end
return code
end
}
)
--[[splits a string at the first ":" and returns the (whitespace-stripped)
characters on either side]]
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
--[[searches a table t for a member that is evaluated to true by the test
function f. if found, return that member. if not, create a new object with
function m, add it to t, and return it]]
local function getOrCreateMember ( t, f, m )
for _, value in ipairs(t) do
if f(value) then
return value
end
end
local created = m()
table.insert(t, created)
return created
end
--[[processes the data from frame.args into an intermediate format that can be
converted into an html table]]
local function processLangs( arg )
local out = {}
local lang = {}
local word = {}
for _, parameter in ipairs(arg) do
local name, value
if pcall(function () name, value = split(parameter) end) then
name = string.lower(name)
if name == "l" then
decodedValue = LANGUAGE_CODES[value]
lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, function() return {["l"] = decodedValue} end)
elseif name == "w" then
word = getOrCreateMember(lang, function(f) return f.w == value end, function() return {["w"] = value} end)
elseif name == "c" or name == "r" or name == "m" then
word[name] = value
else
mw.addWarning("'''WARNING''' (Foreign names): Could not parse \"" .. mw.text.nowiki(parameter) .. "\".")
end
else
mw.addWarning("'''WARNING''' (Foreign names): Could not parse \"" .. mw.text.nowiki(parameter) .. "\".")
end
end
return out
end
--generate html for one word
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
--generate html for one language
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
local p = {}
--[[entry point, generates an html table of the names of a subject in various
languages]]
function p.main( frame )
local langs = processLangs(frame.args)
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