Module:PTV route/sandbox: Difference between revisions
Appearance
Content deleted Content added
load data from Module:PTV route/data |
allow the first positional parameter instead of "route" |
||
Line 3: | Line 3: | ||
require('Module:No globals') |
require('Module:No globals') |
||
local data = mw.loadData('Module: |
local data = mw.loadData('Module:PTVBus/data') |
||
local p = {} |
local p = {} |
||
local function normalizeArguments(args) |
|||
-- Make a copy of the table so we don't alter our caller's data |
|||
local ret = {} |
|||
for key, value in pairs(args) do |
|||
ret[key] = value |
|||
end |
|||
-- Set aliases |
|||
ret.route = ret.route or ret[1] |
|||
return ret |
|||
end |
|||
local function makeExternalLink(url, display) |
local function makeExternalLink(url, display) |
||
Line 46: | Line 57: | ||
function p._main(args) |
function p._main(args) |
||
-- Generate a link to a Public Transport Victoria route. |
-- Generate a link to a Public Transport Victoria route. |
||
args = normalizeArguments(args) |
|||
local routeData = getRouteData(args.mode, args.route) |
local routeData = getRouteData(args.mode, args.route) |
||
if not routeData then |
if not routeData then |
||
Line 58: | Line 70: | ||
function p._cite(args) |
function p._cite(args) |
||
-- Generate a citation for a Public Transport Victoria route. |
-- Generate a citation for a Public Transport Victoria route. |
||
args = normalizeArguments(args) |
|||
local routeData = getRouteData(args.mode, args.route) |
local routeData = getRouteData(args.mode, args.route) |
||
if not routeData then |
if not routeData then |
Revision as of 15:28, 9 March 2019
![]() | This is the module sandbox page for Module:PTV route (diff). |
This module implements {{PTV route}} and {{cite PTV route}}. Please see the template pages for documentation.
To update the bus route data, please see Module:PTV route/data.
-- This module implements [[Template:PTV route]].
require('Module:No globals')
local data = mw.loadData('Module:PTVBus/data')
local p = {}
local function normalizeArguments(args)
-- Make a copy of the table so we don't alter our caller's data
local ret = {}
for key, value in pairs(args) do
ret[key] = value
end
-- Set aliases
ret.route = ret.route or ret[1]
return ret
end
local function makeExternalLink(url, display)
-- Make an external link, given a URL and an optional display string.
if display then
return string.format('[%s %s]', url, display)
else
return string.format('[%s]', url)
end
end
local function makePtvUrl(routeData)
-- Make the URL for a PTV route page.
return 'https://www.ptv.vic.gov.au/route/' .. routeData.page
end
local function makeDisplayText(args, routeData)
-- Make the display text for a link to a PTV route page.
if args.display == 'num' or args.display == 'number' or args.numtext then
return args.route
elseif args.display == 'desc' or args.display == 'description' or args.deftext then
return routeData.text
else
return args.text
end
end
local function getRouteData(mode, route)
-- Get the route data for a route from the data module.
if not route then
return nil
end
mode = mode or 'bus'
local modeData = data[mode]
if not modeData then
return nil
end
return modeData[route]
end
function p._main(args)
-- Generate a link to a Public Transport Victoria route.
args = normalizeArguments(args)
local routeData = getRouteData(args.mode, args.route)
if not routeData then
return nil
end
local url = makePtvUrl(routeData)
local display = makeDisplayText(args, routeData)
return makeExternalLink(url, display)
end
function p._cite(args)
-- Generate a citation for a Public Transport Victoria route.
args = normalizeArguments(args)
local routeData = getRouteData(args.mode, args.route)
if not routeData then
return nil
end
local title
if args.route == routeData.text then
title = routeData.text
else
title = string.format('%s %s', args.route, routeData.text)
end
local citeArgs = {
title = title,
url = makePtvUrl(routeData),
publisher = 'Public Transport Victoria',
df = 'dmy-all',
}
for _, field in ipairs{
'access-date',
'accessdate',
'archive-date',
'archivedate',
'archive-url',
'archiveurl',
'dead-url',
'deadurl',
} do
citeArgs[field] = args[field]
end
return mw.getCurrentFrame():expandTemplate{
title = 'Cite web',
args = citeArgs,
}
end
local function makeInvokableFunction(func, wrappers)
-- Make a function that can be accessed with #invoke.
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = wrappers,
})
return func(args)
end
end
p.main = makeInvokableFunction(p._main, 'Template:PTV route')
p.cite = makeInvokableFunction(p._cite, 'Template:Cite PTV route')
return p