This page contains Lua code for Template:Ref.
-- 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 section then
return string.format('%s (%s). %s in %s.', author, date, section, title)
else
return string.format('%s (%s). %s.', author, date, title)
end
end
local function gameref(game)
game = string.lower(game)
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']
return string.format("%s (%s). ''" .. "[" .. "[" .. "%s|%s]]''. [[wikipedia:Sony Interactive Entertainment|Sony Interactive Entertainment]].", dev, releasedate, page, title, platform)
end
local function guideref(game)
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']
return string.format("%s (%s). ''%s''. %s. [[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]", author, releasedate, title, publisher, isbn, isbn)
end
local function movieref(movie)
movie = string.lower(movie)
assert(data[movie]['media'] == 'movie', "Did not reference a book")
local director = data[movie]['director']
local releasedate = lang:formatDate('F Y', data[movie]['release'])
local title = data[movie]['title']
local studio = data[movie]['studio']
return string.format("%s (%s). ''%s''. %s.", director, releasedate, title, studio)
end
local function bookref(book)
book = string.lower(book)
assert(data[book]['media'] == 'book', "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']
return string.format("%s (%s). ''%s''. %s. [[wikipedia:ISBN|ISBN]] [[Special:Booksources/%s|%s]]", author, releasedate, title, 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'])
elseif args['guide']
then refoutput = guideref(args['guide'])
elseif args['movie']
then refoutput = movieref(args['movie'])
elseif args['book']
then refoutput = bookref(args['book'])
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