Module:Utilities

From Sun Haven Wiki
Jump to navigation Jump to search

This module is designed to hold cross category utility functions. It is not a catch all for 'I don't know where to put this' functions.

Repeat Template

A comma separated list of pages containing table data already, in the format <PageName>!<DataType> And / Or a commas separated list of custom data, in the format <Col1> @ <Col2> @ <ColN> When mixing and matching custom row data with page data, try and match the same number of columns This method only formats the data, you will still need to provide a header row

Format

recipes = Omelet!Food, Veggie Wrap!Food, Chili!Food

The data for recipes are stored in the `FoodRow` thus recipes use !Food.

datum = row1 col1 @ row1 col2 @ row1 col3, row2 col1 @ row2 col2 @ row2 col3 

This custom data will generate a 2 rows for a table of 3 columns in width

datum = Omelet!Food, 
row2 col1 @ row2 col2 @ row2 col3 @ row2 col4 @ row2 col5 @ row2 col6, 
Chili!Food

This custom data will generate a 3 rows: Recipe, Custom, Recipe

Example

{{#invoke:Utilities|repeatTemplate|Omelet!FoodRow, row2 col1 @ row2 col2 @ row2 col3 @ row2 col4 @ row2 col5 @ row2 col6, Chili!FoodRow}}
|}


CSV To Bulleted List

A comma separated list to be turned into a bulleted list all off the same indentation

Format

datum = Omelet, Veggie Wrap, Chili

A simple comma separated list

Example

{{#invoke:Utilities|csv2bullets|Omelet, {{icon|Veggie Wrap}}, I prefer {{Currency|Coins}} over {{Currency|Tickets}}}}


Function N

Function descriptor

Format

<onlyinclude>
Sample Code Formatting
</onlyinclude>

Example

Sample code to display

Execution code

Subpages

local p = {} --p stands for package

local trim = mw.text.trim

function p.repeatTemplate(frame)
	
	-- Boilerplate CSV to array. Splits the input, removes empty indicies,
	-- and trims leading/trailing whitespace
    local a = mw.text.split(frame.args[1], ",")
    local args = {}
    for i = 1, #a, 1 do
		a[i] = trim(a[i])
		if not (a[i] == "" or not a[i]) then -- remove empty indexes
			args[#args + 1] = a[i]
		end
    end
    
    local result = ""
    for k,v in ipairs(args) do
		local row = mw.text.split(v, "!")
		-- If there is a ! treat it in the format of Page!DataType
		if #row > 1 then
			result = result.."\n{{:"..row[1].."|"..row[2].."}}"
		-- If there is NOT a ! treat it as a custom row delimited by `@`
		else
			result = result.."\n|-\n|"..string.gsub(v, "@", "||")
		end
    end

	return frame:preprocess(result)
end


function p.repeatCookingTemplate(frame)
	
	-- Boilerplate CSV to array. Splits the input, removes empty indicies,
	-- and trims leading/trailing whitespace
    local a = mw.text.split(frame.args[1], ",")
    local args = {}
    for i = 1, #a, 1 do
		a[i] = trim(a[i])
		if not (a[i] == "" or not a[i]) then -- remove empty indexes
			args[#args + 1] = a[i]
		end
    end
    
    local result = ""
    for k,v in ipairs(args) do
		local row = mw.text.split(v, "!")
		-- If there is a '@' treat it as a custom row
		if string.find(row[1], "@") then
			result = result.."\n|-\n|"..string.gsub(v, "@", "||")
		-- If there is not a `@` treat it in the format of Page!DataType
		else
			result = result.."\n{{:"..row[1].."|CookingRow}}"
		end
    end

	return frame:preprocess(result)
end


function p.csv2bullets(frame)
	
	-- Boilerplate CSV to array. Splits the input, removes empty indicies,
	-- and trims leading/trailing whitespace
    local a = mw.text.split(frame.args[1], ",")
    local args = {}
    for i = 1, #a, 1 do
		a[i] = trim(a[i])
		if not (a[i] == "" or not a[i]) then -- remove empty indexes
			args[#args + 1] = a[i]
		end
    end
    
    local result = ""
    for k,v in ipairs(args) do
    	-- Convert line to Bulleted list formatting
		result = result.."* "..v.."\n"
    end

	return frame:preprocess(result)
end

-- [Module] Converts wiki frame.args into a parameter list allowing one function prototype to be called from both a module and wiki
-- param = The parameter to resolve
-- index = The expected index of the param in frame.args
---- This value should not be populated for the first param
---- This value should be 0 to convert an unknown # of params into a table
-- returnCanBeNil = If this param is allowed to be NIL
function p.resolveParameter(param, index, returnCanBeNil)
    local retArg
    local args
    
    --if parameter is passed from wiki, not other function
    if type(param) == "table" then 
		args = require('Module:Arguments').getArgs(param)
		retArg = ((index == 0 and args) or (args and args[(index or 1)] and p.replaceSpecialCharacters(args[(index or 1)]))) 
    end
    
    --param.args ~= nil and next(param.args): this means that params.args is not nil but empty table
    if retArg == nil and type(args) == "table" and next(args) == nil and not returnCanBeNil then 
    	retArg = p.getParamOrPageName() 
    end 
    
    --if parameter was passed directly or not at all
    if retArg == nil and not returnCanBeNil then 
    	retArg = p.getParamOrPageName(param) 
    end 
    
    --simply pass string parameter, and proccess it (with special char removal).
    if retArg == nil and type(param) == "string" and not returnCanBeNil then
    	retArg = param 
    end 
    
    --final processing
    if retArg ~= nil and type(retArg) == "string" then 
    	retArg = p.replaceSpecialCharacters(retArg) 
    end 
    
    return retArg
end


function p.getParamOrPageName(param)
	if param and 
		(type(param) ~= "table" or 
		(type(param) == "table" and not param.args)) 
	then 
		return param
	else
		local pageName = mw.title.getCurrentTitle().text
		local pageNameParts = p.split(pageName, '/')
		local langs = require("Module:Languages")
		for _, lang in ipairs(langs.languages) do
			if pageNameParts[#pageNameParts] == lang.iso then
				return pageNameParts[#pageNameParts - 1]
			end
		end
		return pageNameParts[#pageNameParts] -- return the last name part - This should be adjusted when language suffixes are the thing
	end
end

function p.replaceSpecialCharacters(name)
    local charList = { 
        ["'"] = '&#39;',
        ["&"] = '&#38;'
    }
    if name == nil then error("Name parameter is empty, but it shouldnt?") end
    for repl, sChar in pairs(charList) do
        name = string.gsub(name, sChar, repl)
    end
    return name
end

function p.split(inputstr, sep)
        if sep == nil then
            sep = "%s"
        end
        local t = {}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            table.insert(t, str)
        end
        return t
end

return p