Moduuli:Cite web

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 18. syyskuuta 2024
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Cite web/ohje

-- <nowiki>
-- 
-- This module implements [[Template:Cite web]].
 
local p = {}
 
-- Lazily load a mw.language object.
local lang
 
local function makeCategoryLink(cat)
	return string.format('[[Luokka:%s]]', cat)
end
 
local function makeWikitextError(msg)
	local ret = ''
	ret = ret .. string.format(
		'<strong class="error">[[Malline:Cite web]] virhe: %s.</strong>',
		msg
	)
	if mw.title.getCurrentTitle().namespace == 0 then
		ret = ret .. makeCategoryLink('Sivut, joissa on mallineparametrivirheitä')
	end
	return ret
end
 
local function makeDateLink(date, year)
	lang = lang or mw.language.getContentLanguage()
	local success, dateLink = pcall(
		lang.formatDate, lang,
		'[[j. F"ta"]] [[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(' Haettu %s %s.', monthDay, year)
end
 
function p._main(args)
	-- Validate input
	if not args.url or not args.title then
		return makeWikitextError("parametrit '''url''' ja '''title''' on määritettävä")
	end
 
	local ret = {}
 
	-- Authors
	if args.author then
		ret[#ret + 1] = args.author
		ret[#ret + 1] = '. '
	end
 
	-- URL
	if args.title then
		if args.archiveurl then
			ret[#ret + 1] = string.format('[%s %s]', args.archiveurl, args.title)
		elseif args.archivedate then
			ret[#ret + 1] = string.format('[https://web.archive.org/web/%s/%s %s]', args.archivedate, args.url, 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
	
	-- Add date for when no author is specified.
	if args.date or args.year or args.month then
		ret[#ret + 1] = makeDate(args.date, args.year, args.month)
	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

	ret[#ret + 1] = '.'

	-- Archive date.
	if args.archiveurl or args.archivedate then
		local url = args.url or '{{{url}}}'
		local dateLink = makeDateLink(args.archivedate, args.archiveyear)
		if dateLink then
			ret[#ret + 1] = string.format(
				' Arkistoitu [%s alkuperäisestä] %s.',
				url, dateLink
			)
		else
			ret[#ret + 1] = string.format(' Arkistoitu [%s alkuperäisestä].', url)
		end
	end
	
	if args.nolive then
		ret[#ret + 1] = ' (sisältö ei enää saatavilla)'
	end
 
	if not args.archiveurl and not args.archivedate and not args.nobackup then
		if mw.title.getCurrentTitle().namespace == 0 or mw.title.getCurrentTitle().namespace == 6 then
			ret[#ret + 1] = makeCategoryLink('Sivut, joissa on puuttuvia arkistolinkkejä')
		end
	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>