Modulo:Marker/sandbox

Da Wikivoyage.

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

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

	Lazy loads:
	require('Modulo:WikidataIB').getSiteLink
]]
local coord2Dec = require('Module:Coordinates').toDec
local type2color = require('Module:TypeToColor').convertImpl

local function _templateStyle( frame, src )
	return frame:extensionTag( 'templatestyles', '', { src = src } )
end

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

local function _makeMarkerSymbol( args, frame )
	local image4map = _isDefined(args.immagine) and ('[[File:' .. args.immagine .. '|300px]]') or ''
	local tagArgs = {
		zoom = tonumber( args.zoom ),
		latitude = tonumber( args.lat ),
		longitude = tonumber( args.long ),
		show = args.show,
		group = args.tipo,
		description = (_isDefined(args.description) or '') .. image4map
	}
	if _isDefined( args.text ) then
		tagArgs.text = args.text
		if not args.useIcon then
			tagArgs.class = 'no-icon'
		end
	end

	local geoJson = {
		type = 'Feature',
		geometry = {
			type = 'Point',
			coordinates = { tagArgs.longitude, tagArgs.latitude }
		},
		properties = {
			title = args.nome,
			description = tagArgs.description,
			['marker-symbol'] = args.symbol,
			['marker-color'] = args.color,
			['marker-size'] = 'medium',
		}
	}

	-- frame:extensionTag is expensive
	return frame:extensionTag( 'maplink', mw.text.jsonEncode( geoJson ), tagArgs )
end

local function _Marker(frame)
	local args = frame.args
	local imageErrMsg = ''
	--shortcut variable
	local nome = args.nome or ''
	local tag = ((args.islisting ~= 'yes') and mw.html.create( 'span' ):addClass( 'vcard' )) or mw.html.create()
	local anchor = ''

	if _isDefined( args.wikidata ) then
		-- Se manca una delle due coordinate, provo a estrarle entrambe da Wikidata
		if not _isDefined(args.lat) or not _isDefined(args.long) then
			local coords = mw.wikibase.getBestStatements( args.wikidata, 'P625')
			if coords[1] then
				args.lat = coords[1].mainsnak.datavalue.value.latitude
				args.long = coords[1].mainsnak.datavalue.value.longitude
			end
		end
		-- Se non è stata fornita l'immagine, provo a estrarla da Wikidata
		if not _isDefined(args.immagine) then
			local immagine = mw.wikibase.getBestStatements( args.wikidata, 'P18')
			args.immagine = immagine[1] and immagine[1].mainsnak.datavalue.value
		end
	end

	-- Se sono presenti le coordinate creo il marker tag
	if _isDefined(args.lat) and _isDefined(args.long) then
		-- argument check
		args.lat = coord2Dec(args.lat or 0, '', 6).dec
		args.long = coord2Dec(args.long or 0, '', 6).dec
		args.tipo = _isDefined(args.tipo) or 'listing'
		args.zoom = _isDefined(args.zoom) or '17'

		-- additional arguments
		args.symbol = '-number-' .. ( args.counter or args.tipo or 'listing' )
		args.color = type2color( args.tipo )
		args.show = args.show or 'mask,around,buy,city,do,drink,eat,go,listing,other,see,sleep,vicinity,view'

		local mapTagOutput = _makeMarkerSymbol( args, frame )

		tag:tag( 'span' )
			:addClass( 'noprint listing-coordinates' )
			:tag( 'span' )
				:addClass( 'geo' )
				:tag( 'abbr' )
					:addClass( 'latitude' )
					:wikitext( args.lat )
				:done()
				:tag( 'abbr' )
					:addClass( 'longitude' )
					:wikitext( args.long )
		:allDone()
		:tag( 'span' )
			:attr( 'title', 'Vedi questo elemento nella mappa' )
			:wikitext( mapTagOutput )
	else
		local listingTypes = {see=true, eat=true, buy=true, drink=true, sleep=true, ['do']=true, go=true, city=true, vicinity=true}
		if mw.title.getCurrentTitle().namespace == 0 and args.tipo and listingTypes[string.lower(args.tipo)] then
				anchor = anchor .. "[[Categoria:" .. string.lower(args.tipo) .. " listing senza coordinate]]"
		end
	end

	-- costruisco l'ancora sul nome ed eventualmente gli associo il sitoweb
	if nome ~= '' then
		anchor = '<span id="' .. mw.uri.anchorEncode( nome ) .. '" class="fn org listing-name">' .. nome .. '</span>'
		-- se includo un sito web il nome deve essere puramente testuale
		if _isDefined(args.sito) then
			anchor = string.match(nome, '%[%[') and ('[[Categoria:Listing con nomi non puramente testuali]]' .. anchor)
				or ('[' .. args.sito .. ' ' .. anchor .. ']')
		end
	end
	if mw.title.getCurrentTitle().namespace == 0 then
		anchor = anchor .. '[[Categoria:Articolo con marker]]'
	end

	-- Se l'immagine fornita non esiste genero un messaggio di errore
	if _isDefined( args.immagine ) then
		local image = 'Media:' .. args.immagine
		if mw.title.new( image ) and mw.title.new( image ).exists == true then
			args.description = '[[' .. image .. '|100x100px|' .. nome .. ']]'
		else
			imageErrMsg = mw.html.create( 'span' )
				:addClass('imageinfo debuginfo')
				:wikitext( 'IMMAGINE DEL LISTING NON ESISTENTE' )
		end
	end

	if (args.islisting ~= 'yes') and _isDefined( args.wikidata )then
		local getSiteLink = require('Modulo:WikidataIB').getSiteLink
		if _isDefined( getSiteLink{args={wiki='itwikivoyage', qid=args.wikidata}} ) then
			anchor = anchor .. (string.match(nome, '%[%[') and '' or '[[Categoria:Marker di articoli Wikivoyage senza wikilink]]')
		else
			local wpLink = _isDefined( getSiteLink{args={wiki='itwiki', qid=args.wikidata}} )
			anchor = anchor .. (wpLink and '<span class="listing-sister-icons">&#32;[[File:Wikipedia-icon.png|15px|class=listing-sister|link=w:' .. wpLink .. '|<span>' .. wpLink .. '</span> su Wikipedia]]</span>' or '')
		end
	end

	return _templateStyle( frame, 'Marker/styles.css' ) .. tostring(tag:node(imageErrMsg):wikitext(' ' .. anchor))
end

--Interfacce del modulo
local p = {}

function p.MarkerTemplate(frame)
	--l'oggetto frame è creato automaticamente dalla chiamata in ambiente wiki
	--da tale oggetto recupero i parametri del template che stanno a livello superiore
	return _Marker(frame:getParent())
end

function p.MarkerInvoke(frame)
	--l'oggetto frame è creato automaticamente dalla chiamata in ambiente wiki
	--tale oggetto contiene anche i parametri passati
	return _Marker(frame)
end

function p.MarkerModule(frame)
	--l'oggetto frame non esiste e va creato sul momento
	--i parametri passati alla funzione vanno aggiunti manualmente
	local Cframe = mw.getCurrentFrame()
	Cframe.args = frame.args
	return _Marker(Cframe)
end

return p