Modul:Koordinaten

Aus GaretienWiki
Version vom 2. August 2018, 09:53 Uhr von VolkoV (D | B) (Die Seite wurde neu angelegt: „ local p = {} function robusttrim(s) if s==nil then return '' end local p=string.gsub(s,'%[%[SMW::on%]%]','') p=string.gsub(p,'%[%[SMW::off%]%]','')…“)
(U) ← Nächstältere Version | Aktuelle Version (U) | Nächstjüngere Version → (U)
Zur Navigation springen Zur Suche springen
Dokumentation und Testfälle unter Modul:Koordinaten/Doku.
	local p = {}
	
	function robusttrim(s)
		if s==nil then return '' end
		local p=string.gsub(s,'%[%[SMW::on%]%]','')
		p=string.gsub(p,'%[%[SMW::off%]%]','')
		return mw.text.trim(p)
	end
	
	function split(inputstr,sep)
		if sep==nil then sep="%s" end
		local t={}
		local i=1
		for str in string.gmatch(inputstr,"([^"..sep.."]+)") do
			t[i]=str
			i=i+1
		end
		return t
	end
	
	function holeKoordinaten(frame,ort)
		if ort=='' then return '' end
		local a=frame:callParserFunction(
			'#show',ort,
			'?Koordinaten'
		)
		return a
	end
	
	function holeViewbox(frame,ort)
		if ort=='' then return '' end
		local a=frame:callParserFunction(
			'#show',ort,
			'?Viewbox'
		)
		return a
	end
	
	function holeHandlungsorte(frame,geschichte)
		if geschichte=='' then return '' end
		local a=frame:callParserFunction(
			'#show',geschichte,
			'?Handlungsort ist#=',
			'sep=€'
		)
		return a
	end
	
	function viewboxToKoordinaten(viewbox)
		local result=""
		local width=10000000
		for token in split(viewbox,",") do
			local subtokens={}
			local i=0
			for subtoken in split(token) do
				subtokens[i]=0+subtoken
				i=i+1
			end
			if subtokens[3] then
				if subtokens[3]<width then
					width=subtokens[3]
					local x=subtokens[0]+subtokens[2]/2
					local y=subtokens[1]+subtokens[3]/2
					result=x..";"..y
				end
			end
		end
		return result
	end
	
	function erzeugeKoordinaten(frame,geschichte)
		if geschichte=='' then return '' end
		local orte=holeHandlungsorte(frame,geschichte)
		local ortet=split(orte,'€')

		local result=""
		for ort in ortet do
			koord=holeKoordinaten(frame,ort)
			if koord=="" then
				view=holeViewbox(frame,ort)
				koord=viewboxToKoordinaten(view)
			end
			result=result+"\n"+geschichte+";0!14;;"+koord
		end
		
		return result
	end
	
	function p.Koordinaten()
		frame=mw.getCurrentFrame()
		geschichte=robusttrim(frame.args['Geschichte'])
		if(geschichte=='') then return '' end
		return erzeugeKoordinaten(frame,geschichte)
	end
	
	return p