Taming.io Wiki

This module adds some additional functions to String in the dev wiki.


local p = require('Dev:String')
local getArgs = require('Dev:Arguments').getArgs

-- Unstrips the nowiki and proccesses it
-- e.g. {{#invoke:string|unstripNoWiki|1=<nowiki>{{Normal}}</nowiki>}} = [[Normal Type]]
-- This is useful for functions like Desktop-only
-- to not have the need to keep on using {{!}} to
-- escape
--- @param frame Frame
function p.unstripNoWiki ( frame )
	local args = getArgs(frame)
	return frame:preprocess(
        mw.text.decode(
            mw.text.unstripNoWiki(frame.args[1])
        )
    )
end

-- Adds the args in a semantic manner, just like the `mw.text.listToText` library
--- @param frame Frame
function p.join ( frame )
	local args = getArgs(frame)
	local stringTable = {}

	for k, v in pairs(args) do
		if type(k) == 'number' then
			stringTable[k] = v;
		end
	end

	return mw.text.listToText(stringTable)
end

-- Adds the table passed as its first argument a with the second argument
-- Do NOT implement it as using table.concat, since it wont work with
-- metatables
function p.add ( frame )
	local args = getArgs(frame)

	local stringToAdd = p.replace{ args = { args[2], "×", " "} } or " "
	local tableToAdd = args[1]
	local str = tableToAdd[1]

	local i = 2

	while (tableToAdd[i] ~= nil) do
		str = str..stringToAdd..tableToAdd[i]
		i = i + 1
	end

	return str
end

-- Separates string into a table
function p.separate ( frame )
	local args = getArgs(frame)
	local replaceStr = p.replace{ ["args"] = { args[2], "×", " "} } or " "

	return mw.text.split(args[1], replaceStr, true)
end

-- Returns filename with format
-- File:name.png
-- From something like [[name.png|150px]]
function p.standardizeFileName ( frame )
	local fileName = getArgs(frame)[1]
	
	-- For files with File: prefix, we remove
	-- the File: prefix and add it at the end,
	-- therefore guaranteeing we always have
	-- the File: prefix
	return 'File:'
	-- For files written as File:... .png|...
	.. mw.text.split(fileName, '|')[1]
		-- For files written as [[File:... .png]]
		:gsub('%[%[', '')
		:gsub(']]', '')
		:gsub('[Ff][Ii][Ll][Ee]%s*:%s*', '')
end

return p