Moduł:Lokalizacja

Z Nonsensopedii, polskiej encyklopedii humoru

local tools = require('Moduł:Narzędzia')
local p = {}

-- parsuje nazwę lokalizacji i zwraca trzy wartości: 
-- * koordynaty wg OSM
-- * tekst do wyświetlenia w szablonie
-- * tablicę z atrybutami SMW do ustawienia
-- ustawienie drugiego parametru na true zwraca w tekście dodatkowo niewidzialny
-- tag dla różnych programistycznych cudaków
function p.parseLocation(frame, name, addInvisibleTag)
	local geocoded = frame:callParserFunction('#geocode', name)
	if geocoded == 'Geocoding failed' then
		return false, name .. tools.makeWarning('Nieprawidłowa lokalizacja'), {}
	end
	
	local geoF = frame:callParserFunction( '#geocode', 
		{ name, format = 'float', directional = 'no' } )
	
	local props = {
		['Ma lokalizację'] = geocoded
	}
	
	local fParts = mw.text.split( geoF, ', ', true )
	local text = '<span style="font-style: italic;">[https://www.openstreetmap.org/?mlat='
		.. fParts[1] .. '&mlon=' .. fParts[2] .. ' ' .. geocoded .. ']</span>'
		
	if not mw.ustring.find(name, '^[0-9%.,][0-9%.,]') then
		text = name .. ', ' .. text .. ', [https://nominatim.openstreetmap.org/search.php?q=' .. mw.uri.encode(name) .. '&polygon_geojson=1 zobacz miejsce].'
		props['Ma nazwę miejsca'] = name
	end
	
	if addInvisibleTag then
		-- format ma być zgodny z tym na Commons
		-- vide https://github.com/wikimedia/mediawiki-extensions-CommonsMetadata/blob/master/src/TemplateParser.php#L149
		local geocodedFloat = frame:callParserFunction('#geocode', { name, format = 'float', directional = 'no' })
		geocodedFloat = mw.ustring.gsub(geocodedFloat, ',', ';')
		text = text .. '<span class="geo" style="display:none">' .. geocodedFloat .. '</span>'
	end
	
	return geocoded, text, props
end

return p