Module:Marker

--[[ 	Source script:	https://it.wikivoyage.org/wiki/Modulo:Marker 	Maintainer:		Andyrom75  	Lazy loads: 	require('Module:WikidataIB').getSiteLink ]] local coord2Dec = require('Module:Coordinates').toDec local type2color = require('Module:TypeToColor').convertImpl local i18n = require( 'Module:Marker/i18n' ) local par = i18n.parameters   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[par.image]) and ('[[File:' .. args[par.image] .. '|300px]]') or '' 	local tagArgs = { 		zoom = tonumber( args[par.zoom] ), 		latitude = tonumber( args[par.lat] ), 		longitude = tonumber( args[par.long] ), 		show = args[par.show], 		group = args[par.group], 		description = (_isDefined(args[par.description]) or '') .. image4map 	} 	if _isDefined( args[par.text] ) then 		tagArgs.text = args[par.text] 		if not args[par.useIcon] then 			tagArgs.class = 'no-icon' 		end 	end  	local geoJson = { 		type = 'Feature', 		geometry = { 			type = 'Point', 			coordinates = { tagArgs.longitude, tagArgs.latitude } 		}, 		properties = { 			title = args[par.name], 			description = tagArgs.description, 			['marker-symbol'] = args[par.symbol], 			['marker-color'] = args[par.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 = '' 	local WLErrMsg = '' 	--shortcut variable 	local nome = args[par.name] or '' 	local tag = ((args[par.islisting] ~= i18n.msg.yes) and mw.html.create( 'span' ):addClass( 'vcard' )) or mw.html.create() 	local anchor = ''  	if _isDefined( args[par.wikidata] ) then 		-- Se manca una delle due coordinate, provo a estrarle entrambe da Wikidata 		if not _isDefined(args[par.lat]) or not _isDefined(args[par.long]) then 			local coords = mw.wikibase.getBestStatements( args[par.wikidata], 'P625') 			if coords[1] and coords[1].mainsnak.datavalue then 				args[par.lat] = coords[1].mainsnak.datavalue.value.latitude 				args[par.long] = coords[1].mainsnak.datavalue.value.longitude 			end 		end 		-- Se non è stata fornita l'immagine, provo a estrarla da Wikidata 		if not _isDefined(args[par.image]) then 			local immagine = mw.wikibase.getBestStatements( args[par.wikidata], 'P18') 			args[par.image] = immagine[1] and immagine[1].mainsnak.datavalue.value 		end 	end  	-- Se sono presenti le coordinate creo il marker tag 	if args[par.lat] ~= 'NA' and args[par.long] ~= 'NA' then 		if _isDefined(args[par.lat]) and _isDefined(args[par.long]) then 			-- arguments setup 			args[par.lat] = coord2Dec(args[par.lat] or 0, '', 6).dec 			args[par.long] = coord2Dec(args[par.long] or 0, '', 6).dec 			args[par.mtype] = (_isDefined(args[par.mtype]) and string.lower(args[par.mtype]) ) or 'listing' 			args[par.zoom] = _isDefined(args[par.zoom]) or '17' 			args[par.group] = (_isDefined(args[par.group]) and string.lower(args[par.group]) ) or args[par.mtype] 			args[par.symbol] = '-number-' .. ( args[par.counter] or args[par.mtype] or 'listing' ) 			args[par.color] = type2color( args[par.mtype] ) 			args[par.show] = args[par.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[par.lat] ) 					:done() 					:tag( 'abbr' ) 						:addClass( 'longitude' ) 						:wikitext( args[par.long] ) 			:allDone() 			:tag( 'span' ) 				:attr( 'title', i18n.msg.tooltip ) 				:wikitext( mapTagOutput ) 		else 			-- costruisco l'elenco delle chiavi valide nella lingua configurata 			local listingTypes = {} 			for _, translatedKey in pairs(i18n.listingTypes) do 				listingTypes[translatedKey] = true 			end  			if mw.title.getCurrentTitle().namespace == 0 and args[par.mtype] and listingTypes[string.lower(args[par.mtype])] then 				anchor = anchor .. "[[Category:" .. string.lower(args[par.mtype]) .. i18n.categories.noCoords .. "]]" 			end 		end 	end  	-- costruisco l'ancora sul nome ed eventualmente gli associo il sitoweb 	if nome ~= '' then 		local anchor2 		-- se il nome è puramente testuale lo trasformo in un link esterno, altrimenti gli affianco un icona 		if _isDefined(args[par.url]) then 			anchor2 = string.match(nome, '%[%[') 				and (nome .. '<span class="noprint listing-external-icons">&#32;[[File:' .. i18n.icons.url .. '|15px|class=listing-external-url|link=' .. args[par.url] .. '|' .. i18n.msg.url .. ']]</span>') 				or ('[' .. args[par.url] .. ' ' .. nome .. ']') 		end 		if mw.title.getCurrentTitle().namespace == 0 then 			-- costruisco l'ancora sul nome del marker ma solo nel NS principale 			anchor = anchor .. '<span id="' .. mw.uri.anchorEncode( nome ) .. '" class="fn org listing-name">' .. (anchor2 or nome) .. '</span>' 		else 			anchor = anchor .. (anchor2 or nome) 		end 	end 	if mw.title.getCurrentTitle().namespace == 0 then 		-- costruisco l'ancora sull'istanza wikidata ma solo nel NS principale 		if _isDefined( args[par.wikidata] ) then 			anchor = anchor .. '<span id="' .. args[par.wikidata] .. '" class="noprint"></span>' 		end 		anchor = anchor .. '[[Category:' .. i18n.categories.withMarker .. ']]' 	end  	-- Se l'immagine fornita non esiste genero un messaggio di errore 	if _isDefined( args[par.image] ) then 		local image = mw.title.new(args[par.image], 'Media')  		if image and image.exists then 			args[par.description] = '[[' .. image.fullText .. '|100x100px|' .. nome .. ']]' 		else 			imageErrMsg = mw.html.create( 'span' ) 				:addClass('imageinfo debuginfo') 				:wikitext( i18n.errorMsg.wrongFile ) 		end 	end  	if (args[par.islisting] ~= i18n.msg.yes) and _isDefined( args[par.wikidata] ) then 		local getSiteLink = require('Module:WikidataIB').getSiteLink 		if _isDefined( getSiteLink{args={wiki=i18n.wiki.home, qid=args[par.wikidata]}} ) then 			if not nome:match('%[%[') then 				anchor = anchor .. '[[Category:' .. i18n.categories.noWikilink ..']]' 				WLErrMsg = mw.html.create( 'span' ) 					:addClass('debuginfo') 					:wikitext( i18n.errorMsg.missingWL ) 			end 		else 			local wpLink = _isDefined( getSiteLink{args={wiki=i18n.wiki.wikipedia, qid=args[par.wikidata]}} ) 			anchor = anchor .. (wpLink and '<span class="noprint listing-sister-icons">&#32;[[File:' .. i18n.icons.wikipedia .. '|15px|class=listing-sister-wikipedia|link=w:' .. wpLink .. '|' .. wpLink .. i18n.msg.wikipedia .. ']]</span>' or '') 		end 	end  	return _templateStyle( frame, 'Marker/styles.css' ) .. tostring(tag:node(imageErrMsg):node(WLErrMsg):wikitext(' ' .. anchor)) end  --Interfacce del modulo local p = {}  function p.MarkerTemplate(frame) 	return _Marker(frame:getParent()) end  function p.MarkerInvoke(frame) 	return _Marker(frame) end  function p.MarkerModule(frame) 	local Cframe = mw.getCurrentFrame() 	Cframe.args = frame.args 	return _Marker(Cframe) end  return p