1,921 Pages

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

-- <nowiki>
-- This module implements ref templates.
-- See {{ref}} for the base template and a documentation.
local ref = {}
local getArgs = require('Dev:Arguments').getArgs
local data = mw.loadData('Module:Codename')
local lang = mw.language.getContentLanguage()

--Automatically creates a reference for games based on Module:Codename/data
local function gameref(game, section, version)
    game = assert(data[game], 'Could not find a game in Module:Codename/data matching this input')
    data = game
    if version and data['versions'][version] then
        data = data['versions'][version]
    end
    
    local outputdata = {}
    outputdata['author'] = game['developer']
    outputdata['date'] = lang:formatDate('Y', data['release'])
    outputdata['link'] = game['pagename']
    outputdata['title'] = game['title']
    outputdata['platform'] = '[[wikipedia:' .. game['platform'] .. '|' .. game['platform'] .. ']]'
    outputdata['publisher'] = '[[wikipedia:Sony Interactive Entertainment|Sony Interactive Entertainment]]'
    outputdata['media'] = data['media']
	outputdata['italictitle'] = true
    
    return outputdata
end

--Automatically creates a reference for game guides based on Module:Codename/data
local function guideref(game, section)
    game = assert(data[game], 'Could not find a game in Module:Codename/data matching this input')
    data = game
	assert(data['guide'], 'Could not find a guide for this game')

    local outputdata = {}
    outputdata['author'] = data['guide']['author']
    outputdata['date'] = lang:formatDate('F Y', data['guide']['release'])
    outputdata['title'] = data['guide']['title']
    outputdata['publisher'] = data['guide']['publisher']
    outputdata['isbn'] = data['guide']['isbn']
    outputdata['media'] = data['guide']['media']
	outputdata['italictitle'] = true
    
    return outputdata
end

--Automatically creates a reference for movies based on Module:Codename/data
local function movieref(movie, section)
    movie = assert(data[movie], 'Could not find a movie in Module:Codename matching this input; fill generic reference')
    data = movie
    
    local outputdata = {}
    outputdata['author'] = data['director']
    outputdata['author-type'] = 'Director'
    outputdata['date'] = lang:formatDate('Y', data['release'])
    outputdata['link'] = movie['pagename']
    outputdata['title'] = data['title']
    outputdata['publisher'] = data['studio']
    outputdata['media'] = data['media']
	outputdata['italictitle'] = true
    
    return outputdata
end

--Automatically creates a reference for books or manga based on Module:Codename/data or Module:Sources
local function bookref(book, section)
    book = assert(data[book] or sourcedata[book], 'Could not find a game in Module:Codename/data or Module:Sources matching this input')
    data = book
    
    local outputdata = {}
    outputdata['author'] = data['author']
    outputdata['date'] = lang:formatDate('F Y', data['release'])
    outputdata['link'] = book['pagename']
    outputdata['title'] = data['title']
    outputdata['publisher'] = data['publisher']
    outputdata['isbn'] = data['isbn']
    outputdata['media'] = data['media']
	outputdata['italictitle'] = true
    
    return outputdata
end

--Assistant for url archives links
local function archiveformat(url, accessdate, archiveurl, archivedate)
    local link = ''
    if archiveurl then
        link = archiveurl
    else
        link = 'https://web.archive.org/web/' .. url
    end
    
    if archivedate
        then return string.format('Accessed %s. [%s Archived] from the original on %s. ', accessdate, link, archivedate)
    else
        return string.format('[%s Archived] from the original on %s. ', link, accessdate)
    end
end

--Assistant for automating certain platform links
local function platformformat(platform)
    if platform == 'ps2'
        then return '[[wikipedia:PlayStation 2|PlayStation 2]]'
    elseif platform == 'ps3'
        then return '[[wikipedia:PlayStation 3|PlayStation 3]]'
    elseif platform == 'ps4'
        then return '[[wikipedia:PlayStation 4|PlayStation 4]]'
    elseif platform == 'ps5'
        then return '[[wikipedia:PlayStation 5|PlayStation 5]]'
    elseif platform == 'psp'
        then return '[[wikipedia:PlayStation Portable|PlayStation Portable]]'
    elseif platform == 'psvita'
        then return '[[wikipedia:PlayStation Vita|PlayStation Vita]]'
    else
        return platform
    end
end

--Produces the ref template, allows for parameter input or autogeneration.
--Parameter inputs override refs.
function ref.main(frame)
	local args = getArgs(frame)
	
	local data = {}
	local refoutput
	
	if args['game']
	    then data = gameref(args['game'], args['section'], args['version'])
	elseif args['guide']
		then data = guideref(args['guide'], args['section'])
    elseif args['movie']
        then data = movieref(args['movie'], args['section'])
    elseif args['book']
        then data = bookref(args['book'], args['section'])
    else
    end
    
    local authorsect = ''
    if args['author'] or data['author'] then
        author = args['author'] or data['author']
    	if author == 'ig'
    	    then author = '[[wikipedia:Insomniac Games|Insomniac Games]]'
        end
        
        if args['author-id'] and args['tweet-id']
            then authorsect = string.format('[https://twitter.com/%s %s]', args['author-id'], author)
        else
            authorsect = author
        end
        
        if args['author-type'] or data['author-type']
        	then authorsect = authorsect .. ' (' .. ((args['author-type'] or data['author-type']):gsub("^%l", string.upper)) .. ')'
        end
        
        if args['date'] or data['date']
            then authorsect = authorsect .. ' (' .. (args['date'] or data['date']) .. ')'
        end
        
        authorsect = authorsect .. '. '
	else
		if args['date'] or data['date']
            then authorsect = (args['date'] or data['date']) .. '. '
        end
    end
    
    local title = assert(args['title'] or data['title'], 'Title is mandatory')
    local titlesect = title
    
    if data['link'] or args['link']
        then titlesect = string.format("[[%s|%s]]", (data['link'] or args['link']), title)
    elseif args['archive-url'] and args['brokenlink']
	    then titlesect = string.format('[%s %s]', args['archive-url'], title)
    elseif args['url']
        then titlesect = string.format('[%s %s]', args['url'], title)
    elseif args['youtube-id']
        then titlesect = string.format('[https://www.youtube.com/watch?v=%s %s]', args['youtube-id'], title)
    elseif args['tweet-id']
        then titlesect = string.format('[https://twitter.com/%s/status/%s %s]', args['author-id'], args['tweet-id'], title)
    end
    
 	if args['italictitle'] or data['italictitle']
        then titlesect = string.format("''%s''", titlesect)
    else
        titlesect = string.format("%s", titlesect)
    end
    
    if args['media'] or data['media']
    	then titlesect = titlesect .. ' [' .. ((args['media'] or data['media']):gsub("^%l", string.upper)) .. ']'
    end
    
    local section = ''
    if args['section']
        then section = args['section'] .. ' in '
    end
    
    local extra = ''
    if args['p'] or args['page']
        then extra = '. p. ' .. args['p'] or args['page']
    end
    if args['extra']
        then extra = '. ' .. args['extra']
    end
    
    --Creates essential reference format
    refoutput = string.format('%s%s%s%s.', authorsect, section, titlesect, extra)
    
    --Additional fields added in string to be appended at the end
    local additional = ''
    
    if args['channel-id'] and args['channel'] and args['youtube-id'] then
        additional = additional .. string.format('[https://www.youtube.com/channel/%s %s]. ', args['channel-id'], args['channel'])
    end
    
    if args['youtube-id']
        then additional = additional .. '[[wikipedia:YouTube|YouTube]]. '
    elseif args['tweet-id']
        then additional = additional .. '[[wikipedia:Twitter|Twitter]]. '
    elseif args['platform'] or data['platform']
        then additional = additional .. ' ' .. platformformat(args['platform'] or data['platform']) .. '. '
    end
    
    if args['additional-authors'] or data['additional-authors']
        then additional = additional .. string.format('%s. ', (args['additional-authors'] or data['additional-authors']))
    end
    
    if data['publisher'] or args['publisher']
        then local publisher = data['publisher'] or args['publisher']
            if publisher == 'sony'
                then publisher = '[[wikipedia:Sony Interactive Entertainment|Sony Interactive Entertainment]]'
            elseif args['channel-id'] and args['youtube-id']
                then publisher = string.format('[https://www.youtube.com/channel/%s %s]', args['channel-id'], publisher)
            end
        additional = additional .. ' ' .. publisher .. '. '
    end

    if data['isbn'] or args['isbn'] then
        additional = additional .. string.format('[[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]. ', (args['isbn'] or data['isbn']), (args['isbn'] or data['isbn']))
    end
    
    if args['url'] and not args['broken-link']
        then additional = additional .. archiveformat(args['url'], assert(args['access-date'], 'Access dates are required for website references'), args['archive-url'], args['archive-date'])
    elseif args['broken-link']
        then additional = additional .. string.format('Archived from the [%s original] on %s. ', args['url'], assert(args['access-date'], 'Access dates are required for website references') or args['archive-date'])
    end
    
    refoutput = refoutput .. ' ' .. additional
    
    --id used to link to a reference if needed
    if args['id']
        then refoutput = string.format('<span id="%s">%s</span>', args['id'], refoutput)
    end
	
	return frame:preprocess(refoutput)
end

return ref

--[[Category:Modules]]
Community content is available under CC-BY-SA unless otherwise noted.