Module:Road data/parser
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 41,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module exports the parser
function, which can be used from within Lua to fetch an entry from the appropriate string module. See the comment for the p.parser
function for the function's parameters.
local p = {}
local format = mw.ustring.format
local gsub = mw.ustring.gsub
local trim = mw.text.trim
local upper = mw.ustring.upper
local country
local prepattern = "%[(%w+)%|(.*)%|(.*)%|(.*)%]"
local pattern = "%%(%w+)%%"
local function parser(formatStr, args)
local function testArgs(test, equals, ifexists, ifnot)
if equals ~= '' then
if args[test] == equals then return ifexists else return ifnot end
else
if args[test] and args[test] ~= '' then return ifexists else return ifnot end
end
end
if type(formatStr) == 'table' then
local route = args.route
formatStr = formatStr[route] or formatStr.default
end
if formatStr == '' then return '' end
local preprocessed = gsub(formatStr, prepattern, testArgs)
local final = gsub(preprocessed, pattern, args)
return trim(final)
end
local function formatString(args, form, part)
local formatStringData = mw.loadData("Module:Road data/strings")
local state = upper(args.state or args.province or '')
if args.country then
country = upper(args.country)
else
countryModule = mw.loadData("Module:Road data/countrymask")
country = countryModule[state] or 'UNK'
end
local type = args.type
local typeTable
local countryTable = formatStringData[country] or {}
if state then
local stateTable = countryTable[state]
if stateTable then
typeTable = stateTable[type]
else
typeTable = countryTable[type]
end
else
typeTable = countryTable[type]
end
if typeTable then
return part and typeTable[form][part] or typeTable[form]
else
return ''
end
end
function p.parser(passedArgs, form, part)
local args = {state = passedArgs.state, type = passedArgs.type, route = passedArgs.route,
denom = passedArgs.denom, county = passedArgs.county, dab = passedArgs.dab}
local formatStr = formatString(args, form, part)
return parser(formatStr, args)
end
return p