Difference between revisions of "Module:Road data/parser"
Jump to navigation
Jump to search
blackwiki>Mitchazenia m (Changed protection level of Module:Road data/parser: Adjust per request ([Edit=Allow only template editors and admins] (indefinite) [Move=Allow only administrators] (indefinite))) |
blackwiki>Happy5214 (August update: Add multiple shield support; use empty string type as fallback for non-existent types) |
||
| Line 32: | Line 32: | ||
formatTable = formatStr | formatTable = formatStr | ||
local hook = formatStr.hook | local hook = formatStr.hook | ||
| − | if hook then | + | local both = formatStr[2] |
| + | if both then | ||
| + | local first = formatStrInTable(formatStr[1]) | ||
| + | local second = formatStrInTable(both) | ||
| + | return {first, second} | ||
| + | elseif hook then | ||
local hooksModule = require "Module:Road data/parser/hooks" | local hooksModule = require "Module:Road data/parser/hooks" | ||
local hookFunction = hooksModule[hook] or error("Hook '" .. hook .. "' does not exist", 0) | local hookFunction = hooksModule[hook] or error("Hook '" .. hook .. "' does not exist", 0) | ||
| Line 40: | Line 45: | ||
return formatStrInTable(formatStr[arg] or formatStr.default) | return formatStrInTable(formatStr[arg] or formatStr.default) | ||
end | end | ||
| + | end | ||
| + | local function parse(formatStr) | ||
| + | local preprocessed = gsub(formatStr, prepattern, testArgs) | ||
| + | local parsedStr = gsub(preprocessed, pattern, args) | ||
| + | local final = trim(parsedStr) | ||
| + | if formatTable.ifexists then | ||
| + | local exists = ifexists(final) | ||
| + | if exists then | ||
| + | return final | ||
| + | else | ||
| + | return parser(formatTable.otherwise, args, form) | ||
| + | end | ||
| + | end | ||
| + | return final | ||
end | end | ||
formatStr = formatStrInTable(formatStr) | formatStr = formatStrInTable(formatStr) | ||
if not formatStr or formatStr == '' then return '' end | if not formatStr or formatStr == '' then return '' end | ||
| − | + | if type(formatStr) == 'table' then | |
| − | + | local first = parse(formatStr[1]) | |
| − | + | local second = parse(formatStr[2]) | |
| − | + | return first, second | |
| − | local | + | else |
| − | + | return parse(formatStr) | |
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | |||
end | end | ||
| − | local function formatString(args, form | + | local function formatString(args, form) |
local function getTypeData(module, type) | local function getTypeData(module, type) | ||
local success, moduleData = pcall(mw.loadData, module) | local success, moduleData = pcall(mw.loadData, module) | ||
if not success then return '' end | if not success then return '' end | ||
local typeTable = moduleData[type] or moduleData[''] | local typeTable = moduleData[type] or moduleData[''] | ||
| + | local defaultTable = moduleData[''] or {} | ||
if typeTable then | if typeTable then | ||
local alias = typeTable.alias | local alias = typeTable.alias | ||
| Line 69: | Line 84: | ||
return getTypeData(aliasedModule, aliasedType) | return getTypeData(aliasedModule, aliasedType) | ||
end | end | ||
| − | return | + | return typeTable[form] or defaultTable[form] or '' |
else | else | ||
return '' | return '' | ||
| Line 94: | Line 109: | ||
end | end | ||
| − | function p.parser(passedArgs, form | + | function p.parser(passedArgs, form) |
local args = {state = passedArgs.state, type = passedArgs.type, route = passedArgs.route, | local args = {state = passedArgs.state, type = passedArgs.type, route = passedArgs.route, | ||
denom = passedArgs.denom, county = passedArgs.county, dab = passedArgs.dab, | denom = passedArgs.denom, county = passedArgs.county, dab = passedArgs.dab, | ||
country = passedArgs.country, township = passedArgs.township} | country = passedArgs.country, township = passedArgs.township} | ||
| − | local formatStr = formatString(args, form | + | local formatStr = formatString(args, form) |
if not formatStr or formatStr == '' then return nil end | if not formatStr or formatStr == '' then return nil end | ||
| − | + | return parser(formatStr, args, form) | |
| − | |||
end | end | ||
return p | return p | ||
Revision as of 02:49, 4 August 2014
Documentation for this module may be created at Module:Road data/parser/doc
local p = {}
local format = string.format
local gsub = mw.ustring.gsub
local trim = mw.text.trim
local upper = mw.ustring.upper
local prepattern = "%[(%w+)%|(.*)%|(.*)%|(.*)%]"
local pattern = "%%(%w+)%%"
local function parser(formatStr, args, form)
local function ifexists(name)
if name == '' then return false end
local title
if form == 'shield' then
title = mw.title.new(name, 'Media')
else
title = mw.title.new(name, 0)
end
return title.exists
end
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
local formatTable = {}
-- Recursively dig into tables that could be parser hooks or argument tables.
local function formatStrInTable(formatStr)
if type(formatStr) ~= "table" then return formatStr end
formatTable = formatStr
local hook = formatStr.hook
local both = formatStr[2]
if both then
local first = formatStrInTable(formatStr[1])
local second = formatStrInTable(both)
return {first, second}
elseif hook then
local hooksModule = require "Module:Road data/parser/hooks"
local hookFunction = hooksModule[hook] or error("Hook '" .. hook .. "' does not exist", 0)
return formatStrInTable(hookFunction(formatStr, args))
else
local arg = args[formatStr.arg or "route"]
return formatStrInTable(formatStr[arg] or formatStr.default)
end
end
local function parse(formatStr)
local preprocessed = gsub(formatStr, prepattern, testArgs)
local parsedStr = gsub(preprocessed, pattern, args)
local final = trim(parsedStr)
if formatTable.ifexists then
local exists = ifexists(final)
if exists then
return final
else
return parser(formatTable.otherwise, args, form)
end
end
return final
end
formatStr = formatStrInTable(formatStr)
if not formatStr or formatStr == '' then return '' end
if type(formatStr) == 'table' then
local first = parse(formatStr[1])
local second = parse(formatStr[2])
return first, second
else
return parse(formatStr)
end
end
local function formatString(args, form)
local function getTypeData(module, type)
local success, moduleData = pcall(mw.loadData, module)
if not success then return '' end
local typeTable = moduleData[type] or moduleData['']
local defaultTable = moduleData[''] or {}
if typeTable then
local alias = typeTable.alias
if alias then
local aliasedModule = "Module:Road data/strings/" .. alias.module
local aliasedType = alias.type
return getTypeData(aliasedModule, aliasedType)
end
return typeTable[form] or defaultTable[form] or ''
else
return ''
end
end
local stateCountries = {USA = true, CAN = true}
local state = upper(args.state or '')
local country
if args.country then
country = upper(args.country)
else
local countryModule = mw.loadData("Module:Road data/countrymask")
country = countryModule[state] or 'UNK'
end
local typeArg = args.type
local module
if stateCountries[country] and state ~= '' then
module = format("Module:Road data/strings/%s/%s", country, state)
else
module = format("Module:Road data/strings/%s", country)
end
return getTypeData(module, typeArg)
end
function p.parser(passedArgs, form)
local args = {state = passedArgs.state, type = passedArgs.type, route = passedArgs.route,
denom = passedArgs.denom, county = passedArgs.county, dab = passedArgs.dab,
country = passedArgs.country, township = passedArgs.township}
local formatStr = formatString(args, form)
if not formatStr or formatStr == '' then return nil end
return parser(formatStr, args, form)
end
return p