Module:Adjacent stations/convert

From Wikipedia, the free encyclopedia

local p = {}

function p.adjacent(frame)
	local args = frame.args
	local code = mw.text.split(mw.ustring.gsub(args[1], '^%s*{{(.*)}}%s*$', '%1'), '%s*}}%s*{{%s*')
	local system
	local group = 0
	local delete = {
		['s-rail'] = true,
		['s-rail-next'] = true,
		['s-rail-national'] = true,
		['s-start'] = true,
		['s-rail-start'] = true,
		['start'] = true,
		['s-end'] = true,
		['end'] = true
	}
	local order = {
		'line', 'left', 'right', 'to-left', 'to-right',
		'oneway-left', 'oneway-right', 'through-left', 'through-right',
		'reverse', 'reverse-left', 'reverse-right',
		'note-left', 'note-mid', 'note-right', 'transfer'
		-- circular: use module subpage
		-- state: not implemented
	}
	local replace = {
		['previous'] = 'left',
		['next'] = 'right',
		['type'] = 'to-left',
		['type2'] = 'to-right',
		['branch'] = 'type',
		['note'] = 'note-left',
		['notemid'] = 'note-mid',
		['note2'] = 'note-right',
		['oneway1'] = 'oneway-left',
		['oneway2'] = 'oneway-right',
		['through1'] = 'through-left',
		['through2'] = 'through-right'
	}
	local remove_rows = {}
	local data = {}
	for i, v in ipairs(code) do
		code[i] = mw.ustring.gsub(code[i], '\n', ' ')
		local template = mw.ustring.lower(mw.text.trim(mw.ustring.match(code[i], '^[^|]+')))
		code[i] = mw.ustring.match(code[i], '(|.+)$')
		if template == 's-line' then
			data[i] = {}
			local this_system = mw.text.trim(mw.ustring.match(code[i], '|%s*system%s*=([^|]+)'))
			code[i] = mw.text.split(code[i], '%s*|%s*')
			for i1, v1 in ipairs(code[i]) do
				local tmp = mw.text.split(v1, '%s*=%s*')
				if tmp[3] then
					tmp[2] = mw.ustring.gsub(v1, '^.-%s*=', '')
				end
				tmp[1] = replace[tmp[1]] or tmp[1]
				if tmp[2] then
					-- checks for matching brackets
					local curly = select(2, mw.ustring.gsub(tmp[2], "{", ""))-select(2, mw.ustring.gsub(tmp[2], "}", ""))
					local square = select(2, mw.ustring.gsub(tmp[2], "%[", ""))-select(2, mw.ustring.gsub(tmp[2], "%]", ""))
					if not (curly == 0 and square == 0) then
						local count = mw.clone(i1)+1
						while not (curly == 0 and square == 0) do
							tmp[2] = tmp[2]..'|'..code[i][count]
							curly = curly+select(2, mw.ustring.gsub(code[i][count], "{", ""))-select(2, mw.ustring.gsub(code[i][count], "}", ""))
							square = square+select(2, mw.ustring.gsub(code[i][count], "%[", ""))-select(2, mw.ustring.gsub(code[i][count], "%]", ""))
							code[i][count] = ''
							count = count+1
						end
					end
					data[i][tmp[1]] = tmp[2]
				end
			end
			if (this_system ~= system) or (not system) then
				system = this_system
				data[i]['system'] = system
			else
				data[i]['system'] = nil
			end
			local last = data[i-1] or data[i-2] or data[i-3]
			if last then
				for k1, v1 in pairs({
					['hide1'] = {'left', 'to-left', 'note-left', 'oneway-left'},
					['hide2'] = {'right', 'to-right', 'note-right', 'oneway-right'},
					['hidemid'] = {'type', 'note-mid'}
					}) do
					if data[i][k1] then
						for k2, v2 in ipairs(v1) do
							if not data[i][v2] then
								data[i][v2] = last[v2]
							end
						end
					end
				end
			end
			code[i] = {}
			local X = '|'
			local Y = (i+group)..'='
			if data[i]['system'] then
				table.insert(code[i], '|system')
				table.insert(code[i], Y)
				table.insert(code[i], data[i]['system'])
				table.insert(code[i], '\n')
			end
			for i1, v1 in ipairs(order) do
				if data[i][v2] then
					table.insert(code[i], X)
					table.insert(code[i], v2)
					table.insert(code[i], Y)
					table.insert(code[i], data[i][v2])
				end
			end
			code[i] = table.concat(code[i])
		elseif template == 's-note' then
			code[i] = mw.ustring.gsub(code[i], '|%s*text%s*=', '|header'..i+group..'=')
			code[i] = mw.ustring.gsub(code[i], '|%s*wide%s*=[^|]*', '')
		elseif template == 's-text' then
			code[i] = mw.ustring.gsub(code[i], '|%s*text%s*=', '|note-row'..i+group..'=')
		elseif delete[template] then
			code[i] = ''
			table.insert(remove_rows, 1, i) -- at the start, so that the rows are deleted in reverse order
			group = group-1
		end
	end
	for i, v in ipairs(remove_rows) do
		table.remove(code, v)
	end
	code = table.concat(code, '\n')
	local t = {'{{Adjacent stations', '\n}}'}
	system = mw.ustring.match(code, '|system(%d*)=')
	code = mw.ustring.gsub(code, '\n\n+', '\n')
	if tonumber(system) > 1 then
		-- If s-line isn't the first template then the system will have to be moved to the top
		system = mw.ustring.match(code, '|system%d*=([^|]*[^|\n])')
		code = mw.ustring.gsub(code, '|system%d*=[^|]*', '')
		code = '\n|system1='..system..code
	elseif not mw.ustring.match(code, '^[^{%[]*|[^=|]+2=') then
		-- If there's only one parameter group then there's no need to have line breaks
		code = mw.ustring.gsub(code, '\n', '')
		code = mw.ustring.gsub(code, '(|[^=|]+)1=', '%1=')
		t[2] = '}}'
		if not mw.ustring.match(code, '[%[{]') then
			code = mw.ustring.gsub(code, '|[^=|]*=$', '')
			code = mw.ustring.gsub(code, '|[^=|]*$', '')
		end
	end
	if not mw.ustring.match(code, '[%[{]') then
		code = mw.ustring.gsub(code, '|[^=|]*=|', '|')
		code = mw.ustring.gsub(code, '|[^=|]*|', '|')
		code = mw.ustring.gsub(code, '|[^=|]*=\n', '\n')
		code = mw.ustring.gsub(code, '|[^=|]*\n', '\n')
	end
	return t[1]..code..t[2]
end

return p