Jump to content

Module:PTV route/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:57, 7 March 2019 (refactor p._main - split it into three separate functions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements [[Template:PTVBus]].

local data = mw.loadData('Module:PTVBus/data')
local p = {}

local function formatUrlLink(url, display)
	if display then
		return string.format('[%s %s]', url, display)
	else
		return string.format('[%s]', url)
	end
end

local function makeDisplayText(args, routeData)
	local display
	if args.numtext then
		display = args.route
	elseif args.deftext then
		display = routeData.text
	else
		display = args.text
	end
	return display
end

function p._main(args)
	if not args.route then
		return nil
	end

	local routeData = data[args.route]
	if not routeData then
		return nil
	end

	local url = 'https://www.ptv.vic.gov.au/route/' .. routeData.page
	local display = makeDisplayText(args, routeData)
	return formatUrlLink(url, display)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:PTVBus',
	})
	return p._main(args)
end

return p