Module:ParserFunctions

-- --Start Module local p = {}

--Get Other Required Modules local getArgs = require(Module:Arguments).getArgs local _error = require(Module:String)._error local string = require(Module:String)

--Invokes --Gets Input from template function p.ifNot(frame) local args = getArgs(frame) --Local Arguments local a = args[1] local b = args[2] local c = args[3] return p._ifNot(a, b, c) end

-- Module Access Point function p._ifNot(a, b, c)   if not a or a == '' or a == nil or a == false then return b       else return c    end end

--Invokes --Gets Input From Template function p.ifString(frame) local args = getArgs(frame) --Local Arguments local a = args[1] local b = args[2] local c = args[3] or "" local d = args[4] or "" if not a or not b then return _error('\"IfString\" requires at least 2 arguments.') end

local success, response = pcall(p._ifString, a, b, c, d)   return success and response or string.error(response) end

-- Module Access point function p._ifString(a, b, c, d)   if string.match(a, b)        then return c        else return d    end end

function p.len(frame) local args = getArgs(frame) local str = args[1] if not str then return _error('Invalid Arguments: \"Len\" requires inputs.') end return string.len(str) end

- -- Template: Sub -- -- returns a substring of the length specified by the limit - function p.sub(frame) local args = getArgs(frame) local str = args[1] local start = args[2] local len = args[3] return string.sub(str, start, len) end

function p.replace(frame) local args = getArgs(frame, {removeBlanks = false, trim=false}) local text = args[1] local search = args[2] local repl = args[3] local lim = args[4] if not search or not text then return _error('\"replace\" requires at least 2 arguments.') end return p._replace(text, search, repl, lim) end

function p._replace(text, search, repl, lim) local ret = string.gsub(text, search, repl or '', tonumber(lim) or nil) return ret end

function p.pos(frame) local args = getArgs(frame) local target_str = args[1] or '' local pos = tonumber(args[2]) or 0

if pos == 0 or math.abs(pos) > string.len(target_str) then return _error('String index out of range') end

return string.sub(target_str, pos, pos) end

function p.ifNotEq(frame) local args = getArgs(frame)

local text1 = args[1] local text2 = args[2] local tVal = args[3] local fVal = args[4] if text1 ~= text2 then return tVal else return fVal end end

- -- function: match(frame: table|frame) -- -- Invoked by. - function p.match(frame) local args = getArgs(frame, { removeBlanks=false, trim=false }) if not args[1] and args[2] then return string.error('"Match" requires atleast 2 arguments.') end local matches = { args[1]:match(args[2]) } return #matches == 1 and matches[1] or matches[args[3] or 1] or '' end --Finish Module return p