Jump to content

Module:Tzitatzione

Dae Wikipedia, s'entziclopedia lìbera.

Documentation for this module may be created at Module:Tzitatzione/doc

-- Mòdulu pro sa gestione de sas tzitatziones

--[[ ===============================================================================
Variabile in ue benit ammentadas sas conditziones de errore registradas durante s'esecutzione
de sas funtziones de su mòdulu.
	===============================================================================]]
local z = {
	error_categories = {}; -- lista de sas categorias de errore
	error_ids = {}; -- lista de sos còdighes de errore
	message_tail = {}; -- messàgios de errore de ammustrare a s'acabu de sa tzitatzione
}

--[[ ===============================================================================
Carrigamentu de sas tabellas de configuratzione de su mòdulu.
	===============================================================================]]
local cfg = mw.loadData( 'Module:Tzitatzione/Configuratzione');

--[[ ===============================================================================
Lista de totu sos paràmetros reconnotos.
	===============================================================================]]
local whitelist = mw.loadData( 'Module:Tzitatzione/Whitelist' );

--[[ ===============================================================================
Torrat true si una variàbile est impostada (diferente dae nil e dae istringa bòida)
	===============================================================================]]
local function is_set( var )
	return not (var == nil or var == '');
end

-- Imprenta su nostru {{Ligàmene interrùmpidu}}
local function interrupted_url()
	return mw.getCurrentFrame():expandTemplate{ title = 'Ligàmene interrùmpidu' }
end

--[[ ===============================================================================
Torrat sa prima variàbile impostada intre cussas coladas a sa funtzione
	===============================================================================]]
local function first_set(...)
	local list = {...};
	for _, var in pairs(list) do
		if is_set( var ) then
			return var;
		end
	end
end

--[[ ===============================================================================
Torrat sa positzione de needle in sa lista haystack, si nono torrat false
	===============================================================================]]
local function in_array( needle, haystack )
	if needle == nil then return false; end
	for n,v in ipairs( haystack ) do
		if v == needle then return n; end
	end
	return false;
end

--[[ ===============================================================================
Pòbulat sos argumentos numerados in s'istringa msg impreende sa tabella de argumentos args
	===============================================================================]]
local function substitute( msg, args )
	return args and mw.message.newRawMessage( msg, args ):plain() or msg;
end

--[[ ===============================================================================
Faghet in modu chi s'istringa siat segura pro su markup corsivu '' ... ''
Ammenta·ti: non faghet a impreare <i> pro su corsivu ca su cumportamentu isetadu si lu si dislindat
pro sos tìtulos est de los fàghere non cursivos. Annotamala <i> e '' interagint
male cun sa funtzione HTML tidy de Mediawiki
	===============================================================================]]
local function safe_for_italics( str )
	if not is_set(str) then
		return str;
	else
		if str:sub(1,1) == "'" then str = "<span></span>" .. str; end
		if str:sub(-1,-1) == "'" then str = str .. "<span></span>"; end

		-- Bogat sas achirradas a capu ca truncant su corsivu.
		return str:gsub( '\n', ' ' );
	end
end

--[[ ===============================================================================
Torrat true/false a segunda chi s'istringa siat esclusivamente in caràteres latinos o nono
	===============================================================================]]
local function is_latin( str )
	return require('Module:IsLatin')._IsLatin({ str })
end

--[[ ===============================================================================
Frunit unu messàgiu de sa tabella cfg.messages in ue benit insertada un'istringa
- key: còdighe de su messàgiu de ammustrare in cfg.messages
- str: un'istringa de insertare in su messàgiu, si no est definida o uguale a un'istringa
	bòida sa funtzione torrat un'istringa bòida
	===============================================================================]]
local function wrap( key, str )
	if not is_set( str ) then
		return "";
	elseif in_array( key, { 'italic-title', 'trans-italic-title' } ) then
		str = safe_for_italics( str );
	end
	return substitute( cfg.messages[key], {str} );
end

--[[ ===============================================================================
Insertat unu messàgiu de purificatzione (debug) de ammustrare a s'acabu de sa tzitatzione
	===============================================================================]]
local function debug_msg(msg)
	table.insert( z.message_tail, { set_error( 'debug_txt', {msg}, true ) } );
end

--[[ ===============================================================================
Pro sa punna de sa purificatzione (su debug), annanghet s'istringa 'name=<value>' a s'acabu de sa tzitztzione
	===============================================================================]]
local function debug_value(name, value)
	if not value then value='nil' end
	debug_msg(name .. '="'.. value .. '"')
end

--[[ ===============================================================================
Furmata unu cummentu pro identificare sos errores, annanghende sa classe css pro
lu torrare visìbile o nono
	===============================================================================]]
local function error_comment( content, hidden )
	return wrap( hidden and 'hidden-error' or 'visible-error', content );
end

--[[ ===============================================================================
Impostat una cunditzione de errore e torrat unu messàgiu apropriadu. S'insertada
de su messàgiu in s'output est de responsabilidade de sa funtzione chiamante
-- -- error_id: còdighe de s'errore (una crae bàlida pro cfg.error_conditions)
-- -- arguments: una lista de argumentos optzionales pro sa formatatzione de su messàgiu
-- -- raw: torrat una còpia: {messàgiu de errore, visibilidade} imbetzes de su messàgiu
--       de errore formatadu
-- -- prefix: istringa de annànghere a s'incumintzu de su messàgiu
-- -- suffix: istringa de annànghere a s'acabu de su messàgiu
	===============================================================================]]
local function set_error( error_id, arguments, raw, prefix, suffix )
	local error_state = cfg.error_conditions[ error_id ];

	prefix = prefix or "";
	suffix = suffix or "";

	if error_state == nil then
		error( cfg.messages['undefined_error'] );
	elseif is_set( error_state.category ) then
		table.insert( z.error_categories, error_state.category );
	end

	local message = substitute( error_state.message, arguments );

	message = mw.ustring.format(
		'%s ([[%s#%s|%s]])',
		message, cfg.messages['help page link'], error_state.anchor,
		cfg.messages['help page label']
	)

	z.error_ids[ error_id ] = true;
	if in_array( error_id, { 'bare_url_missing_title', 'trans_missing_title' } )
			and z.error_ids['citation_missing_title'] then
		return '', false;
	end

	message = table.concat({ prefix, message, suffix });
	if raw == true then return message, error_state.hidden end
	return error_comment( message, error_state.hidden );
end

--[[ ===============================================================================
Chircat su primu paràmetru impostadu dae una lista de paràmetros e gènerat un'errore si
prus de unu paràmetru est impostadu.
Torrat sa còpia (value, selected) in ue value est su balore de su paràmetru agatadu e
selected su nùmene de su paràmetru agatadu
	===============================================================================]]
local function select_one( args, possible, error_condition, index )
	local value = nil;
	local selected = '';
	local error_list = {};

	if index ~= nil then index = tostring(index); end

	-- Handle special case of "#" replaced by empty string
	if index == '1' then
		for _, v in ipairs( possible ) do
			v = v:gsub( "#", "" );
			if is_set(args[v]) then
				if value ~= nil and selected ~= v then
					table.insert( error_list, wrap( 'parameter', v ) );
				else
					value = args[v];
					selected = v;
				end
			end
		end
	end

	for _, v in ipairs( possible ) do
		if index ~= nil then
			v = v:gsub( "#", index );
		end
		if is_set(args[v]) then
			if value ~= nil and selected ~= v then
				table.insert( error_list, wrap( 'parameter', v ));
			else
				value = args[v];
				selected = v;
			end
		end
	end

	if #error_list > 0 then
		-- generat su messàgiu errore concatenende sos paràmetros duplicados
		local error_str = "";
		if #error_list == 1 then
			error_str = error_list[1] .. cfg.messages['parameter-pair-separator'];
		else
			error_str = table.concat(error_list, cfg.messages['parameter-separator']) .. cfg.messages['parameter-final-separator'];
		end
		error_str = error_str .. wrap( 'parameter', selected );
		table.insert( z.message_tail, { set_error( error_condition, {error_str}, true ) } );
	end
	return value, selected;
end

--[[ ===============================================================================
Funtzione de suportu pro sa mappadura de sos argumentos de sos documentos de configuratzione,
in manera chi nùmenes mùltiplos podent èssere assignados a una variàbile interna ebbia
	===============================================================================]]
local function argument_wrapper( args )
	local origin = {};

	return setmetatable({
		ORIGIN = function( self, k )
			local dummy = self[k]; --fortzat su carrigamentu de sa variàbile.
			return origin[k];
		end
	},
	{
		__index = function ( tbl, k )
			if origin[k] ~= nil then
				return nil;
			end

			local args, list, v = args, cfg.aliases[k];

			if type( list ) == 'table' then
				v, origin[k] = select_one( args, list, 'redundant_parameters' );
				if origin[k] == nil then
					origin[k] = ''; -- Istringa bòida, non nil
				end
			elseif list ~= nil then
				v, origin[k] = args[list], list;
			else
				-- maybe let through instead of raising an error?
				-- v, origin[k] = args[k], k;
				error( cfg.messages['unknown_argument_map'] );
			end

			-- Istringas bòidas, non nil;
			if v == nil then
				v = cfg.defaults[k] or '';
				origin[k] = '';
			end

			tbl = rawset( tbl, k, v );
			return v;
		end,
	});
end

--[[ ===============================================================================
Verificat chi su nùmene de unu paràmetru siat bàlidu impreende sa whitelist
	===============================================================================]]
local function validate( name )
	name = tostring( name );
	-- Normal arguments
	if whitelist.basic_arguments[ name ] then return true end
	-- Arguments with numbers in them
	name = name:gsub( "%d+", "#" );
	if whitelist.numbered_arguments[ name ] then return true end
	-- Not found, argument not supported.
	return false
end

--[[ ===============================================================================
Ogetu pro ammentare sos elementos de una tzitatzione. Unu frammentu de tzitatzione est
formadu dae custos elementos:
- self[n]: n-èsimu elementu de aunire, est una lista de istringas interrùmpida dae sos
			iscrobadores de impreare pro las unire.
- self.last_priority: prioridade de s'iscobadore de serrada
- self.first_priority: prioridade de s'iscobadore de abertura
- self.sep_key: còdighe de su caràtere iscrobadore predefinidu de impreare
				si unida a un'àteru frammentu
===============================================================================]]
local Fragment = {}

Fragment.precedence = cfg.style.separator_priority
local Fragment_mt = { __index = Fragment }

Fragment.new = function(texts, sep_key)
	if type(texts) == "string" then texts = { texts } end
	if not Fragment.precedence[sep_key] then sep_key = "" end
	local tx = { }
	tx.last_priority = 0
	tx.first_priority = 0
	tx.sep_key = sep_key
	tx[1] = ""
	for _, el in ipairs(texts) do
		if el ~= "" then
			tx[#tx+1] = el
			tx[#tx+1] = Fragment.precedence[tx.sep_key].sep
		end
	end
	if #tx > 1 then
		tx.last_priority = Fragment.precedence[tx.sep_key].order
	else
		tx[1] = ""
	end
	setmetatable(tx, Fragment_mt)
	return tx
end

--- mudat s'iscrobadore de incumintzu de unu frammentu de tzitatzione
function Fragment:start(sep_key)
	if #self == 0 then return self end
	local separator = Fragment.precedence[sep_key] or Fragment.precedence[""]
	self[1] = separator.sep
	self.first_priority = separator.order
	return self
end

-- mudat s'iscrobadore de acabu de unu frammentu de tzitatzione
function Fragment:last(sep_key)
	if #self == 0 then return self end
	local separator = Fragment.precedence[sep_key] or Fragment.precedence[""]
	self[#self] = separator.sep
	self.last_priority = separator.order
	return self
end

-- torrat unu frammentu de tzitatzione bòidu
function Fragment:empty()
	return #self==0
end

-- apicat un'istringa o unu frammentu de tzitatzione a s'acabu
function Fragment:append(txr)
	if txr == nil then return self end
	if type(txr) == "string" then txr = Fragment.new(txr, self.sep_key) end
	if #txr == 0 then return self end
	if #self == 0 then self[1] = txr[1] end
	self.last_priority = self.last_priority or 0
	if self.last_priority < txr.first_priority then
		self[#self] = txr[1]
	end
	for i, el in ipairs(txr) do
		if i>1 then self[#self+1] = el end
	end
	self.last_priority = txr.last_priority
	--self.sep_key = txr.sep_key
	return self
end

-- apicat una lista de istringas o frammentos de tzitatziones
function Fragment:appends(fragments)
	for _,f in ipairs(fragments) do
		self:append(f)
	end
	return self
end

-- collassat su frammentu in un'istringa e la torrat
Fragment_mt.__tostring = function(tx)
	return table.concat(tx, '')
end
-- =====================================================================
-- Acabu definitzione ogetu Fragment
-- =====================================================================

--[[ ===============================================================================
Formatat unu wikilink internu o unu ligàmene esternu a unu documentu
- options.coas_id: còdighe pro su ligàmene esternu
- options.id: eticheta de su ligàmene esternu o id de su documentu
- options.encode: si true o nil s'url benit codificadu (en:Percent-encoding)
- options.link: ligàmene a sa boghe wiki in su còdighe documentu
- options.label: eticheta de su ligàmene a sa boghe wiki in su còdighe documentu
- options.separator: iscrobadore tra còdighe e ligàmene (de default un'ispàtziu unificatore)
- options.pattern: pattern de su ligàmene in ue "$1" est s'id normale e "$2" s'id in s'url esternu
	===============================================================================]]
local function link_id(options)
	local url_string = options.code_id or options.id;
	if options.encode == true or options.encode == nil then
		url_string = mw.uri.encode( url_string );
	end
	for w in mw.ustring.gmatch( options.pattern, '$1' ) do
		options.pattern = mw.ustring.gsub( options.pattern, '^(%[[^%[]%S+)$1(.-%])', '%1$2%2' );
		options.pattern = mw.ustring.gsub( options.pattern, '([^%[]%[[^%[]%S+)$1(.-%])', '%1$2%2' );
	end
	return mw.ustring.format( '[[%s|%s]]%s%s',
		options.link, options.label, options.separator or "&nbsp;",
		substitute( options.pattern, {mw.text.nowiki(options.id), url_string} )
	);
end

--[[ ===============================================================================
Determinat si un'URL est curretu. Pro como verificat petzi chi s'istringa incumentzet cun
unu prefissu URI bàlidu e chi non cuntèngiat ispàtzios
DE FÀGHERE: annànghere controllos prus astrintos (comente in en.wiki)
	===============================================================================]]
local function check_url( url_str )
	-- si bi sunt ispàtzios non podet èssere unu url curretu
	if nil == url_str:match ("^%S+$") then
		return false;
	end
	-- Protocol-relative or URL scheme
	return url_str:sub(1,2) == "//" or url_str:match( "^[^/]*:" ) ~= nil;
end

--[[ ===============================================================================
Faghet in modu chi un'istringa siat segura pro èssere impreada comente descritzione de un'url
	===============================================================================]]
local function safe_for_url( str )
	if str:match( "%[%[.-%]%]" ) ~= nil then
		table.insert( z.message_tail, { set_error( 'wikilink_in_url', {}, true ) } );
	end

	return str:gsub( '[%[%]\n]', {
		['['] = '&#91;',
		[']'] = '&#93;',
		['\n'] = ' ' } );
end

--[[ ===============================================================================
Formatat unu ligàmene esternu cun verìfica de sos errores
- URL: url de su ligàmene esternu
- label: eticheta de su ligàmene esternu (si no insertada benit impreadu
		 s'URL comente eticheta e benit sinnaladu s'errore)
- source: paràmetru in ue s'agatat s'url
	===============================================================================]]
local function external_link( URL, label, source )
	local error_str = "";
	if not is_set( label ) then
		label = URL;
		if is_set( source ) then
			error_str = set_error( 'bare_url_missing_title', { wrap( 'parameter', source ) }, false, " " );
		else
			error( cfg.messages["bare_url_no_origin"] );
		end
	end
	if not check_url( URL ) then
		error_str = set_error( 'bad_url', {}, false, " " ) .. error_str;
	end
	return table.concat({ "[", URL, " ", safe_for_url( label ), "]", error_str });
end

--[[ ===============================================================================
Formatat unu DOI e verìficat pro errores
	===============================================================================]]
local function doi(id, inactive)
	local cat = ""
	local handler = cfg.id_handlers['DOI'];

	local text;
	if is_set(inactive) then
		local inactive_year = inactive:match("%d%d%d%d") or ''
		--text = "[[" .. handler.link .. "|" .. handler.label .. "]]:" .. id;
		if is_set(inactive_year) then
			table.insert( z.error_categories, "Pàginas cun DOI non ativu dae su " .. inactive_year );
		else
			table.insert( z.error_categories, "Pàginas cun DOI non ativu" );		-- when inactive doesn't contain a recognizable year
		end
		inactive = " (" .. cfg.messages['inactive'] .. " " .. inactive .. ")"
	end
	text = link_id({link = handler.link, label = handler.label,
			pattern=handler.pattern,id=id,separator=handler.separator, encode=handler.encode}) .. (inactive or '')
	if nil == id:match("^10%.[^%s–]-/[^%s–]-[^%.,]$") then						-- doi must begin with '10.', must contain a fwd slash, must not contain spaces or endashes, and must not end with period or comma
		cat = set_error( 'bad_doi' );
	end
	return text .. cat
end

--[[ ===============================================================================
Formatat unu ligàmene a Open library e controlla per errori
	===============================================================================]]
local function open_library(id)
	local code = id:sub(-1,-1)
	local handler = cfg.id_handlers['OL'];
	if ( code == "A" ) then
		return link_id({link=handler.link, label=handler.label,
			pattern="[http://openlibrary.org/authors/OL$1 $1]",id=id, separator=handler.separator,
			encode = handler.encode})
	elseif ( code == "M" ) then
		return link_id({link=handler.link, label=handler.label,
			pattern="[http://openlibrary.org/books/OL$1 $1]",id=id, separator=handler.separator,
			encode = handler.encode})
	elseif ( code == "W" ) then
		return link_id({link=handler.link, label=handler.label,
			pattern= "[http://openlibrary.org/works/OL$1 $1]",id=id, separator=handler.separator,
			encode = handler.encode})
	else
		return link_id({link=handler.link, label=handler.label,
			pattern= "[http://openlibrary.org/OL$1 $1]",id=id, separator=handler.separator,
			encode = handler.encode}) ..
			' ' .. set_error( 'bad_ol' );
	end
end

--[[ ===============================================================================
Formatat unu ligàmene a sa libreria Opac impreende s'SBN e verificat pro errores
	===============================================================================]]
local function sbn(id)
	local handler = cfg.id_handlers['SBN']
	local start_match, end_match, cd1, cd2 = string.find(id, '^IT\\ICCU\\(...)\\(%d+)')
	if not(cd1 and cd2) then
		start_match, end_match, cd1, cd2 = string.find(id, '^IT\\ICCU\\(....)\\(%d+)')
	end
	if cd1 and cd2 then
		return link_id({link=handler.link, label=handler.label,
			pattern='[http://opac.sbn.it/bid/$1 $1]', id = id, code_id=cd1 .. cd2,
			encode =handler.encode})
	else
		return link_id({link=handler.link, label=handler.label,
			pattern='[http://opac.sbn.it/bid/$1 $1]', id = id,
			encode =handler.encode}) .. ' ' .. set_error('bad_sbn')
	end
end

--[[ ===============================================================================
	Nice Opaque Identifiern impreadu pro sos formados Ark pro ingendrare una crae
	adatadu dae da fr:Module:Biblio/Références
	===============================================================================]]
local function ark_id( base )
	base = tostring( base )
	if base then
		local xdigits = '0123456789bcdfghjkmnpqrstvwxz'
		local sum = 0 
		local position
		for i = 1, base:len() do
			position = xdigits:find( base:sub( i, i ), 1, true ) or 1
			sum = sum + i * ( position - 1 )
		end
		local index = sum % 29 + 1
		return xdigits:sub( index, index )
	end
end

--[[ ===============================================================================
	Formatat unu ligàmene a sa Bibliothèque Nationale de France e verìficat pro errores
	adatadu dae fr:Module:Biblio/Références
	===============================================================================]]
local function bnf(id)
	local handler = cfg.id_handlers['BNF']
	if id then
		local txt = id
		local error_code = ''
		local bnf_id = id:upper():match( 'BNF(%d+%w)' ) or id:lower():match( 'cb(%d+%w)' ) or id:match( '^%d+%w' )
		
		if bnf_id then
			-- bnf contient une suite de chiffres qui peut être un ark valide
			local base = bnf_id:sub( 1, 8 )
			if bnf_id:len() == 8 then 
				-- il manque la clé, on l'ajoute
				id = base .. ark_id( 'cb' .. base )
				txt = base
			elseif bnf_id:len() > 8 and bnf_id:sub( 9, 9 ) == ark_id( 'cb' .. base ) then
				-- ark valide
				id = bnf_id:sub( 1, 9 )
				txt = base
			else
				-- ark qui semble non valide
				id = bnf_id
				txt = bnf_id
				error_code = set_error('bad_bnf')
			end
		else
			-- le paramètre ne semble pas un ark valide
			error_code = set_error('bad_bnf')
		end
		
		-- dans tous les cas on renvoie l'adresse, on catégorise juste pour vérifier ce qui ne va pas
		return link_id( {
			link=handler.link,
			label=handler.label,
			pattern=handler.pattern,
			id=txt,
			code_id=bnf_id,
			separator=handler.separator
		} ) .. ' ' .. error_code
	end
end

--[[ ===============================================================================
Bogat testu e tratigheddos irrilevantes dae unu nùmeru isbn
	===============================================================================]]
local function clean_isbn( isbn_str )
	return isbn_str:gsub( "[^-0-9X]", "" );
end

--[[ ===============================================================================
Determinat si un'istringa ISBN est vàlida
	===============================================================================]]
local function check_isbn( isbn_str )
	isbn_str = clean_isbn( isbn_str ):gsub( "-", "" );

	local len = isbn_str:len();

	if len ~= 10 and len ~= 13 then
		return false;
	end
	local temp = 0;
	if len == 10 then
		if isbn_str:match( "^%d*X?$" ) == nil then return false; end
		isbn_str = { isbn_str:byte(1, len) };
		for i, v in ipairs( isbn_str ) do
			if v == string.byte( "X" ) then
				temp = temp + 10*( 11 - i );
			else
				temp = temp + tonumber( string.char(v) )*(11-i);
			end
		end
		return temp % 11 == 0;
	else
		if isbn_str:match( "^%d*$" ) == nil then return false; end
		isbn_str = { isbn_str:byte(1, len) };
		for i, v in ipairs( isbn_str ) do
			temp = temp + (3 - 2*(i % 2)) * tonumber( string.char(v) );
		end
		return temp % 10 == 0;
	end
end

--[[ ===============================================================================
Torrat s'ùnica eticheta visìbile de unu wikilink
	===============================================================================]]
local function remove_wikilink( str )
	-- Sia [[A|B]] che [[B]] ritornano B
	return (str:gsub( "%[%[([^%[%]]*)%]%]", function(l)
		return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1");
	end));
end

--[[ ===============================================================================
Reconnoschet sas datas in su formadu ISO yyyy-mm-dd e sas riformaat in dmy. S'assigurat
chi 01 e 1 siant torrados comente a 1º in casu chi indichent sa die.
	===============================================================================]]
local function get_date(str)
	if is_set(str) then
		local try_year, try_month, try_day = string.match(str, '^(%d%d%d%d)-(%d%d)-(%d%d)$')
		if try_day then
			local Month = cfg.months[tonumber(try_month)]
			if Month then
				try_day = try_day == '01' and '1º' or tonumber(try_day)
				return string.format("%s %s %s", try_day, Month, try_year)
			end
		end
		try_day, try_month, try_year = string.match(str, '^(%d%d?) (%a+) (%d%d%d%d)$')
		if try_day then
			try_day = (try_day == '1' or try_day == '01') and '1º' or tonumber(try_day)
			return string.format("%s %s %s", try_day, try_month, try_year)
		end
	end
	return str
end

--[[ ===============================================================================
Aunit year, day e month torrende sa data comente a un' ùnica istringa.
month est compidadu solu si year est definidu, e day est compidadu solu si month est definidu.
Si month est unu nùmeru chircat de lu cunvertire in su nùmene chi li currispondet (1->ghennàrgiu, 2->freàrgiu...),
si nono non lu mudat
	===============================================================================]]
local function get_date_yyyy_mm_dd(year, month, day)
	local date = year
	if is_set(date) then
		if is_set(month) then
			local month = cfg.months[tonumber(month)] or month
			date = month .. " " .. year
			if is_set(day) then
				if day == "01" or day=="1" then day="1º" end
				date = day .. " " .. date
			end
		end
		return date
	end
	return ""
end

--[[ ===============================================================================
Suponet chi str siat una data bene formada (una de sas variantes "gg mm aaaa",
"gg/mm/aaaa" o "gg-mm-aaaa") e rendet s'artìculu de antepònnere pro la tzitare
comente a data de atzessu/archìviu
	===============================================================================]]
local function article_date(str)
	local start = mw.ustring.sub(str, 1, 2)
	if in_array( start, {'08', '8 ', '8-', '8/', '11'} ) then
		return " s'"
	elseif mw.ustring.find(str, '^pre ') then -- per i valori "pre x/x/x" inseriti da ArchiveBot
		return ' in data '
	end
	return str ~= '' and " su " or ''
end

--[[ ===============================================================================
Verificat chi s'istringa colada siat in unu formadu ammìtidu, in casu contràriu
 torrat su còdighe de errore
	===============================================================================]]
local function check_time(str)
	local h,m,s = string.match(str, '^(%d+):(%d+):(%d+)$')
	if not(h) then h,m,s = string.match(str, '^(%d+) o (%d+) min (%d+) s$') end
	if not(m) then m,s = string.match(str, '^(%d+) min (%d+) s$') end
	if not(m) then m,s = string.match(str, '^(%d+):(%d+)$') end
	if not(m) then m = string.match(str, '^(%d+) min$') end
	if not(m) then return 'time_not_valid' end
	if tonumber(m) >= 60 then return 'minutes_wrong' end
	if s and tonumber(s) >= 60 then return 'seconds_wrong' end
	if h and not(tonumber(s)) then return 'hour_wrong' end
	return nil
end

--[[ ===============================================================================
Formatat una lista de persones (autores o editores)
	===============================================================================]]
local function list_people(control, people)
	local sep = control.sep;
	local lastsep = control.lastsep
	local text = {}
	local etal = control.etal
	local coauthors = control.coauthors
	local person_list = {}

	for i,person in ipairs(people) do
		local last = person.last
		if is_set(last) then
			local fullname = ""
			local first = person.first
			if is_set(first) then
				if invertorder then first, last = last, first end
				fullname = table.concat({first, person.last}, ' ')
			else
				fullname = person.last
			end
			if is_set(person.link) then fullname = table.concat({"[[", person.link, "|", fullname, "]]"}) end
			table.insert( person_list, fullname )
		end
		if etal then
			break
		end
	end
	local count = #person_list
	local result = ""
	 if count > 0 then
		if coauthors then
			result = table.concat(person_list, sep)
		elseif etal then
			result = person_list[1] .. cfg.messages['et al']
		else
			result = mw.text.listToText(person_list, sep, lastsep)
		end
	end
	return result, count
end

--[[ ===============================================================================
Generat un id pro un'àncora CITEREF
	===============================================================================]]
local function anchor_id( options )
	return "CITEREF" .. table.concat( options );
end

--[[ ===============================================================================
Pigat una lista de nùmenes (autores o editores) dae sa lista de argumentos
	===============================================================================]]
local function extract_names(args, list_name)
	local names = {};
	local i = 1;
	local last;

	while true do
		last = select_one( args, cfg.aliases[list_name .. '-Last'], 'redundant_parameters', i );
		if not is_set(last) then
			local first = select_one( args, cfg.aliases[list_name .. '-First'], 'redundant_parameters', i )
			if not is_set(first) then
				break;
			else -- in casu chi siat definidu "nùmene" ma non "sambenadu"
				names[i] = {
					last = first,
					first = '',
					link = select_one( args, cfg.aliases[list_name .. '-Link'], 'redundant_parameters', i ),
				}
			end
		else
			names[i] = {
				last = last,
				first = select_one( args, cfg.aliases[list_name .. '-First'], 'redundant_parameters', i ),
				link = select_one( args, cfg.aliases[list_name .. '-Link'], 'redundant_parameters', i ),
			};
		end
		i = i + 1;
	end
	return names;
end

--[[ ===============================================================================
Pigat dae sos argumentos sos còdighes bibliogràficos reconnotos impreende sa tabella
cfg.id_handlers
	===============================================================================]]
local function extract_ids( args )
	local id_list = {};
	for k, v in pairs( cfg.id_handlers ) do
		v = select_one( args, v.parameters, 'redundant_parameters' );
		if is_set(v) then
			if k == 'ISBN' then v = string.gsub(v, '^ISBN%s*', '') end -- hack pro iscantzellare s'ISBN repìtidu
			id_list[k] = v;
		end
	end
	return id_list;
end

--[[ ===============================================================================
Formatat sos id bibliogràficos presentes in sa tabella id_list
	===============================================================================]]
local function build_id_list( id_list, options )
	local new_list, handler = {};

	local function fallback(k)
		return { __index = function(t,i) return cfg.id_handlers[k][i] end }
	end;

	local function comp( a, b )
		return a[1] < b[1];
	end

	for k, v in pairs( id_list ) do
		-- fallback to read-only cfg
		local handler = setmetatable( { ['id'] = v }, fallback(k) );

		if k == 'DOI' then
			table.insert( new_list, {handler.label, doi( v, options.DoiBroken ) } );
		elseif k == 'OL' then
			table.insert( new_list, {handler.label, open_library( v ) } );
		elseif k == 'SBN' then
			table.insert (new_list, {handler.label, sbn(v) } );
		elseif k == 'BNF' then
			table.insert (new_list, {handler.label, bnf(v) } );
		elseif k == 'ISBN' then
			local ISBN
			if v == 'no esistit' or v == 'no' then --sa forma longa pro intertzetare su balore torradu dae su template NoIsbn
				ISBN = 's\'ISBN no esistit'
			else
				ISBN = link_id( handler );
				if not check_isbn( v ) and not is_set(options.IgnoreISBN) then
					ISBN = ISBN .. set_error( 'bad_isbn', {}, false, "<sup>", "</sup>" );
				end
			end
			table.insert( new_list, {handler.label, ISBN } );
		else
			table.insert( new_list, {handler.label, link_id( handler ) } );
		end
	end
	table.sort( new_list, comp );
	for k, v in ipairs( new_list ) do
		new_list[k] = v[2];
	end

	return new_list;
end

--[[ ===============================================================================
Gènerat s'istringa pro su formadu, si format no est definida chircat de lu agatare dae s'url
	===============================================================================]]
local function get_format(format, url)
	if format:lower() == 'html' then
		return ''
	elseif not is_set(format) then
		format = mw.ustring.match(url, "^.*%.(.+)$" ) or ''
		if not cfg.external_link_type[format:lower()] then
			format = mw.ustring.match(format, "^(.+)#.+$") or ''
			if not cfg.external_link_type[format:lower()] then
				return ''
			end
		end
	end

	-- Si su formadu esternu est intre cussos prevìdidos ìmitat s'istile de sos templates {{PDF}} o {{doc}}
	local f = cfg.external_link_type[format:lower()]
	if f then
		return mw.ustring.format(' (<span style="font-weight: bolder; font-size:80%%"><abbr title="%s">%s</abbr></span>)', f.text, f.label)
	else
		table.insert( z.message_tail, { set_error('unknown_format', format, true) } );
		return mw.ustring.format(' (%s)', format)
	end
end

--[[ ===============================================================================
Generat sa tzitatzione
	===============================================================================]]
local function citation0( config, args )
	local A = argument_wrapper( args );
	local i

	local Stylename = A['Style']
	local Style = cfg.style
	local PPPrefix = (is_set( A['NoPP'] ) and "") or Style.ppprefix
	local PPrefix = (is_set( A['NoPP'] ) and "") or Style.pprefix
	-- Pick out the relevant fields from the arguments. Different citation templates
	-- define different field names for the same underlying things.
	-- local Authors = A['Authors'];
	local a = extract_names( args, 'AuthorList' );

	local Coauthors = A['Coauthors'];
	local Others = A['Others'];
	local Editors = A['Editors'];
	local No_editor = A['No_editor'];
	local e = extract_names( args, 'EditorList' );

	------------------------------------------------- Get date data
	local PublicationDate = A['PublicationDate'];
	local LayDate = A['LayDate'];
	------------------------------------------------- Get title data
	local Title = A['Title'];
	local Conference = A['Conference'];
	local Organization = A['Organization']
	local TransTitle = A['TransTitle'];
	local OriginalTitle = A['OriginalTitle']
	-- local TitleNote = A['TitleNote'];
	local TitleLink = A['TitleLink'];
	local Chapter = A['Chapter'];
	local ChapterLink = A['ChapterLink'];
	local TransChapter = A['TransChapter'];
	local TitleType = A['TitleType'];
	local ArchiveURL = A['ArchiveURL'];
	local URL = A['URL']
	local URLorigin = A:ORIGIN('URL');
	local ChapterURL = A['ChapterURL'];
	local ChapterURLorigin = A:ORIGIN('ChapterURL');
	local ConferenceURL = A['ConferenceURL'];
	local ConferenceURLorigin = A:ORIGIN('ConferenceURL');
	local Abstract = A['Abstract']
	local Periodical = A['Periodical'];
	local Illustrator = A['Illustrator'];
	local Translator = A['Translator'];

	if is_set(OriginalTitle) and not is_set(TransTitle) then
		TransTitle = Title
		Title = OriginalTitle
	end

	local isPublicatzione = (config.CitationClass == 'publicatzione') or
							(config.CitationClass=='testu' and is_set(Periodical))

	------------------------------------------------------------------------------
	-- Formatatzione de Position - cuntenit sa pàgina/positzione o su puntu de su vìdeu
	-- a su cale faghet riferimentu sa fonte
	------------------------------------------------------------------------------
	local Position = A['Position'];
	local PositionOrigin=A:ORIGIN('Position')
	if is_set(Position) then
		if PositionOrigin == "p" then
			Position = PPrefix .. Position
		elseif PositionOrigin == "pp" then
			Position = PPPrefix .. Position
		elseif PositionOrigin ~= "positzione" then
			table.insert( z.error_categories, 'Artìculos cun mòdulu tzitatzione e paràmetru ' .. PositionOrigin )
			if PositionOrigin == "pàginas" then
				if config.CitationClass == "libru" then
					if tonumber(Position) then
						Position = PPrefix .. Position
					elseif string.find(Position, '^%d') then
						Position = PPPrefix .. Position
					end
				elseif config.CitationClass=="cunferèntzia" or config.CitationClass== "publicatzione" then
					Position = (tonumber(Position) and PPrefix or PPPrefix) .. Position
				end
			elseif PositionOrigin == "pàgina" then
				Position = PPrefix .. Position
			else
				Position = PPPrefix .. Position
			end
		end
	end
	local Hour = A['Hour']
	local Minutes = A['Minutes']
	local Seconds = A['Seconds']
	local Time = A['Time']
	if in_array(config.CitationClass, { "vìdeu", "tv", "àudio" } ) then
		local ComposeTime = {}
		local TimeError = {}
		if is_set(Hour) then
			if not is_set(Minutes) then TimeError[#TimeError+1] = set_error('need_minutes' , {'ora'}) end
			if not tonumber(Hour) then TimeError[#TimeError+1] = set_error('timepar_must_be_integer', {'ora'}) end
			ComposeTime[#ComposeTime+1] = Hour .. '&nbsp;o'
		end
		if is_set(Minutes) then
			local check_error = tonumber(Minutes)
			if not check_error then
				TimeError[#TimeError+1] = set_error('timepar_must_be_integer', {'minutu'})
			elseif check_error > 60 then
				TimeError[#TimeError+1] = set_error('minutes_wrong')
			end 
			ComposeTime[#ComposeTime+1] = Minutes .. '&nbsp;min'
		end
		if is_set(Seconds) then
			if not is_set(Minutes) then TimeError[#TimeError+1] = set_error('need_minutes', {'segundu'}) end
			local check_error = tonumber(Seconds)
			if not check_error then
				TimeError[#TimeError+1] = set_error('timepar_must_be_integer', {'ora'})
			elseif check_error > 60 then
				TimeError[#TimeError+1] = set_error('seconds_wrong')
			end
			ComposeTime[#ComposeTime+1] = Seconds .. '&nbsp;s'
		end
		if #ComposeTime > 1 then
			if is_set(Position) then TimeError[#TimeError+1] = set_error('time_parameter_conflict') end
			Position = 'a ' .. table.concat(ComposeTime, '&nbsp;')
		end
		if is_set(Time) then
			if is_set(Position) then TimeError[#TimeError+1] = set_error('time_parameter_conflict') end
			local check_error = check_time(Time)
			if check_error then TimeError[#TimeError+1] = set_error(check_error) end
			Position = 'a ' .. Time
		end
		if #TimeError > 0 then Position = Position .. " " .. table.concat(TimeError, ", ") end
	else
		if is_set(Hour) or is_set(Minutes) or is_set(Seconds) or is_set(Time) then
			table.insert( z.message_tail, { set_error( 'not_video_citation', {}, true ) } );
		end
	end
	if is_set(Position) then Position = ' ' .. Position end

	------------------------------------------------------------------------------
	-- Formatatzione de volume/nùmeru/sèrie/episòdiu
	------------------------------------------------------------------------------
	local Series = A['Series'];
	local Volume = A['Volume'];
	local Issue = A['Issue'];
	if config.CitationClass == "tv" then
		if is_set(Issue) then
			if is_set(Volume) then
				Issue = substitute(cfg.messages['season_episode'], {Volume, Issue} )
				Volume = ''
			else
				Issue = substitute(cfg.messages['episode'], {Issue})
			end
		end
	else
		if is_set(Volume) then
			if tonumber(Volume) or A:ORIGIN('Volume') == "vol" then
				Volume = "vol.&nbsp;" .. Volume
			end
		end
		if is_set(Issue) then
			if tonumber(Issue) then
				Issue = "nº&nbsp;" .. Issue
			end
		end
	end

	local Edition = A['Edition'];
	local Place = A['Place']
	local PublisherName = A['PublisherName'];
	local SubscriptionRequired = A['SubscriptionRequired'];
	local Via = A['Via'];
	-- local Agency = A['Agency'];
	local DeadURL = A['DeadURL']
	local Language = A['Language'];
	local Format = A['Format'];
	local Ref = A['Ref'];

	local DoiBroken = A['DoiBroken'];
	local ID = A['ID'];
	local IgnoreISBN = A['IgnoreISBN'];
	local Quote = A['Quote'];
	local sepc = Style.sep
	local sepcspace = sepc .. " "
	local PostScript = first_set(A['PostScript'], Style['postscript'])
	local no_tracking_cats = A['NoTracking'];
	local use_lowercase = ( sepc ~= '.' );
	local this_page = mw.title.getCurrentTitle(); --Also used for COinS

	local ID_list = extract_ids( args );
	if ( isPublicatzione ) then
		if not is_set(URL) and is_set(ID_list['PMC']) then
			local Embargo = A['Embargo'];
			if is_set(Embargo) then
				local lang = mw.getContentLanguage();
				local good1, result1, good2, result2;
				good1, result1 = pcall( lang.formatDate, lang, 'U', Embargo );
				good2, result2 = pcall( lang.formatDate, lang, 'U' );

				if good1 and good2 and tonumber( result1 ) < tonumber( result2 ) then
					URL = "http://www.ncbi.nlm.nih.gov/pmc/articles/PMC" .. ID_list['PMC'];
					URLorigin = cfg.id_handlers['PMC'].parameters[1];
				end
			else
				URL = "http://www.ncbi.nlm.nih.gov/pmc/articles/PMC" .. ID_list['PMC'];
				URLorigin = cfg.id_handlers['PMC'].parameters[1];
			end
		end
	end
	ID_list = build_id_list( ID_list, {DoiBroken = DoiBroken, IgnoreISBN = IgnoreISBN} );

	local Station = A['Station'];
	if is_set(Station) then
		local wkStation = A['StationLink']
		if is_set(wkStation) then
			Station = '[[' .. wkStation .. '|' .. Station .. ']]'
		end
	end
	if config.CitationClass == "tv" then
		Chapter = Title;
		ChapterLink = TitleLink;
		TransChapter = TransTitle;
		Title = Series;
		TitleLink = A['SeriesLink'];
		TransTitle = '';
		Series = '';
	end

	------------------------------------------------------------------------------
	-- Si s'url est in realidade un'url archiviadu, su tratat in sa manera adata
	-- (pro como riconnoschet petzi s'Internet Archive)
	------------------------------------------------------------------------------
	local formatoIA = '^https?://web%.archive%.org/w?e?b?/?%d+/'
	if is_set(URL) and is_set(Title) and URL:match(formatoIA) and not is_set(ArchiveURL) then
		ArchiveURL = URL
		URL = URL:gsub(formatoIA,'')
		if not URL:match('://') then
			URL = 'http://' .. URL
		end
	end

	------------------------------------------------------------------------------
	-- Si s'òpera/ su situ no est dislindada/u, impreat su domìniu de s'URL
	------------------------------------------------------------------------------
	local auto_Periodical = false
	if config.CitationClass == "web" and not is_set(Periodical) and not is_set(PublisherName) and is_set(URL) then
		Periodical = mw.ustring.match(URL, "//([^/#%?]*)") or ''
		-- tolgo anche eventuale www.
		if string.find(Periodical, "^[Ww][Ww][Ww]%.") then
			Periodical = mw.ustring.sub(Periodical, 5)
		end
		-- non repitet si su domìniu est istadu impreadu comente tìtulu o editore
		if Periodical and mw.ustring.lower(Title or '') == mw.ustring.lower(Periodical) then
			Periodical = nil
		end
		auto_Periodical = true
	end

	------------------------------------------------------------------------------
	-- Si aparit unu de sos paràmetros ligados a una publicatzione periòdica (òpera, rivista, etz...)
	-- e no est definidu capìtulu, ma petzi tìtulu iscòstio tìtulu a capìtulu
	------------------------------------------------------------------------------
	if is_set(Periodical) and not is_set(Chapter) and is_set(Title) then
		Chapter = Title;
		ChapterLink = TitleLink;
		TransChapter = TransTitle;
		Title = '';
		TitleLink = '';
		TransTitle = '';
	end

	------------------------------------------------------------------------------
	-- Recùpero e formato sa lista de autores
	------------------------------------------------------------------------------
	local AuthorSeparator = Style.peoplesep
	local control = {
		sep = AuthorSeparator,
		maximum = Style.maximum_authors,
		lastsep = Style.lastsepauthor,
		invertorder = Style.invertorder,
		etal = false,
		coauthors = false,
	};
	local Etal = A['Etal']
	-- If the coauthor field is also used, prevent adding ''et al.''
	if is_set(Coauthors) then
		table.insert( z.error_categories, 'Artìculos cun mòdulu tzitatzione e paràmetru ' .. A:ORIGIN('Coauthors') )
		control.coauthors = true
	elseif is_set(Etal) then
		control.etal = true
	end
	local Authors = list_people(control, a)
	if not is_set(Authors) and is_set(Coauthors) then -- si non sunt istados compilados campos autore, ma petzi coautore
		Authors = Coauthors
		Coauthors = ""
	elseif is_set(Coauthors) then
		Authors = table.concat({Authors, AuthorSeparator, Coauthors})
	end

	------------------------------------------------------------------------------
	-- Recùpero e formato sa lista de curadores
	------------------------------------------------------------------------------
	local EditorCount, msg_editors
	local CuratoriEtal = A['Etalcuratori']
	control.coauthors = false
	if is_set(CuratoriEtal) then
		control.etal = true
	else
		control.etal = false
	end
	if is_set(Editors) then
		msg_editors = 'editors'
	else
		Editors, EditorCount = list_people(control, e)
		if is_set(Editors) then
			if EditorCount <= 1 then msg_editors = 'editor' else msg_editors = 'editors' end
		end
	end
	------------------------------------------------------------------------------
	-- Si non sunt definidos autores mudo cun curadores
	------------------------------------------------------------------------------
	if not is_set(Authors) and is_set(Editors) then
		Authors = Editors
		Editors = ""
	end

	------------------------------------------------------------------------------
	-- Si est una cunferèntzia annango su campu Organizatzione
	------------------------------------------------------------------------------
	if config.CitationClass == 'cunferèntzia' then
		if is_set (Authors) and is_set(Organization) then
			Authors = table.concat({Authors, ', ', Organization})
		elseif is_set(Organization) then
			Authors = Organization
		end
		Organization = ""
	end

	------------------------------------------------------------------------------
	-- Formato sa data
	------------------------------------------------------------------------------
	local Date = get_date(A['Date'])
	local Year = A['Year']
	if not is_set(Date) then Date=get_date_yyyy_mm_dd(Year, A['Month'], A['Day']) end
	local OrigDate = get_date(A['OrigDate'])
	if not is_set(OrigDate) then OrigDate=get_date_yyyy_mm_dd(A['OrigYear'], A['OrigMonth'], A['OrigDay']) end
	local AccessDate = get_date(A['AccessDate'])
	if not is_set(AccessDate) then AccessDate=get_date_yyyy_mm_dd(A['AccessYear'], A['AccessMonth'], A['AccessDay']) end
	local ArchiveDate = get_date(A['ArchiveDate']);
	if is_set(OrigDate) and not is_set(Date) then
		Date = OrigDate
		OrigDate = ""
	end
	OrigDate = is_set(OrigDate) and (" " .. wrap( 'origdate', OrigDate)) or "";

	if in_array(PublicationDate, {Date, Year}) then PublicationDate = '' end
	if not is_set(Date) and is_set(PublicationDate) then
		Date = PublicationDate;
		PublicationDate = '';
	end

	-- Captures the value for Date prior to adding parens or other textual transformations
	local DateIn = Date;
	if not is_set(URL) and
		not is_set(ChapterURL) and
		not is_set(ArchiveURL) and
		not is_set(ConferenceURL) then
		-- Test if cite web is called without giving a URL
		if ( config.CitationClass == "web" ) then
			table.insert( z.message_tail, { set_error( 'cite_web_url', {}, true ) } );
		end
		-- Test if accessdate is given without giving a URL
		if is_set(AccessDate) then
			table.insert( z.message_tail, { set_error( 'accessdate_missing_url', {}, true ) } );
			AccessDate = '';
		end
		-- Test if format is given without giving a URL
		if is_set(Format) then
			Format = Format .. set_error( 'format_missing_url' );
		end
	end

	-- Test if citation has no title
	if not is_set(Chapter) and
		not is_set(Title) and
		not is_set(Periodical) and
		not is_set(Conference) and
		not is_set(TransTitle) and
		not is_set(TransChapter) then
		table.insert( z.message_tail, { set_error( 'citation_missing_title', {}, true ) } );
	end

	-- generat s'istringa pro su formadu
	Format = get_format(Format, (is_set(URL) and URL) or ChapterURL )

	-- In manera predefinida s'URL no est decraradu mortu
	if is_set(DeadURL) then
		 DeadURL = (DeadURL:lower() ~= 'no') and (DeadURL:lower() ~= 'nono') 
	else
		-- Si s'archìviu no est dislindadu
		DeadURL = is_set(ArchiveURL)
	end

	local OriginalURL = URL
	if is_set( ArchiveURL ) then
		if DeadURL then
			URL = ArchiveURL
			URLorigin = A:ORIGIN('ArchiveURL')
		end
	end

	---------------------------------------------------------------
	-- si publicatzione verificat pro su paràmetru abstract
	--------------------------------------------------------------
	if is_set(Abstract) then
		if isPublicatzione then
			if is_set(ChapterURL) then
				TitleType = external_link( ChapterURL, 'abstract' )
				ChapterURL = ""
				if not is_set(URL) then Format = "" end
			elseif is_set(URL) then
				TitleType = external_link( URL, 'abstract' )
				URL = ""
			else
				Abstract = ''
			end
		else
			Abstract = ""
		end
	else
		Abstract = ""
	end
	TitleType = is_set(TitleType) and ("(" .. TitleType .. ")") or "";

	---------------------------------------------------------------
	-- Format chapter / article title
	---------------------------------------------------------------
	local TransError = ""
	if is_set(TransChapter) then
		if not is_set(Chapter) then
			TransError = " " .. set_error( 'trans_missing_chapter' )
			Chapter = TransChapter
			TransChapter = ""
		else
			TransChapter = wrap( 'trans-italic-title', TransChapter )
		end
	end
	Chapter = is_latin( Chapter ) and wrap( 'italic-title', Chapter ) or Chapter;
	if is_set(TransChapter) then Chapter = Chapter .. " " .. TransChapter end
	if is_set(Chapter) then
		if is_set(ChapterLink) then
			Chapter = table.concat({"[[", ChapterLink, "|", Chapter, "]]"})
		elseif is_set(ChapterURL) then
				Chapter = external_link( ChapterURL, Chapter ) .. TransError;
				if not is_set(URL) then --se est impostadu s'URL sarvo Format pro l'insertare a pustis de custu
					Chapter = Chapter .. Format;
					Format = "";
				end
		elseif is_set(URL) then
			Chapter = external_link( URL, Chapter ) .. TransError .. Format;
			URL = "";
			Format = "";
		else
			Chapter = Chapter .. TransError;
		end
	elseif is_set(ChapterURL) then
		Chapter = external_link( ChapterURL, nil, ChapterURLorigin ) .. TransError
	end

	---------------------------------------------------------------
	-- Format main title
	---------------------------------------------------------------
	TransError = "";
	if is_set(TransTitle) then
		if not is_set(Title) then
			TransError = " " .. set_error( 'trans_missing_title' )
			Title = TransTitle
			TransTitle = ""
		else
			TransTitle = wrap( 'trans-italic-title', TransTitle )
		end
	end
	Title = is_latin( Title ) and wrap('italic-title', Title ) or Title;
	if is_set(TransTitle) then Title = Title .. " " .. TransTitle end
	if is_set(Title) then
		if is_set(TitleLink) then
			Title = "[[" .. TitleLink .. "|" .. Title .. "]]"
		elseif is_set(URL) then
			Title = external_link( URL, Title ) .. TransError .. Format
			URL = "";
			Format = "";
		else
			Title = Title .. TransError;
		end
	end
	---------------------------------------------------------------
	-- Format Conference
	---------------------------------------------------------------
	if is_set(Conference) then
		Conference = wrap('italic-title', Conference )
		if is_set(ConferenceURL) then
			Conference = external_link( ConferenceURL, Conference );
		end
	elseif is_set(ConferenceURL) then
		Conference = external_link( ConferenceURL, nil, ConferenceURLorigin );
	end

	---------------------------------------------------------------
	-- Cumponet s'istringa de su limbàgiu
	---------------------------------------------------------------
	local Language_code = ""
	if is_set(Language) then
		if Language:sub(1,1) == "(" then
			Language_code = Language
		else
			local frame_limbas = {return_error='true'}
			for limba in mw.text.gsplit(Language, ',', true) do
				limba = mw.text.trim(limba)
				if limba ~= '' then
					frame_limbas[#frame_limbas+1] = limba
				end
			end
			if #frame_limbas > 1 or (#frame_limbas==1 and frame_limbas[1]:lower()~="sc") then
				local lg_error
				Language_code, lg_error = require("Module:Limbàgios").limbas(frame_limbas)
				if lg_error and #lg_error > 0 then
					local error_string = mw.text.listToText(lg_error, ", ", " e " )
					table.insert( z.message_tail, { set_error('unknown_language', {error_string}, true) } );
				end
			end
		end
	end

	if is_set(Edition) then
		if A:ORIGIN('Edition') == "ed" or tonumber(Edition) then
			Edition = Edition .. "ª&nbsp;ed."
		end
	end

	-- si s'URL no est istadu consumidu dae unu capìtulu/tìtulu emitit un'errore
	if is_set(URL) then
		URL = " " .. external_link( URL, nil, URLorigin );
	end

	--Annango sas virguleddas a sa tzitatzione-
	if is_set(Quote) then
		Quote = wrap( 'quoted-text', Quote );
	end
	---------------------------------------------------------------
	-- Paràmetru via e subscription
	---------------------------------------------------------------
	if is_set(Via) then
		if is_set(SubscriptionRequired) then
			Via = wrap( 'viasubscription', Via );
		else
			Via = wrap('via', Via);
		end
	elseif is_set(SubscriptionRequired) then
		Via = wrap('subscription')
	end

	---------------------------------------------------------------
	-- Formatatzione datos de atzessu/url de archìviu
	---------------------------------------------------------------
	if is_set(AccessDate) then
		AccessDate = substitute( cfg.messages['retrieved'], {AccessDate, article_date(AccessDate)} )
	end
	local Archived
	if is_set(ArchiveURL) then
		local decodeArchiveDate = require('Module:Webarchive').decodeArchiveDate
		local ArchiveURLDate = decodeArchiveDate(ArchiveURL)
		local ArchiveError, ArchiveOutput = ''
		if not is_set(ArchiveDate) then
			ArchiveDate = ArchiveURLDate or ''
			if not ArchiveURLDate then
				ArchiveError = set_error('archive_missing_date', {}, false, ' ')
			end
		elseif ArchiveURLDate and ArchiveURLDate ~= ArchiveDate then
			ArchiveError = set_error('date_mismatch', {ArchiveURLDate}, false, ' ')
		end
		ArchiveOutput = ArchiveDate .. ArchiveError
		local ArchiveURL2 = A['ArchiveURL2']
		local ArchiveDate2 = get_date(A['ArchiveDate2'])
		if is_set(ArchiveURL2) then
			local ArchiveURLDate2 = decodeArchiveDate(ArchiveURL2)
			local ArchiveError2, ArchiveOutput2 = ''
			if not is_set(ArchiveDate2) then
				ArchiveDate2 = ArchiveURLDate2 or ''
				if not ArchiveURLDate2 then
					ArchiveError2 = set_error('archive_missing_date2', {}, false, ' ')
				end
			elseif ArchiveURLDate2 and ArchiveURLDate2 ~= ArchiveDate2 then
				ArchiveError2 = set_error('date2_mismatch', {ArchiveURLDate2}, false, ' ')
			end
			ArchiveOutput2 = ArchiveDate2 .. ArchiveError2
			ArchiveURL2 = substitute(cfg.messages['archived-second-copy'],
				{ external_link( ArchiveURL2, cfg.messages['archived2']), ArchiveOutput2, article_date(ArchiveDate2) } );
		end
		if not DeadURL then
			Archived = substitute( cfg.messages['archived-not-dead'],
				{ external_link( ArchiveURL, cfg.messages['archived'] ), ArchiveOutput, article_date(ArchiveDate),  ArchiveURL2} );
			if not is_set(OriginalURL) then
				Archived = Archived .. set_error('archive_missing_url', {}, false, ' ');
			end
		elseif is_set(OriginalURL) then
			Archived = substitute( cfg.messages['archived-dead'],
				{ OriginalURL, ArchiveOutput, article_date(ArchiveDate), ArchiveURL2 } );
		else
			Archived = substitute( cfg.messages['archived-missing'],
				{ set_error('archive_missing_url'), ArchiveOutput, article_date(ArchiveDate), ArchiveURL2 } );
		end
	else
		Archived = ""
	end

	---------------------------------------------------------------
	-- Data originale si b'est (in òrdine di preferèntzia a pustis de
	-- sa data de publicatzione, e a pustis s'editore e su logu de publicatzione, )
	---------------------------------------------------------------
	if is_set(OrigDate) then
		if is_set(Date) then
			Date = Date .. " " .. OrigDate
		elseif is_set(PublisherName) then
			PublisherName = PublisherName .. " " .. OrigDate
		elseif is_set(Plase) then
			Place = Place .. " " .. OrigDate
		else
			Date = OrigDate
		end
	end

	-- Several of the above rely upon detecting this as nil, so do it last.
	if is_set(Periodical) then Periodical = wrap( 'italic-title', Periodical ) end
	if config.CitationClass=="news" and is_set(Place) then
		if is_set(Periodical) then
			Periodical = table.concat({Periodical, ' (', Place, ')'})
			Place = ""
		elseif is_set(Title) then
			Title = table.concat({Title, ' (', Place, ')'})
			Place = ""
		end
	end

	-- set translator / illustrator
	if is_set(Translator) then Translator = wrap('translator', Translator) end
	if is_set(Illustrator) then Illustrator = wrap('illustrator', Illustrator) end

	---------------------------------------------------------------
	-- Cumbino paris sos cumponentes diferentes de sa tzitatzione
	---------------------------------------------------------------

	local fragment_Title
	local PostTitle = A['PostTitle']
	if is_set(Title) then
		if DeadURL and not is_set( ArchiveURL ) then
		   -- Report a dead URL without an archived URL
			Title = Title .. interrupted_url()
		end
		fragment_Title = Fragment.new({Title, Format, TitleType, PostTitle}, ' '):last(",")
	else
		fragment_Title = Fragment.new({ })
		if is_set(Chapter) then
			Chapter = DeadURL and not is_set( ArchiveURL )
				and tostring(Fragment.new({Chapter, interrupted_url(), Format, TitleType, PostTitle}, ' '):last(""))
				or  tostring(Fragment.new({Chapter, Format, TitleType, PostTitle}, ' '):last(""))
		end
	end

	local fragment_citation
	if config.CitationClass == "tv" then
		if is_set(Chapter) then
			fragment_Title:last(":"):append(Fragment.new({Issue, Chapter}, sepc))
			Issue = ""
		end
		fragment_citation=Fragment.new({Authors}, sepc)
		fragment_citation:append(fragment_Title)
	else
		if is_set(Authors) and is_set(Editors) and is_set(Title) and not is_set(Chapter) then
			Editors = 'a incuru de ' .. Editors
			fragment_citation = Fragment.new({Authors}, sepc)
			fragment_citation:appends({fragment_Title, Editors})
		else
			if is_set(msg_editors) and not is_set(No_editor) then
				if is_set(Editors) then
					Editors = wrap(msg_editors, Editors)
				else
					Authors = wrap(msg_editors, Authors)
				end
			end
			fragment_citation = Fragment.new({Authors, Chapter}, sepc)
			if Chapter ~= "" or Editors ~= "" then
				if A:ORIGIN('Periodical') == 'situ' or auto_Periodical then -- ponet in antis "in" imbetzes de "in" pro sos sitos web (inùtile inoghe, cantu abarradu dae s'italianu)
					fragment_citation:last("in")
				else
					fragment_citation:last("in")
				end
			end
			fragment_citation:appends({Editors, fragment_Title})
		end
	end
	fragment_citation:appends( { Conference, Periodical, Translator, Illustrator, Others, Series,
								 Volume, Issue, Edition, Place, PublisherName, Station, Date, Position } )
	local fragment_ID_list = Fragment.new(ID_list, sepc):append(ID):start(",")
	local fragment_URL = Fragment.new(URL):start(",")
	local fragment_AccessDate = Fragment.new(AccessDate):start('.')
	local fragment_Archived = Fragment.new(Archived):start(' ')
	local fragment_Via = Fragment.new(Via):start(".")
	local fragment_Quote = Fragment.new({Quote}):start(".")
	fragment_citation:appends({fragment_ID_list, fragment_URL, fragment_AccessDate, fragment_Archived, fragment_Via, fragment_Quote})
	if PostScript == 'nessuno' then
		fragment_citation:last("nothing")
	else
		fragment_citation:last("..")
	end
	fragment_citation:start(" ")
	local text = Language_code .. tostring(fragment_citation)
	--annango s'icona pro tzita vìdeu
	if config.CitationClass == "vìdeu" then text = cfg.messages['icon_video'] .. ' ' .. text end
	if config.CitationClass == "àudio" then text = cfg.messages['icon_audio'] .. ' ' .. text end

	-- Now enclose the whole thing in a <span/> element
	local options = {};

	if is_set(config.CitationClass) and config.CitationClass ~= "citation" then
		options.class = "citation " .. config.CitationClass;
	else
		options.class = "citation";
	end

--  if string.len(text:gsub("<span[^>/]*>.-</span>", ""):gsub("%b<>","")) <= 2 then
--      z.error_categories = {};
--      text = set_error('empty_citation');
--      z.message_tail = {};
--  end

	if is_set(Ref) then
		text = table.concat({ '<cite id="CITEREF', Ref, --mw.uri.anchorEncode('CITEREF' .. Ref),
								'" class="', mw.text.nowiki(options.class), '" style="font-style:normal">', text, "</cite>"})
	else
		text = table.concat({ '<cite class="', mw.text.nowiki(options.class), '" style="font-style:normal">', text, "</cite>"})
	end

	local empty_span = '<span style="display:none;">&nbsp;</span>';

	if #z.message_tail ~= 0 then
		text = text .. " ";
		for i,v in ipairs( z.message_tail ) do
			if is_set(v[1]) then
				if i== #z.message_tail then
					text = text .. error_comment( v[1], v[2] );
				else
					text = text .. error_comment( v[1] .. "; ", v[2] );
				end
			end
		end
	end

-- Chek to insert category error
	if not is_set(no_tracking_cats) then
		for k, v in pairs( cfg.uncategorized_namespaces ) do
			if this_page.nsText == v then
				no_tracking_cats = "true";
				break;
			end
		end
	end
	no_tracking_cats = no_tracking_cats:lower();
	if in_array(no_tracking_cats, {"", "no", "false", "n"}) then
		for _, v in ipairs( z.error_categories ) do
			text = text .. '[[Categoria:' .. v ..']]';
		end
	end

	return text
end

--[[ ===============================================================================
Funtzione de interfache pro s'ingendratzione de sa tzitatzione, impreada dae sos vàrios template
tzita libru, tzita news, etc...
	===============================================================================]]
function z.citation(frame)
	local pframe = frame:getParent()

	local args = {};
	local suggestions = {};
	local error_text, error_state;

	local config = {};
	for k, v in pairs( frame.args ) do
		config[k] = v;
		args[k] = v;
	end
	if config['ignore_parent'] == 's' then
		pframe.args = {}
	end
	local ignore_unnamed = (config['ignore_unnamed'] == 's') or false
	 -- copy unnamed parameter to named parameter
	local lastunnamed = 0
	if not(ignore_unnamed) and cfg.unnamed_parameter[config.CitationClass] then
		for i, v in ipairs(cfg.unnamed_parameter[config.CitationClass]) do
			if pframe.args[i] then
				local args_value = mw.text.trim(pframe.args[i])
				if args_value ~= "" then
					args[v] = args_value
				end
				lastunnamed = i
			else
				break
			end
		end
	end
	for k, v in pairs( pframe.args ) do
		if v ~= '' then
			if not validate( k ) then
				error_text = "";
				if type( k ) ~= 'string' then
					-- Exclude empty numbered parameters
					if v:match("%S+") ~= nil and tonumber(k) > lastunnamed and lastunnamed > 0 then
						error_text, error_state = set_error( 'text_ignored', {v}, true );
					end
				elseif validate( k:lower() ) then
					error_text, error_state = set_error( 'parameter_ignored_suggest', {k, k:lower()}, true );
				else
					if #suggestions == 0 then
						suggestions = mw.loadData( 'Module:Tzitatzione/Impòsitos' );
					end
					if suggestions[ k:lower() ] ~= nil then
						error_text, error_state = set_error( 'parameter_ignored_suggest', {k, suggestions[ k:lower() ]}, true );
					else
						error_text, error_state = set_error( 'parameter_ignored', {k}, true );
					end
				end
				if error_text ~= '' then
					table.insert( z.message_tail, {error_text, error_state} );
				end
			end
			args[k] = v;
		elseif args[k] ~= nil then
			args[k] = v;
		end
	end

	-- hack per l'uso che fanno cita google books e youtube del parametro id
	if args.id and args.id~='' then
		if in_array(config.CitationClass, {"googlebooks", "video"}) then
			args.id = nil
		end
	end
	return citation0( config, args )
end

-- Funtzione pro ingendrare una tzitatzione web dae un'àteru mòdulu
function z.citaweb(args)
	return citation0( {CitationClass = 'web'}, args )
end

-- Elenco sos formados de documentos gestidos
function z.list_external_links(frame)
	local rows = {'{| class = "wikitable sortable"\n!codighe!!Testu popup'}
	local keys = {}
	for key, _ in pairs(cfg.external_link_type) do
		keys[#keys+1] = key
	end
	table.sort(keys)
	for _,key in ipairs(keys) do
		rows[#rows+1] = mw.ustring.format('|-\n|%s||%s', key, cfg.external_link_type[key].text)
	end
	rows[#rows+1] = "|}"
	return table.concat(rows, '\n')
end

-- pro formados esternos dae àteros templates
function z.format(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {frameOnly = true})
	return get_format(args[1])
end

return z