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 .. ' ' .. mw.smw.info('Nieprawidłowa lokalizacja', 'warning'), {}
	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 = '[https://www.openstreetmap.org/?mlat='
		.. fParts[1] .. '&mlon=' .. fParts[2] .. '#map=13 ' .. geocoded .. ']</span>'
	
	if mw.ustring.find(name, '^[0-9%.,][0-9%.,]') then
		text = '<span style="font-style: italic;">' .. text
	else
		text = name .. ', <span style="font-style: italic; font-size: 0.8em">' 
			.. 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/ea2e82fed9f1e261f4d4672ff67a9bc0f8e98662/src/TemplateParser.php#L149
		geoF = mw.ustring.gsub(geoF, ',', ';')
		text = text .. '<span class="geo" style="display:none">' .. geoF .. '</span>'
	end
	
	return geocoded, text, props
end

return p