Module:Test: Difference between revisions
Jump to navigation
Jump to search
KokoroSenshi (talk | contribs) m (testing calling a function from within the module and with outside in wikitext with #invoke) |
KokoroSenshi (talk | contribs) m (testing calling a function from within the module and with outside in wikitext with #invoke) |
||
Line 117: | Line 117: | ||
function p.funcDriver( frame ) | function p.funcDriver( frame ) | ||
--nope--f.args[1] = "something" | --nope--f.args[1] = "something" | ||
--f = {} | --f = {} | ||
--f.args = {} | --f.args = {} | ||
--f.args[1] = "something" | --f.args[1] = "something" | ||
f = {} | |||
f.args = { "something" } | --f = {} | ||
--f.args = { "something" } | |||
f = { args = { "something" } } | |||
return p.func( f ) | return p.func( f ) | ||
end | end | ||
return p | return p |
Revision as of 09:11, 6 August 2017
Documentation for this module may be created at Module:Test/Documentation
local p = {}
--Hello, world!
function p.hello( frame )
return "Hello, world!"
end
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#table
function p.tabletest( frame )
-- Create table
t = {}
t["foo"] = "foo"
t.bar = "bar"
t[1] = "one"
t[2] = "two"
t[3] = "three"
t[12] = "the number twelve"
t["12"] = "the string twelve"
t[true] = "true"
t[tonumber] = "yes, even functions may be table keys"
t[t] = "yes, a table may be a table key too. Even in itself."
-- This creates a table roughly equivalent to the above
t2 = {
foo = "foo",
bar = "bar",
"one",
"two",
[12] = "the number twelve",
["12"] = "the string twelve",
"three",
[true] = "true",
[tonumber] = "yes, even functions may be table keys",
}
t2[t2] = "yes, a table may be a table key too. Even in itself."
end
--Testing
function p.test( frame )
----
-- https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual
-- https://www.lua.org/cgi-bin/demo
----
--Precondition: Assumes no spaces in the string
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Variables
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Frame_object
local game = frame.args[1]
local word = frame.args[2]
local size = frame.args[3]
game = game:upper()
if game == 'BOTW' then game = 'ALBW' end
if size == nil or '' then size = '20' end
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#table
local t = {}
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#string
t[1] = '<span style="display:inline-block; margin:1px 4px;">'
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Length_operator
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Control_structures
local i = 1
repeat
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Function_calls
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#string.sub
local char = word:sub(i,i)
--local char = string.sub(word,i,i)
--local char = word.sub(word,i,i)
char = char:upper()
--char = string.upper(char)
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Control_structures
--No case-switch in Lua
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Relational_operators
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Logical_operators
--https://stackoverflow.com/questions/27633331/can-i-check-strings-equality-in-lua#27634079
local letter = char
if game == 'SS' then
if char == 'D' or char == 'W' then letter = 'DW'
elseif char == 'E' or char == 'K' then letter = 'EK'
elseif char == 'O' or char == 'Z' then letter = 'OZ'
end
elseif game == 'ALBW' then
if char == 'D' or char == 'G' then letter = 'DG'
elseif char == 'F' or char == 'R' then letter = 'FR'
elseif char == 'J' or char == 'T' then letter = 'JT'
elseif char == 'E' or char == 'W' then letter = 'EW'
elseif char == 'O' or char == 'Z' then letter = 'OZ'
end
end
local ext = 'png'
if game == 'ALBW' then ext = 'svg' end
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Concatenation_operator
t[i+1] = '<span class="tooltip" title="' .. char .. '">[[File:' .. game .. ' ' .. letter .. '.' .. ext .. '|x' .. size .. 'px|link=]]</span>'
i = i+1
until( i > #word )
t[i+1] = '</span>'
--https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#table.concat
return table.concat(t," ")
end
--should return the first arg when #invoked
function p.func( frame )
return frame.args[1]
end
--should return something when #invoked
function p.funcDriver( frame )
--nope--f.args[1] = "something"
--f = {}
--f.args = {}
--f.args[1] = "something"
--f = {}
--f.args = { "something" }
f = { args = { "something" } }
return p.func( f )
end
return p