Module:References

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:References/doc. [edit]
Module:References is invoked by .
Module:References requires Module:Mainonly.
Module:References requires Module:Paramtest.
Module:References requires Module:Yesno.
Function list
L 9 — string.starts
L 20 — reftag
L 44 — plainref
L 80 — p.reflist

local onmain = require('Module:Mainonly').on_main
--local tag = require('Module:Reftag')
local paramtest = require('Module:Paramtest')
local hc = paramtest.has_content
local dt = paramtest.default_to
local yn = require('Module:Yesno')

--function to check if a string starts with a specific substring. Used for verification in formatting in CiteVideo and a dead archive check in CiteGeneral.
function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

local p = {} --all template entrance points here
local r = {} --reference formatters here to not expose them to #invoke, and to not clutter p

local lostReferenceMsg = '<sup title="The page referenced can no longer be accessed. Please try to find an archived version of it or another reference for this statement if possible.">Lost reference</sup>'
local unarchivedReferenceMsg = '<span class="unarchived-ref" title="This reference has not been archived">*</span>'

--create the ref tag to be returned
local function reftag(frame, reftype, forcedArgs)
	local args = frame:getParent().args
	local s = ''
	local opts = {}
	
	if forcedArgs ~= nil then
		for k, v in pairs(forcedArgs) do
			args[k] = v
		end
	end
	
	if hc(args.name) then
		opts.name = args.name
	end
	if hc(args.group) then
		opts.group = args.group
	end
	
	local ref, cat = r[reftype](args)
	
	return frame:extensionTag{name = 'ref', content = ref .. cat, args = opts}
	--return tag(opts) .. cat
end

local function plainref(frame, reftype, forcedArgs)
	local args = frame:getParent().args
	
	if forcedArgs ~= nil then
		for k, v in pairs(forcedArgs) do
			args[k] = v
		end
	end
	
	local ref, cat = r[reftype](args)
	return ref .. cat
end

------------ Helper Templates -----------------

--[==[
[[Template:NamedRef]]
--]==]
p.namedref = function(frame)
	local args = frame:getParent().args
	local opts = {}
	opts.name = args.name or args[1]
	if hc(args.group) then
		opts.group = args.group
	elseif hc(args[2]) then
		opts.group = args[2]
	end
	--opts.selfClosing = true
	return frame:extensionTag{name = 'ref', args = opts}
	--return tag(opts)
end


--[==[
[[Template:Reflist]]
--]==]
function p.reflist(frame)
	local args = frame:getParent().args
	local div = mw.html.create('div')
	div:wikitext(frame:extensionTag{
		name = "references",
		content = dt(args.refs, nil),
		args = { group = args.group },
	});
	div:addClass('reflist')
	if not yn(args.quotes) then
		div:addClass('hideQuotes')
	end
	
	-- Implement {{Notelist}}
	if args.group == 'lower-alpha' then
		div:addClass('reflist-lower-alpha')
	end
	
	if args.colwidth then
		div :css({  ['-moz-column-width'] = args.colwidth,
					['-webkit-column-width'] = args.colwidth,
					['column-width'] = args.colwidth
				})
	end
	if args[1] then
		div :wikitext('[[Category:Pages with deprecated parameters]]')
	end
	
	return div
end

------------Marvel Studios Citation templates--------------
--[==[
[[Template:CiteNews]] and [[Template:PlainCiteNews]]
--]==]
p.newsref = function(frame)
	return reftag(frame, 'news')
end

p.newsplain = function(frame)
	return plainref(frame, 'news')
end

r.news = function(a)
	local ref
	local c = ''
	if hc(a.author) then
		ref = a.author .. '. '
	else
		ref = 'Marvel Studios. '
	end
	
	if hc(a.url) then --url, if present makes a link
		ref = ref .. '[' .. a.url
		if hc(a.title) then
			ref = ref .. ' "' .. a.title .. '."'--if title not present leave as link
		end
		ref = ref .. ']'
	else
		if hc(a.title) then
			ref = ref .. '"' .. a.title .. '."'
		end
	end
	ref = ref .. ' '
	if hc(a.newsdate) then
		ref = ref .. a.newsdate .. '.'
	end
	ref = ref .. " Marvel.com. "
	ref = ref .. (a.notes or '')
	
	if onmain() and not (hc(a.url) and hc(a.title) and hc(a.newsdate)) then
		c = c .. '[[Category:Incomplete references]]'
	end
	return ref, c
end


--[==[
[[Template:CiteCharacter]] and [[Template:PlainCiteCharacter]]
--]==]
p.characterref = function(frame)
	return reftag(frame, 'character')
end

p.characterplain = function(frame)
	return plainref(frame, 'character')
end

r.character = function(a)
	local ref = ''
	local c = ''
	if hc(a.character) then
		ref = ref .. '[[' .. a.character .. ']], '
	end
	if hc(a.media) then
		ref = ref .. '"[[' .. a.media .. ']]", '
	end
	ref = ref .. "Marvel Cinematic Universe. "
	
	local islist
	if hc(a.quote) then
		islist = mw.ustring.match( a.quote, '^%s*%*.*\n%*' )
		local quote = islist and ('\n' .. a.quote) or '"' .. a.quote .. '" '
		local span = mw.html.create('span')
		if islist then
			span:newline()
		end
		span:wikitext(quote)
		ref = ref .. tostring(span)
	end
	
	ref = ref .. (a.notes or '')
	
	if onmain() and not ((hc(a.character) or islist) and hc(a.quote)) then
		c = c .. '[[Category:Incomplete references]]'
	end
	return ref, c
end

--[==[
[[Template:CiteText]] and [[Template:PlainCiteText]]
--]==]
p.textref = function(frame)
	return reftag(frame, 'text')
end

p.textplain = function(frame)
	return plainref(frame, 'text')
end

r.text = function(a)
	local ref = ''
	local c = ''
	if hc(a.text) then
		ref = ref .. '[[' .. a.text .. ']], '
	end
	if hc(a.author) then
	    ref = ref .. 'written by [[' .. a.author .. ']], '
	end
	if hc(a.page) then
	    ref = ref .. 'page ' .. a.page .. ', '
	end
	if hc(a.media) then
		ref = ref .. '"[[' .. a.media .. ']]", '
	end
	ref = ref .. "Marvel Cinematic Universe. "
	
	if hc(a.quote) then
		local span =	mw.html.create('span')
							:wikitext('"' .. a.quote .. '" ')
		ref = ref .. tostring(span)
	end
	
	ref = ref .. (a.notes or '')
	
	if onmain() and not (hc(a.text) and hc(a.quote)) then
		c = c .. '[[Category:Incomplete references]]'
	end
	return ref, c
end


------------ General Citation Templates -----------------


--[==[
[[Template:CitePub]] and [[Template:PlainCitePub]]
--]==]
p.pubref = function(frame)
	return reftag(frame, 'pub')
end

p.pubplain = function(frame)
	return plainref(frame, 'pub')
end

r.pub = function(a)
	local ref = ''
	local c = ''
	
	if hc(a.author) then
		ref = a.author .. '. '
	end
	
	local str = ''
	if hc(a.title) then
		ref = ref .. "''" .. a.title .. "''"
		if hc(a.pages) then
			ref = ref .. ', (' .. a.pages .. ')'
		end
		ref = ref .. '. '
	end
	if hc(a.publisher) then
		ref = ref .. a.publisher
		if hc(a.pubyear) then
			ref = ref .. ', ' .. a.pubyear
		end
		ref = ref .. '. '
	elseif hc(a.pubyear) then
		ref = ref .. a.pubyear .. '. '
	end
	ref = ref .. (a.notes or '')
	
	if onmain() and not (hc(a.title) and hc(a.publisher) and hc(a.pubyear)) then
		c = c .. '[[Category:Incomplete references]]'
	end
	return ref, c
end


--[==[
[[Template:CiteTwitter]] and [[Template:PlainCiteTwitter]]
--]==]
p.twitterref = function(frame)
	return reftag(frame, 'twitter')
end

p.twitterplain = function(frame)
	return plainref(frame, 'twitter')
end

r.twitter = function(a)
	local ref
	local c = ''
	ref = 'Marvel Studios. '
	
	if hc(a.url) or hc(a.archiveurl) then --url, if present makes a link
		if hc(a.archiveurl) then
			ref = ref .. '[' .. a.archiveurl
		elseif hc(a.url) then
			ref = ref .. '[' .. a.url
		end
		if hc(a.author) then
			ref = ref .. ' ' .. a.author .. "'s Twitter account"--if title not present leave as link
		end
		ref = ref .. ']. '
	elseif hc(a.author) then
		ref = ref .. ' ' .. a.author .. "'s Twitter account. "
	end
	
	if hc(a.date) then
		ref = ref .. a.date .. '. '
	end
	
	if hc(a.archivedate) then
		ref = ref .. ' (Archived from [' .. a.url .. ' the original] on ' .. a.archivedate .. '.) '
	end
	
	if hc(a.quote) then
		if hc(a.author) then
			ref = ref .. a.author .. ': '
		end
		ref = ref .. '"' .. a.quote .. '" '
	end
	ref = ref .. (a.notes or '')
	if hc(a.lost) then
		ref = ref .. lostReferenceMsg
	end
	
	local err = ''
	if not (hc(a.url) and hc(a.author) and hc(a.quote) and hc(a.date)) then
		err = err .. "Error calling CiteTwitter: parameters '''url''', '''author''', '''quote''', and '''date''' must be specified.<br />"
	end
	
	if hc(a.archiveurl) ~= hc(a.archivedate) then --xor
		err = err .. "Error calling CiteTwitter: parameters '''archiveurl''' and '''archivedate''' must both be specified or both be unspecified.<br />"
	end
	
	
	if onmain() and err ~= '' then
		c = c .. '[[Category:Incomplete references]]'
	end
	
	if onmain() and not hc(a.archiveurl) then
		if hc(a.lost) then
			c = c .. '[[Category:Lost Twitter references]]'
		else
			c = c .. '[[Category:Unarchived Twitter references]]'
			ref = ref .. unarchivedReferenceMsg
		end
	end
	
	return ref, c
end

--[==[
[[Template:CiteVideo]] and [[Template:PlainCiteVideo]]
--]==]
p.videoref = function(frame)
	return reftag(frame, 'video')
end

p.videoplain = function(frame)
	return plainref(frame, 'video')
end

r.video = function(a)
	local ref = ''
	local c = ''
	if hc(a.author) then
		ref = ref .. a.author .. '. '
	end
	if not a.site then --default site to youtube
		a.site = 'YouTube'
	end
	local url = a.url;
	if hc(url) then --url, if present makes a link
		if string.starts(url, "//") then
			url = "https:" .. url;
		elseif string.starts(url, "http:") then
			url = "https:" .. mw.ustring.sub(url, 6);
		end
		if string.lower(a.site) == 'youtube' then
			if string.starts(url, 'https://www.youtube.com/watch?v=') then
				ref = ref .. '[' .. url
			elseif string.starts(url, "https://youtu.be/") then
				ref = ref .. "[https://www.youtube.com/watch?v=" .. mw.ustring.gsub(mw.ustring.sub(url, 18), "%?", "&", 1);
			else
				ref = ref .. '[https://www.youtube.com/watch?v=' .. url
			end
		elseif string.lower(a.site) == 'twitch' then
			if string.starts(url, 'https://www.twitch.tv/videos/') then
				ref = ref .. '[' .. url
			elseif string.starts(url, 'https://clips.twitch.tv/') then
				ref = ref .. '[' .. url
			else
				ref = ref .. '[https://www.twitch.tv/videos/' .. url
			end
		else
			ref = ref .. '[' .. a.url
		end
		if hc(a.title) then
			ref = ref .. ' "' .. a.title .. '."'--if title not present leave as link
		end
		ref = ref .. ']'
	else
		if hc(a.title) then
			ref = ref .. '"' .. a.title .. '."'
		end
	end
	ref = ref .. ' '
	if hc(a.timestamp) then
		ref = ref ..'(' .. a.timestamp .. '),'
		end
	ref = ref .. " " .. dt(a.site, 'YouTube') .. " video. "
	ref = ref .. ' '
	if hc(a.date) then
		ref = ref .. a.date .. '. '
	end
	if hc(a.quote) then
		ref = ref .. '"' .. a.quote .. '" '
	end
	ref = ref .. (a.notes or '')

	if hc(a.lost) then
		ref = ref .. lostReferenceMsg
		c = c .. '[[Category:Lost video references]]'
	end

	if onmain() then
		if not (hc(a.author) and hc(a.url) and hc(a.title) and hc(a.date)) then
			c = c .. '[[Category:Incomplete references]]'
		end
	end
	return ref, c
end

--[==[
[[Template:CiteGeneral]] and [[Template:PlainCiteGeneral]]
--]==]
p.generalref = function(frame)
	return reftag(frame, 'general')
end

p.generalplain = function(frame)
	return plainref(frame, 'general')
end

r.general = function(a)
	local ref = ''
	local c = ''
	local str = ''

	-- WebCitation is dead. This will mark the citation is lost if the url is a webcitation link but not if the link points to the Internet Archive.
	if string.find(a.url, 'webcitation.org', 1, true) and not string.starts(a.url, 'https://web.archive.org/') then
		a.lost = 'yes'
	end
	if hc(a.archiveurl) then -- This will invalidate webcitation.org archive urls and handle them as if the archive wasn't there.
	    if string.find(a.archiveurl, 'webcitation.org', 1, true) and not string.starts(a.archiveurl, 'https://web.archive.org/') then
		    a.archiveurl = ''
		    a.archivedate = ''
	    end
	end

	if hc(a.author) or hc(a.last) then
		if hc(a.last) then
			str = a.last
			if hc(a.first) then
				str = str .. ', ' .. a.first
			end
		else
			str = a.author
		end
		
		if hc(a.authorlink) then
			ref = ref .. '[' .. a.authorlink
			if str ~= '' then
				ref = ref .. ' ' .. str
			end
			ref = ref .. ']'
		else
			ref = ref .. str
		end
		
		if hc(a.coauthors) then
			ref = ref .. '; ' .. a.coauthors
		end
		
		ref = ref .. '. '
	end
	
	if hc(a.title) then
		if hc(a.archiveurl) then
			ref = ref .. '[' .. a.archiveurl .. ' ' .. a.title .. ']'
		elseif hc(a.url) then
			ref = ref .. '[' .. a.url .. ' ' .. a.title .. ']'
		end
	end
	
	if hc(a.language) then
		local span =	mw.html.create('span')
							:css({['font-size'] = '0.95em', color = '#555', position = 'relative', ['font-weight'] = 'bold'})
							:wikitext('(' .. a.language .. ')')
		ref = ref .. ' ' .. tostring(span)
	end
	
	if hc(a.format) then
		ref = ref .. ' (' .. a.format .. ')'
	end
	
	if hc(a.work) then
		ref = ref .. ". ''" .. a.work .. "''"
	end
	
	if hc(a.pages) then
		ref = ref .. ' pp. ' .. a.pages
	end
	
	if hc(a.publisher) then
		ref = ref .. '. ' .. a.publisher
	end
	
	if hc(a.date) then
		ref = ref .. '. ' .. a.date
	elseif hc(a.year) then
		if hc(a.month) then
			ref = ref .. '. ' .. a.month .. ' ' .. a.year
		else
			ref = ref .. '. ' .. a.year
		end
	end
	
	if ref ~= '' then
		ref = ref .. '.'
	end
	
	if hc(a.archivedate) then
		ref = ref .. ' (Archived from [' .. a.url .. ' the original] on ' .. a.archivedate .. '.)'
	end
	
	if hc(a.quote) then
		ref = ref .. ' "' .. a.quote .. '"'
	end
	
	if hc(a.notes) then
		ref = ref .. ' ' .. a.notes
	end
	
	if hc(a.lost) then
		ref = ref .. lostReferenceMsg
	end
	
	local err = ''
	if not (hc(a.url) and hc(a.title)) then
		err = err .. "Error calling CiteGeneral: parameters '''url''' and '''title''' must be specified.<br />"
	end
	if hc(a.archiveurl) ~= hc(a.archivedate) then --xor
		err = err .. "Error calling CiteGeneral: parameters '''archiveurl''' and '''archivedate''' must both be specified or both be unspecified.<br />"
	end
	if not (hc(a.archiveurl) and hc(a.archivedate)) then
		ref = err .. ref .. mw.getCurrentFrame():expandTemplate{title = 'RefDate', args = { (a.accessdate or ''), '180'} }
	end
	
	if onmain() and err ~= '' then
		c = c .. '[[Category:Incomplete references]]'
	end
	
	if onmain() and not hc(a.archiveurl) then
		if hc(a.lost) then
			c = c .. '[[Category:Lost web references]]'
		else
			c = c .. '[[Category:Unarchived web references]]'
			ref = ref .. unarchivedReferenceMsg
		end
	end

	return ref, c
end

--[==[
[[Template:CiteReddit]] and [[Template:PlainCiteReddit]]
--]==]
p.redditref = function(frame)
	return reftag(frame, 'reddit')
end

p.redditplain = function(frame)
	return plainref(frame, 'reddit')
end

r.reddit = function(a)
	local ref
	local c = ''
	ref = 'Marvel Studios. '
	ref = dt(a.author, 'Marvel Studios') --author, defaults to Marvel Studios
	ref = ref .. '. '
	if hc(a.url) or hc(a.archiveurl) then --url, if present makes a link
		if hc(a.archiveurl) then
			ref = ref .. '[' .. a.archiveurl
		elseif hc(a.url) then
			ref = ref .. '[' .. a.url
		end
		if hc(a.title) then
			ref = ref .. ' ' .. a.title
		end
		ref = ref .. ']. '
	end
	ref = ref .. "''Reddit''. "
	if hc(a.date) then
		ref = ref .. a.date .. '. '
	end
	
	if hc(a.archivedate) then
		ref = ref .. ' (Archived from [' .. a.url .. ' the original] on ' .. a.archivedate .. '.) '
	end
	
	if hc(a.quote) then
		if hc(a.author) then
			ref = ref .. a.author .. ': '
		end
		ref = ref .. '"' .. a.quote .. '" '
	end
	ref = ref .. (a.notes or '')
	if hc(a.lost) then
		ref = ref .. lostReferenceMsg
	end
	
	local err = ''
	if not (hc(a.url) and hc(a.author) and hc(a.quote) and hc(a.date) and hc(a.title)) then
		err = err .. "Error calling CiteReddit: parameters '''url''', '''author''', '''quote''', and '''date''', and '''title''' must be specified.<br />"
	end
	
	if hc(a.archiveurl) ~= hc(a.archivedate) then --xor
		err = err .. "Error calling CiteReddit: parameters '''archiveurl''' and '''archivedate''' must both be specified or both be unspecified.<br />"
	end
	
	if onmain() and err ~= '' then
		c = c .. '[[Category:Incomplete references]]'
	end
	
	if onmain() and not hc(a.archiveurl) then
		c = c .. '[[Category:Unarchived Reddit references]]'
			ref = ref .. unarchivedReferenceMsg
	end
	
	return ref, c
end

return p