Modul:Sandkasse/Chameleon222/Test

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon


--
-- Creates a link or cite template based on a UN document symbol id
--

-- Required modules
require('strict')
local getArgs = require('Module:Arguments').getArgs
--local yesno = require('Module:Yesno')

-- Global constants/variables
local organs = {
		A  = '[[FNs generalforsamling]]', -- General Assembly
		S  = '[[FNs sikkerhetsråd]]', -- Security Council
		E  = '[[FNs økonomiske og sosiale råd]]', -- Economic and Social Council
		T  = '[[FNs tilsynsråd]]', -- Trusteeship Council
		ST = '[[FN-sekretariatet]]' -- Secretariat
	}

local docTypes = {
		RES = '[[FN-resolusjon|resolusjon]]', -- Resolution
		PET = '[[petisjon]]' -- Petition
	}

--[[ Info on the UN document ID:
		http://research.un.org/en/docs/symbols
		http://documents.un.org/help_E.htm
		http://www.undocs.org/ST/LIB/SER.B/5/Rev.5 ]]
local function decodeSymbolId(symbolId)
	local parts = mw.text.split(symbolId, '/', true)
	if parts == nil or #parts == 0 then return nil end
	local docInfo = {}
	docInfo['organ'] = organs[parts[1]]
	docInfo['type'] = docTypes[parts[2]]
	if parts[2] == 'RES' then -- Resolution
		if parts[1] == 'A' then -- General Assembly Resolution
			docInfo['session'] = parts[3]
			docInfo['resolution'] = parts[4]
		elseif parts[1] == 'S' then -- Security Council Resolution
			docInfo['resolution'] = parts[3]
		end
		docInfo['document'] = parts[3]
		if parts[4] and #parts[4] ~= 0 then
			docInfo['document'] = docInfo['document'] .. '/' .. parts[4]
		end
	end
	
	return docInfo
end

-- Returns a raw/unformatted URL to an UN document with a specified symbol id
local function docLinkURL(symbolId)
	return 'http://www.undocs.org/' .. mw.uri.encode(symbolId, 'PATH');
end

-- Returns an external link to an UN document with a specified symbol id
-- If title is supplied then it is used as link text, else the symbol id is used as a link text
local function docLink(symbolId, title)
	local link = '[' .. docLinkURL(symbolId) .. ' ';
	if title ~= nil and title ~= '' then
		link = link .. title
	else
		link = link .. symbolId
	end
	link = link .. ']'
	return link	
	--[[local link = mw.html.create('a')
	link:attr('rel', 'nofollow')
	link:attr('class', 'external text')
	link:attr('href', docLinkURL(symbolId))
	if title ~= nil and title ~= '' then
		link:wikitext(title)
	else
		link:wikitext(symbolId)
	end
	link:allDone()
	return tostring(link) ]]
end

-- Returns a wiki formatted cite for document
-- See also 'How should I cite a United Nations document?' (http://ask.un.org/faq/14438)
local function docCite(symbolId, title, docDate, accessDate)
	local span = mw.html.create('span')
	span:attr('class', 'citation web')
	
	if #title == 0 then title = symbolId end
	span:wikitext(docLink(symbolId, '«' .. title .. '»'))
	
	span:wikitext(' ')
	local language = span:tag('small')
	language:attr('class', 'tekst-graa')
	language:tag('b'):wikitext('(engelsk)')
	language:done()
	span:wikitext('.')
	
	local docInfo = decodeSymbolId(symbolId)
	if docInfo then
		if docInfo.organ and #docInfo.organ ~= 0 then
			span:wikitext(' ' .. docInfo.organ)
			if docInfo.type and #docInfo.type ~= 0 then
				span:wikitext(', ' .. docInfo.type)
				if docInfo.document and #docInfo.document ~= 0 then
					span:wikitext(' ' .. docInfo.document)
				end
			end
			span:wikitext('.')
		end
	end
	
	if docDate and #docDate ~= 0 then
		span:wikitext(' ' .. docDato .. '.')
	end
	
	if accessDate and #accessDate ~= 0 then
		span:wikitext(' Besøkt ' .. accessDate .. '.')
	end
	
	span:allDone()
	return tostring(span)
end

--
-- Misc for decoding arguments from templates
--
local function selectStr(str1, str2)
	if str1 and #str1 ~= 0 then return str1 end
	return str2 or ''
end

local function decodeArgs(argsIn, minimumDecode)
	local args = getArgs(argsIn, {trim = true, removeBlanks = false, parentFirst = true})
	local argsDecoded = {}
	argsDecoded['symbolId'] = selectStr(argsIn['symbolid'], args[1])
	if argsDecoded['symbolId'] == '' then error('Uttrykksfeil: Argument for symbolid mangler', 0) end
	argsDecoded['title'] = selectStr(args['title'], args[2])
	if minimumDecode then return argsDecoded end
	argsDecoded['date'] = selectStr(args['date'], nil)
	argsDecoded['accessDate'] = selectStr(args['accessdate'], nil)
	return argsDecoded
end

--
-- Interface for templates
--
local p = {}

function p.link(frame)
	local args = decodeArgs(frame.args, true)
	return docLink(args.symbolId, args.title)
end

function p.cite(frame)
	local args = decodeArgs(frame.args, false)
	return docCite(args.symbolId, args.title, args.date, args.accessDate)
end

return p