Jak and Daxter Wiki
This page contains Lua code for Template:Cite.

-- This module implements cite templates.
-- See {{cite}} for the base template and a documentation.
local cite = {}
local getArgs = require('Dev:Arguments').getArgs
local data = mw.loadData('Module:Codename')

-- Helper functions
local function gamelink(game)
    game = string.lower(game)
    local link = data[game]['link']
    local shortname = data[game]['shortname']
    return string.format("[" .. "[" .. "%s|\'\'%s\'\']]", link, shortname)
end

local function missionlink(mission, display)
    if display then
        return string.format(', \"[' .. '[' .. '%s|%s]]\"', mission, display)
    else
        return string.format(', \"[' .. '[' .. '%s]]\"', mission)
    end
end

local function gamefile(file)
    return string.format(', file: <code>%s</code>', file)
end

local function script(game, scriptsection, display)
    game = string.lower(game)
    local gamename = data[game]['shortname']
    if display then
        return string.format(' [' .. '[' .. '%s script#%s|script &sect; \"%s\"]]', gamename, scriptsection, display)
    else
        return string.format(' [' .. '[' .. '%s script#%s|script &sect; \"%s\"]]', gamename, scriptsection, scriptsection)
    end
end

-- Produces the cite template.
function cite.main(frame)
	local args = getArgs(frame)
	
	local game = args['game'] or args[1]
	
	local gamesec = ''
	if game then gamesec = gamelink(game) end
  	
  	local contentsec = ''
  	    
	if args['mission']
	    then contentsec = missionlink(args['mission'], args['display'])
    elseif args['file']
        then contentsec = gamefile(args['file'])
    elseif args['script']
        then contentsec = script(game, args['script'], args['display'])
	elseif args['guide']
	    then contentsec = ", ''" .. data[game]['guide']['title'] .. "'', p. " .. args['guide']
    elseif args['manual']
        then contentsec = ', ' .. data[game]['manual'][args['region']] .. ', p. ' .. args['manual']
    elseif args[2] or args['other']
        then contentsec = ', ' .. (args[2] or args['other'])
	end
	
	local refname = ''
	if args['name'] then refname = ' name="' .. args['name'] .. '"' end
	
	local citeoutput = '<ref' .. refname ..'>' .. gamesec .. contentsec .. '</ref>'
	
	return frame:preprocess(citeoutput)
end

return cite