Module:Sandbox/Ahecht/Cite DNV

This is an old revision of this page, as edited by Ahecht (talk | contribs) at 17:23, 17 May 2016 (bugfix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local htmlerror = require('Module:Error').error

function p.citednv(frame)
	local origArgs = frame:getParent().args
	local args = {}
	local success=true
	local register="DNV"
	local outstr = ""
	
	for k, v in pairs(origArgs) do
		args[k] = v
	end
	
	url, vesselid = string.match(string.lower(args['url'] or ''),'//exchange.dnv.com/exchange/main.aspx?(.*)vesselid=(g?%d%d%d%d%d%d?)')
	vesselid = (vesselid or string.lower(((args['id'] or args['ID']) or args['vesselid']) or '') )
	if vesselid == '' then
		success=false
	elseif not tonumber(vesselid) then
		vesselid = string.upper(vesselid)
		register = "GL"
	elseif vesselid > 99999 then
		vesselid = "G"..vesselid
		register = "GL"
	end

	shipname = ((string.match((args['title'] or ''),'\'\'(.-)\'\'') or args['title']) or (args['shipname'] or args['name'])) or ''
	if shipname == '' then success=false end
	
	if success then
		return  '{{cite ship register|register='..register..'|id='..vesselid..'|shipname='..shipname..'|accessdate='..((args['accessdate'] or args['access-date']) or '')..'}'
	elseif args['url'] then
		outstr = '{{cite web'
		for k, v in pairs(origArgs) do
			outstr = outstr..'|'..k..'='..v
		end
		return outstr..'}}'
	else
		return htmlerror{'Error in [[Template:Cite DNV]]: Invalid parameters.'} 
	end
	
end

return p