2,020 Pages

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

-- 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()

local function authorref(author, date, title, section)
    if author == 'ig'
        then author = '[[wikipedia:Insomniac Games|Insomniac Games]]'
    end
    
    section = section and section .. ' in ' or ''
    
    return string.format('%s (%s). %s%s.', author, date, section, title)
end

local function gameref(game, section)
    game = assert(string.lower(game), 'Could not find a game matching the input')
    assert(data[game]['media'] == 'game', "Did not reference a game")
    
    local dev = data[game]['developer']
    local releasedate = lang:formatDate('Y', data[game]['release'])
    local page = data[game]['pagename']
    local title = data[game]['title']
    local platform = data[game]['platform']
    
    section = section and section .. ' in ' or ''
    
    return string.format("%s (%s). %s''" .. "[" .. "[" .. "%s|%s]]''. %s. [[wikipedia:Sony Interactive Entertainment|Sony Interactive Entertainment]].", dev, releasedate, section, page, title, platform)
end

local function guideref(game, section)
    game = string.lower(game)
    assert(data[game]['guide']['title'], "Cannot find a guide for this release")
    
    local author = data[game]['guide']['author']
    local releasedate = lang:formatDate('F Y', data[game]['guide']['release'])
    local title = data[game]['guide']['title']
    local publisher = data[game]['guide']['publisher']
    local isbn = data[game]['guide']['isbn']
    
    section = section and section .. ' in ' or ''
    
    return string.format("%s (%s). %s''%s''. %s. [[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]", author, releasedate, section, title, publisher, isbn, isbn)
end

local function movieref(movie, section)
    movie = string.lower(movie)
    assert(data[movie]['media'] == 'movie', "Did not reference a movie")
    
    local director = data[movie]['director']
    local releasedate = lang:formatDate('F Y', data[movie]['release'])
    local title = data[movie]['title']
    local studio = data[movie]['studio']
    
    section = section and section .. ' in ' or ''
    
    return string.format("%s (%s). ''%s''. %s.", director, releasedate, title, studio)
end

local function bookref(book, section)
    book = string.lower(book)
    assert(data[book]['media'] == 'book' or data[book]['media'] == 'comic', "Did not reference a book")
    
    local author = data[book]['author']
    local releasedate = lang:formatDate('F Y', data[book]['release'])
    local title = data[book]['title']
    local publisher = data[book]['publisher']
    local isbn = data[book]['isbn']
    local series = " in ''" .. data[book]['series'] .. "''" or ''
    
    section = section and section .. ' in ' or ''
    
    return string.format("%s (%s). %s''%s''%s. %s in %s. [[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]", author, releasedate, section, title, series, publisher, isbn, isbn)
end

local function basicref(title, date)
    return string.format('%s. %s.', title, date)
end

local function urllinkformat(link, title, platform, publisherlink)
    if platform == 'youtube'
        then link = 'https://www.youtube.com/watch?v=' .. link
    elseif platform == 'twitter'
        then link = 'https://twitter.com/' .. publisherlink .. '/status/' .. link
    end
    return string.format('[%s %s]', link, title)
end

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 at %s. ', archivedate, link, acessdate)
    else
        return string.format('[%s Archived] from the original at %s. ', link, accessdate)
    end
end

local function publisherformat(publisher, publisherlink, platform)
    if publisherlink then
        if platform == 'youtube' then
           publisher = '[https://www.youtube.com/channel/' .. publisherlink .. ' ' .. publisher .. ']'
        elseif platform == 'twitter' then
            publisher = '[https://twitter.com/' .. publisherlink .. ' ' .. publisher .. ']'
        else
            publisher = '[' .. publisherlink .. ' ' .. publisher .. ']'
        end
    elseif publisher == 'sony' then
        publisher = '[[wikipedia:Sony Interactive Entertainment|Sony Interactive Entertainment]]'
    else
        publisher = publisher
    end
    
    return publisher .. '. '
end

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 == 'psp'
        then return '[[wikipedia:PlayStation Portable|PlayStation Portable]]'
    elseif platform == 'psvita'
        then return '[[wikipedia:PlayStation Vita|PlayStation Vita]]'
    elseif platform == 'youtube'
        then return '[[wikipedia:YouTube|YouTube]]'
    elseif platform == 'twitter'
        then return '[[wikipedia:Twitter|Twitter]]'
    else
        return platform
    end
end

-- Produces the ref template.
function ref.main(frame)
	local args = getArgs(frame)
	
	local title = args['title'] or ''
	
	if args['archive-url'] and args['brokenlink']
	    then title = string.format('[%s %s]', args['archive-url'], args['title'])
    elseif args['url']
        then title = urllinkformat(args['url'], args['title'], args['platform'], args['publisher-link'])
    end
    
    if args['noitalictitle']
        then title = title
    else
        title = string.format("''%s''", title)
    end

	local refoutput = ''
	
	if args['author']
	    then refoutput = authorref(args['author'], args['date'], title, args['section'])
    elseif args['game']
        then refoutput = gameref(args['game'], args['section'])
    elseif args['guide']
        then refoutput = guideref(args['guide'], args['section'])
    elseif args['movie']
        then refoutput = movieref(args['movie'], args['section'])
    elseif args['book']
        then refoutput = bookref(args['book'], args['section'])
    else
        refoutput = basicref(title, args['date'])
    end
    
    local extra = ''
    
    if args['additional-authors']
        then extra = extra .. string.format('%s. ', args['additional-authors'])
    end
    
    if args['publisher']
        then extra = extra .. publisherformat(args['publisher'], args['publisher-link'], args['platform'])
    end
    
    if args['platform']
        then extra = extra .. platformformat(args['platform']) .. '. '
    end

    if args['isbn']
        then extra = extra .. string.format('[[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]. ', args['isbn'], args['isbn'])
    end

    if args['url'] and not args['broken-link'] and args['platform'] ~= 'youtube' and args['platform'] ~= 'twitter' 
        then extra = extra .. archiveformat(args['url'], args['access-date'], args['archive-url'], args['archive-date'])
    elseif args['broken-link']
        then extra = extra .. string.format('Archived from the [%s original] at %s. ', args['url'], args['acceess-date'] or args['archive-date'])
    end
    
    refoutput = refoutput .. ' ' .. extra
    
    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.