Modulo:Itemlist/sandbox

Da Wikivoyage.

La documentazione per questo modulo può essere creata in Modulo:Itemlist/sandbox/man

--[[
	Source script:	https://it.wikivoyage.org/wiki/Modulo:Itemlist
	Maintainer:		Andyrom75

	Lazy loads:
	require('Modulo:Avviso').avviso
]]
local Marker = require('Modulo:Marker').MarkerModule

local function _isDefined(s)
	return s ~= '' and s
end

local function _numberedList( t ) 
    local _numbT = {}
    for key, val in pairs(t) do
    	if tonumber(key) then
    		_numbT[#_numbT+1] = val
    	end
    end
    return _numbT
end

function _item(frame, tipo)
	local args = frame.args
	local output = ''
	if _isDefined(args.titolo) then
		output = '<b>' .. args.titolo .. '</b>\n' 
	end
	local MarkerArgs = {
		counter = tipo or 'listing',
		tipo = tipo or 'listing',
		nome = args.nome,
		lat = args.lat,
		long = args.long,
		wikidata = args.wikidata
	}
	output = output .. '*' .. Marker{ args = MarkerArgs }
	if _isDefined(args.alt) then output = output .. " (" .. args.alt .. ")" end
	if _isDefined(args.descrizione) then output = output .. " &mdash; " .. args.descrizione end
	return output .. '\n'
end


local function _itemlist(frame, listtype)
	local args = frame.args
	local output = ''
	--mi assicuro di avere una lista con solo chiavi numeriche senza duplicati
	local numberedArgs = _numberedList(args) 
	for _, val in ipairs(numberedArgs) do
		if _ <= 20 then
			output = output .. (val and ('\n' .. tostring(val)) or '')
		end
	end
	output = '<div id="' .. listtype .. '">' .. output .. '</div>'
	if #numberedArgs > 20 then
		output = require('Modulo:Avviso').avviso{args={tipo = 'stile', testo = 'Il template è pensato per gestire fino a 20 località, le successive non saranno mostrate. Seleziona le più significative eliminando le altre.'}} .. output
		if mw.title.getCurrentTitle().namespace == 0 then
			output = output .. '[[Categoria:' .. listtype .. ' con errori di compilazione]]'
		end
	end
	return output
end

local p = {}

function p.cityitemTemplate(frame)
	return _item(frame:getParent(), 'city')
end

function p.cityitem(frame)
	return _item(frame, 'city')
end

function p.destinationitemTemplate(frame)
	return _item(frame:getParent(), 'vicinity')
end

function p.destinationitem(frame)
	return _item(frame, 'vicinity')
end

function p.citylistTemplate(frame)
	return _itemlist(frame:getParent(), 'citylist')
end

function p.citylist(frame)
	return _itemlist(frame, 'citylist')
end

function p.destinationlistTemplate(frame)
	return _itemlist(frame:getParent(), 'destinationlist')
end

function p.destinationlist(frame)
	return _itemlist(frame, 'destinationlist')
end

return p