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:
--[[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 = {
local LANGUAGE_CODES = {
cs = "Czech",
cs = "Czech",
Line 20: Line 24:
sv = "Swedish",
sv = "Swedish",
zh = "Chinese",
zh = "Chinese",
["zh-Hans"] = "Simplified Chinese",
["zh-hans"] = "Simplified Chinese",
["zh-Hant"] = "Traditional 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",
}
}


local p = {}
--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("Could not find a language code corresponding to \"" .. 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 function split( value )
local pos = string.find(value, ":")
local pos = string.find(value, ":")
Line 32: Line 75:
end
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 )
local function getOrCreateMember ( t, f, m )
for _, value in ipairs(t) do  
for _, value in ipairs(t) do  
Line 39: Line 85:
end
end
table.insert(t, m)
local created = m()
return m
table.insert(t, created)
return created
end
end


--[[processes the data from frame.args into an intermediate format that can be
converted into an html table]]
local function processLangs( arg )
local function processLangs( arg )
Line 50: Line 99:
for _, parameter in ipairs(arg) do
for _, parameter in ipairs(arg) do
     local name, value = split(parameter)
     local name, value
    if name == "l" then
    if pcall(function () name, value = split(parameter) end) then
    decodedValue = LANGUAGE_CODES[value]
    name = string.lower(name)
    lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, {["l"] = decodedValue})
    if name == "l" then
    elseif name == "w" then
    decodedValue = LANGUAGE_CODES[value]
    word = getOrCreateMember(lang, function(f) return f.w == value end, {["w"] = value})
    lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, function() return {["l"] = decodedValue} end)
    else
    elseif name == "w" then
    word[name] = value
    word = getOrCreateMember(lang, function(f) return f.w == value end, function() return {["w"] = value} end)
    end
    elseif name == "c" or name == "r" or name == "m" then
    word[name] = value
    else
    mw.addWarning("Could not parse \"" .. parameter .. "\".")
    end
    else
    mw.addWarning("Could not parse \"" .. parameter .. "\".")
    end
end
end
Line 65: Line 121:
end
end


--generate html for one word
local function processWord(word)
local function processWord(word)
out = "<td>" .. word.w
out = "<td>" .. word.w
Line 85: Line 142:
end
end


--generate html for one language
local function processLang(lang)  
local function processLang(lang)  
out = "<tr><td rowspan=" .. #lang .. ">" .. lang.l .. "</td>"
out = "<tr><td rowspan=" .. #lang .. ">" .. lang.l .. "</td>"
Line 94: Line 152:
end
end


local p = {}
--[[entry point, generates an html table of the names of a subject in various
languages]]
function p.main( frame )
function p.main( frame )
      
      

Revision as of 14:36, February 10, 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.


--[[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("Could not find a language code corresponding to \"" .. 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("Could not parse \"" .. parameter .. "\".")
	    	end
	    else
	    	mw.addWarning("Could not parse \"" .. 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