Fandom Developers Wiki
Register
Advertisement

This page contains some quick Lua snippets to help create modules.

General

Get template and transcluding page arguments

local p = {}
function p.doStuff(frame)
        local transclusionArguments = frame.args
        local templateArguments = frame:getParent().args
        local templateOption = templateArguments["option"] or ""
        local transclusionOption = transclusionArguments["trans_option"] or ""
 
        return transclusionOption .. templateOption
end
 
return p

Check if a lua table contains values.

local tTab = {}

if next(tTab) then
    mw.log (true)
end

Get the title of the page that transcludes a page containing a lua invocation (parent frame). This is typically a template name.

local p = {}

function p.frameTitle(frame)
    local title = frame:getParent():getTitle()
    return (title)
end

return p

Get parameters (bool= in example) passed to template that have no value and read them to set boolean variables (or just localBool in this example).

local p = {}
 
-- include Arguments module at dev.fandom.com
local getArgs = require('Dev:Arguments').getArgs

function p.myTemplate(frame) -- Implements {{myTemplate}}
	local args = getArgs(frame, {
	trim = false,
	removeBlanks = false -- Keeps empty params
	})
	local booleanVar = false -- Default booleanVar to false

	-- Set booleanVar to true if |bool= used with template
	if args["bool"] then
		booleanVar = true
	end

	return p._myTemplate(args, booleanVar)
end

function p._myTemplate( args, isBool )
	local localBool = isBool -- Set local boolean variable
	local returnWikitext = nil

	-- Do stuff that is conditional on isBool and set returnWikitext

	return returnWikitext
end

Text

Find a piece of text within text.

local textToFind = "one"
local content = "In the end there can be only one"
local startposition , endposition = string.find(content, textToFind)

if startposition then
   mw.log (true)
end

Show only a specific number of sequential tokens (group of characters) from a piece of text.

local text = "Human beings are a disease, a cancer of this planet."
local tab = mw.text.split( text, " ")
local tokens = 5
local output = ""
   for i = 0, tokens do
       output = output .. " " .. (tab[i] or "")
   end

print(output)
Output: Human beings are a disease,

Math

Round a number.

function p.round(num, idp)
    local roundedNumber = tonumber(string.format("%." .. (idp or 0) .. "f", num))

    return (roundedNumber)
end

Date

Add days to a date.

local iDays = 50
local iDayToSecs = iDays  * 24 * 60 *60
local timestamp = os.time({month=12,year=2012,day=10})
local futureDate = os.date("%Y-%m-%d", timestamp + iDayToSecs)

mw.log (futureDate)

Conditional statements

Using a ternary operator.

local condition = true
local trueText = "Bananas are good"
local returnText = condition and trueText or ""
-- This returns trueText if condition is true, and "" if not
mw.log (returnText)

Sorting

Sorting using an Associative table[1]

function spairs(t, order)
    -- collect the keys
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end

    -- if order function given, sort by it by passing the table and keys a, b,
    -- otherwise just sort the keys
    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    -- return the iterator function
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

Usage

local power = { Majin = "11", Buu = "3", Baba = "10" , ["5"] = "ratty", ["8"] = "bum"   }

-- basic usage, just sort by the keys
for k,v in spairs(power) do
    print(k,v)
end
-- Sorts by keys
-- Output
-- 5	ratty 8	bum Baba 10 Buu	3 Majin	11
-- Sort by values
for k,v in spairs(power, function(t,a,b) return t[b] < t[a] end) do
    print(k,v)
end
-->5	 ratty
-->8	 bum
-->Buu	 3
-->Majin 11
-->Baba	 10

See also

References

  1. Sort a Table[] in Lua. http://stackoverflow.com/a/15706820
Text above can be found here (edit)
Advertisement