Module:Ep

From Queen's Court Games

Documentation for this module may be created at Module:Ep/doc

local p = {}
-- function to retrieve and format the episode name
function p.ep(frame)
	-- convert the input into the standardized epCode
    local ep =  frame.args[1]
    local epMatcher = require('Module:Ep/Matcher')
    local epCode = epMatcher.matchCode(ep)
    
    -- error message if code doesn't exist
    if epCode == '0x00' then
    	local errorMsg = "<span style=\"color:red; font-weight: bold;\">Undefined Episode</span>"
    	local title = mw.title.getCurrentTitle()
    	local namespace = title.namespace
    	local subpageText = title.subpageText

    	if namespace == 0 then
    		errorMsg = errorMsg .. "[[Category:Ep calls to undefined episodes]]"
    	elseif subpageText == 'test' or subpageText == 'testcases' then 
    		errorMsg = errorMsg .. "<small> would be in [[:Category:Ep calls to undefined episodes]]</small>"
    	end
    	return errorMsg
    end
    
    -- get the title and pagename for that epCode
    local epInfo = require('Module:Ep/Info')
    local epName = epInfo.title(epCode)
    local pageName = epInfo.pagename(epCode)
 
    -- epCode (w/special formatting, if any)
    local epCodeFormatted = epInfo.epCodeFormatted(epCode)
 
    -- formatting with the various parameters of {{ep}}
    local text = frame.args["text"] or nil
    local nolink = frame.args["nolink"] or nil
    local plain = frame.args["plain"] or nil
    local hide = frame.args["hide"] or nil
    local noshow = frame.args["noshow"] or nil
    
    local epValue
    
    if pageName:find("Talks Machina Special") and exists(noshow) then
    	epName = split(pageName, ': ')[2]
    elseif pageName:find("Talks Machina") and exists(noshow) then
    	epName = pageName:gsub('Talks Machina ', '#')
    elseif exists(noshow) then
        epName = pageName
    end

	
	if exists(text) then
		epValue = pageName .. "|" .. text
	elseif pageName ~= epName and not exists(nolink) then
        epValue = pageName .. '|' .. epName
	elseif exists(nolink) then
	    epValue = epName
    else
    	epValue = pageName
    end

    if exists(nolink) then epValue = epValue
    else epValue = "[[" .. epValue .. "]]" end

    if not exists(plain) then epValue = '"' .. epValue .. '"' end
 
    local fullEpValue = epValue .. ' ' .. epCodeFormatted
 
    if exists(hide) then return epValue
        else return fullEpValue end
 
end
 
--function to determine if a string is empty
function exists(s)
   return s ~= nil and s ~= ''
end

-- function to split a string at a specified character
function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end
 
return p