FANDOM


-- <nowiki>
-- 
-- This module implements [[Template:Cite web]].
 
local p = {}
 
-- Lazily load a mw.language object.
local lang
 
local function makeCategoryLink(cat)
	return string.format('[[Category:%s]]', cat)
end
 
local function makeWikitextError(msg)
	local ret = ''
	ret = ret .. string.format(
		'<strong class="error">[[Template:Cite web]] error: %s.</strong>',
		msg
	)
	if mw.title.getCurrentTitle().namespace == 0 then
		ret = ret .. makeCategoryLink('Pages with template parameter errors')
	end
	return ret
end
 
local function makeDateLink(date, year)
	lang = lang or mw.language.getContentLanguage()
	local success, dateLink = pcall(
		lang.formatDate, lang,
		'[[F j]], [[Y]]',
		date
	)
	if success and dateLink then
		if year then
			dateLink = dateLink .. string.format(', [[%s]]', year)
		end
		return dateLink
	end
end
 
local function makeDate(date, year, month)
	local ret
	if date then
		ret = date
	elseif year then
		if month then
			ret = month .. ' ' .. year
		else
			ret = year
		end
	end
	if ret then
		return string.format(' (%s)', ret)
	end
end
 
local function makeAccessdateBlurb(monthDay, year, yearArgName)
		year = year or yearArgName
		return string.format(' Retrieved on %s, %s.', monthDay, year)
end
 
function p._main(args)
	-- Validate input
	if not args.url or not args.title then
		return makeWikitextError("parameters '''url''' and '''title''' must be specified")
	end
	if args.archiveurl and not args.archivedate or args.archivedate and not args.archiveurl then
		return makeWikitextError(
			"parameters '''archiveurl''' and '''archivedate''' must be both specified or both omitted"
		)
	end
 
	local ret = {}
 
	-- Authors
	if args.author or args.last then
		local display
		if args.last then
			display = args.last
			if args.first then
				display = display .. ', ' .. args.first
			end
		else
			display = args.author
		end
		if args.authorlink then
			ret[#ret + 1] = string.format('[[%s|%s]]', args.authorlink, display)
		else
			ret[#ret + 1] = display
		end
		if args.coauthors then
			ret[#ret + 1] = '; ' .. args.coauthors
		end
		ret[#ret + 1] = makeDate(args.date, args.year, args.month)
		ret[#ret + 1] = '. '
	end
 
	-- Editor
	if args.editor then
		ret[#ret + 1] = ' ' .. args.editor .. ':'
	end
 
	-- URL
	if args.title then
		if args.archiveurl then
			ret[#ret + 1] = string.format('[%s %s]', args.archiveurl, args.title)
		elseif args.url then
			ret[#ret + 1] = string.format('[%s %s]', args.url, args.title)
		end
	end
 
	-- Language
	if args.language then
		ret[#ret + 1] = string.format(
			' <span style="font-size: 0.95em; font-weight: bold; color:#555; position: relative;">(%s)</span>',
			args.language
		)
	end
 
	-- Format
	if args.format then
		ret[#ret + 1] = string.format(' (%s)', args.format)
	end
 
	-- Work
	if args.work then
		ret[#ret + 1] = string.format(". ''%s''", args.work)
	end
 
	-- Pages
	if args.pages then
		ret[#ret + 1] = ' ' .. args.pages
	end
 
	-- Publisher
	if args.publisher then
		ret[#ret + 1] = '. ' .. args.publisher
	end
 
	-- Add date for when no author is specified.
	if not args.author and not args.last then
		ret[#ret + 1] = makeDate(args.date, args.year, args.month)
	end
 
	ret[#ret + 1] = '.'
 
	-- Archive date.
	if args.archivedate then
		local url = args.url or '{{{url}}}'
		local dateLink = makeDateLink(args.archivedate, args.archiveyear)
		if dateLink then
			ret[#ret + 1] = string.format(
				' Archived from [%s the original] on %s.',
				url, dateLink
			)
		end
	end
 
	-- Access date
	if args.accessdate then
		local dateLink = makeDateLink(args.accessdate, args.accessyear)
		if dateLink then
			ret[#ret + 1] = string.format(' Retrieved on %s.', dateLink)
		end
	elseif args.accessmonthday then
		ret[#ret + 1] = makeAccessdateBlurb(
			args.accessmonthday,
			args.accessyear,
			'{{{accessyear}}}'
		)
	elseif args.accessdaymonth then
		ret[#ret + 1] = makeAccessdateBlurb(
			args.accessdaymonth,
			args.accessyear,
			'{{{accessyear}}}'
		)
	end
 
	-- Quote
	if args.quote then
		ret[#ret + 1] = string.format('&nbsp;"%s"', args.quote)
	end
 
	return table.concat(ret)
end
 
function p.main(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end
 
return p
 
-- </nowiki>

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.