Module:Sandbox/Bon

From OpenStreetMap Wiki
Jump to navigation Jump to search
-- The following module, intended to be used inside a table, creates all table rows needed to represent a public transport service line,
-- its variants and its routes along with its mapping progress, update, etc.

local p = {}

function p.test(frame)
	local args = frame:getParent().args
	
	----------------------------------------------------------------------------------------------------
	-- Parametri numerali
	--  --- con n = 0,1,2,...
	--  - 2, 5, ... = (3n + 2) = nome percorso
	--  - 3, 6, ... = (3n + 3) = numero relazione percorso
	--  - 4, 7, ... = (3n + 4) = progresso mappatura/aggiornamento percorso
	
	-- Numbered parameters
	--  --- with n = 0,1,2,...
	--  - 2, 5, ... (3n + 2) = route name
	--  - 3, 6, ... (3n + 3) = route relation number
	--  - 4, 7, ... (3n + 4) = route mapping/updating progress
	---------------------------------------------------------------------------------------------------
	
	
	------------------------------------------------------------------------
	-- translation = require('Module:Snadbox/Bon/Translations').translations
	translation = {}
	
	translation['en'] = {
		name = 'name',		-- line name
		colour = 'colour',	-- line colour
		var = 'var',		-- variant
		varL = 'varL',		-- length of the variant
		upd = 'upd',		-- last update
		chk = 'chk',		-- last check
		tags = 'tags',		-- other tags
		notes = 'notes'		-- notes
		}
	
	translation['it'] = {
		name = 'nome',		-- nome linea
		colour = 'colore',	-- colore linea
		var = 'var',		-- variante
		varL = 'Lvar',		-- lunghezza variante
		upd = 'agg',		-- aggiornamento
		chk = 'con',		-- controllo
		tags = 'tag',		-- altri tag
		notes = 'note'		-- note
		}
	------------------------------------------------------------------------
	
	function setLang()
		langCode = mw.text.trim(lang or frame.args.lang or args.lang or 'en')
		if not translation[langCode] then
			local fallbacks_table = mw.language.getFallbacksFor(langCode)
			for _, fb_code in ipairs(fallbacks_table) do
				if translation[fb_code] then langCode = fb_code break end
			end
		end
		Wlang = mw.getLanguage(langCode)
	end
	
	function translate()
		local tl_table = translation[langCode]
		for argname, tl_argname in pairs(tl_table) do
			args[argname] = args[tl_argname]
			local i = 1
			while args[tl_argname..i] do
				args[argname..i] = args[tl_argname..i]
				i = i + 1
			end
		end
	end
	
	local function defaults()		--defaults(lang, rP,nrP)
		
		-- Template parameters
		dateFormat = dateFormat or 'j F Y'	-- defaults to d/M/Y date formatting (e.g. "5 April 1990") if no format is specified
		rP = rP or frame.args[1] or 3		-- number of repeating parameters
		nrP = nrP or frame.args[2] or 1		-- number of non repeating parameters
		
		-- Network and operator parameters
		Nnetwork = args.network or frame.args.network or nil
		Noperator = args.operator or frame.args.operator or nil
		
		-- Line parameters
		Lref = mw.text.trim(args[1] or '')
		Lname = args.name or nil
		Lcolour = args.colour ~= '' and args.colour or nil
		Lnotes = args.notes or nil
		
		-- Variant parameters
		Vlabel = {}
		Vlength = {}
		Vupdate = {}
		Vcheck = {}
		Vtags = {}
		Vlabel[1] = args.var or args.var1 or nil
		Vupdate[1] = args.upd or args.upd1 or nil
		Vcheck[1] = args.chk or args.chk1 or nil
		Vtags[1] = args.tags or args.tags1 or nil
		if Vlabel[1] then
			Vlength[1] = tonumber(args.varL or args.varL1 or 2)
			local i = 2
			while args['var'..i] do
				Vlabel[i] = args['var'..i]
				Vlength[i] = tonumber(args['varL'..i] or 2)
				Vupdate[i] = args['upd'..i] or nil
				Vcheck[i] = args['chk'..i] or nil
				Vtags[i] = args['tags'..i] or nil
				i = i + 1
			end
			n_var = #Vlabel
		end
		
		-- Route parameters
		Rname = {}
		Rrel = {}
		Rprogr = {}
		local i,j = 1, nrP + 1
		while args[j] or args[j+1] do
			Rname[i] = tostring(args[j] or '')
			Rrel[i] = tostring(args[j+1] or '')
			Rprogr[i] = tostring(args[j+2] or '')
			i = i + 1
			j = j + rP
		end
		n_routes = #Rname
	end
	
	
	local function overpass_api_link_builder()
		network = frame.args.network or args.network
		operator = frame.args.operator or args.operator
		if not (network or operator) then
			return ''
		end
		l = 'https://overpass-api.de/api/sketch-line?'
		if network then
			network = mw.text.trim(network)
			network = mw.ustring.gsub(network, ' ', '+')
			l = l..'&network='..network
		end
		if args[1] then
			local ref = mw.text.trim(Lref)
			ref = mw.ustring.gsub(ref, ' ', '+')
			l = l..'&ref='..ref
		end
		if operator then
			operator = mw.text.trim(operator)
			operator = mw.ustring.gsub(operator, ' ', '+')
			l = l..'&operator='..operator
		end
		return l
	end
	
	local function other_links()
	end
	
	
	function line_cell()
		r = ''
		r = r..'\n|-\n|rowspan='..n_routes
		if Vlabel[1]
			then r = r..' style="width:6.5em; border-right:none; text-align:center; font-size:120%; font-weight:bold"| '
			else r = r..' colspan=2 style="text-align:center; font-size:120%; font-weight:bold"|' end
		r = r..Lref
		if Lname then
			r = r..'<br/><span style="font-size:65%">'..Lname..'</span>' end
	end
	
	local function var_cell(Vi, length)
		if Vi ~= 1 then r = r..'\n|-' end
		r = r..'\n|rowspan='..length..' style="width:0.5em; border-left:none; text-align:center; font-style:italic"|'..Vlabel[Vi]
	end
	
	local function rel_and_route_cell(Ri)
		if Ri == 1 then
			r = r..'<span class="anchor" id="'..Lref..'"></span>'
		end
		r = r..'\n|'..mw.getCurrentFrame():expandTemplate{ title = 'RelationIconLink', args = { Rrel[Ri] } }..' '..Rname[Ri]
	end
	
	local function progress_cell(Ri)
		r = r..'\n|'..mw.getCurrentFrame():expandTemplate{ title = 'Progress', args = { Rprogr[Ri] } }
	end
	
	local function last_update_cell(Vi, length)
		r = r..'\n|rowspan='..length..' style="text-align:center" data-sort-value="'..(Vcheck[Vi] or Vupdate[Vi] or '')..'" | '
		if Vupdate[Vi] then
			r = r..Wlang:formatDate(dateFormat,Vupdate[Vi]) end
		if Vcheck[Vi] then
			r = r..'<br/> [[File:Yes check.svg|10px]] <span style="font-size:75%">'..Wlang:formatDate(dateFormat,Vcheck[Vi])..'</span>'
		end
	end
	
	local function other_tags_cell(Vi, length)
		r = r..'\n|rowspan='..length..' style="vertical-align:top"| '
		if colour then
			r = r..mw.getCurrentFrame():expandTemplate { title = 'Tag', args = { 'colour', '', Lcolour } }
			..' '..mw.getCurrentFrame():expandTemplate { title = 'Color sample', args = { Lcolour } }
			if Vtags[Vi] then r = r..'<br/>' end
		end
		if Vtags[Vi]
		then r = r..Vtags[Vi] end
	end
	
	local function links_cell(length)
		r = r..'\n|rowspan='..length..'| '
		link = overpass_api_link_builder()
		if link ~= '' then r = r..'['..link..' Overpass API] ' end
		other_links()
	end
	
	local function notes_cell(length)
		r = r..'\n|rowspan='..length..'|'..(Lnotes or '')
	end
	
	local function make_var(Vi,Ri)
		local length = Vlength[Vi]
		var_cell(Vi,length)
		rel_and_route_cell(Ri)
		progress_cell(Ri)
		last_update_cell(Vi, length)
		other_tags_cell(Vi, length)
		if Vi == 1 then
			links_cell(n_routes)
			notes_cell(n_routes)
		end
		for i=1,length-1 do
			r = r..'\n|-'
			rel_and_route_cell(Ri+i)
			progress_cell(Ri+i)
		end
	end
	
	function make_line()
		local Ri = 1
		local Vi = 1
		line_cell()
		if Vlabel[1] then
			for _,label in ipairs(Vlabel) do
				make_var(Vi,Ri)
				Ri = Ri + Vlength[Vi]
				Vi = Vi + 1
			end
		else
			rel_and_route_cell(1)
			progress_cell(1)
			last_update_cell(1, n_routes)
			other_tags_cell(1, n_routes)
			links_cell(n_routes)
			notes_cell(n_routes)
			Ri = Ri + 1
			for i=2,n_routes do
				r = r..'\n|-'
				rel_and_route_cell(i)
				progress_cell(i)
			end
		end
	end
	
	setLang()
	translate()
	defaults()
	
	make_line()
	
	return r
end

return p