Module:Jct/shields/USA

From Wikipedia

Documentation for this module may be created at Module:Jct/shields/USA/doc

local p = {}

local format = string.format

local shieldDefs = require "Module:Highway shields/defs"
local Country = shieldDefs.Country
local Shield = shieldDefs.Shield
local Type = shieldDefs.Type
local UniqueType = shieldDefs.UniqueType
local Alias = shieldDefs.Alias
local ShieldPreprocessors = shieldDefs.ShieldPreprocessors

do -- Preprocessors
    do -- State name
        local statesModule = require "Module:U.S. States"
        local mapping = statesModule.stateAbbrToName
        function ShieldPreprocessors.stateName(args)
            args.stateName = mapping[args.state]
        end
    end -- State name
    do -- Ifexist
        function ShieldPreprocessors.ifexist(shield)
            local ifexistArgs = shield.ifexist
            local try = ifexistArgs.try
            local otherwise = ifexistArgs.otherwise
            local formatArgs = ifexistArgs.args
            return function (args)
                local formatArguments = {}
                for i,v in ipairs(formatArgs) do
                    formatArguments[i] = args[v]
                end
                local tryStr = format(try, unpack(formatArguments))
                local title = mw.title.new("Media:" .. tryStr)
                if title.exists then
                    args.ifexist = tryStr
                else
                    args.ifexist = format(otherwise, unpack(formatArguments))
                end
            end
        end
    end -- Ifexist
end -- Preprocessors

local shields = Country:new()

do -- Definitions
    -- BEGIN: Function definitions
    
    local function stateRoute(state, shield, types)
        local stateType = shields:uniqueType(state)
        stateType:update{state = state, shield = shield}
        for i,typeStr in ipairs(types) do
            shields:type(typeStr):addShield(state, shield)
        end
    end
    
    -- END: Function definitions
    -- BEGIN: Type definitions
    
    do -- Interstates
        local type = Type:new{formatStr = "I-%s.svg"}
        shields:addType("I", type)
        shields:addType("Interstate", type)
        shields:addType("I-Spur", type)
        
        local function defStates(states)
            for i,state in ipairs(states) do
                type:addShield(state, Shield:new{formatStr = string.format("I-%s (%s).svg", "%s", state)})
            end
        end
        local defaultStateSpecific = {"AL", "AZ", "CA", "IA", "KS", "MO", "NV", "NM", "OK", "SC", "WV"}
        defStates(defaultStateSpecific)
        
        type:addShield("AR", Shield:new{formatStr = "I-%s (AR) Metric.svg"}) -- Arkansas
        type:addShield("HI", Shield:new{formatStr = "I-H%s.svg"}) -- Hawaii
        type:addShield("PR", Shield:new{formatStr = "I-PR%s.svg"}) -- Puerto Rico
        
        do -- New York
            local exceptions = {["90N"] = "I-90N.svg"}
            local by100 = {less = "I-%s.svg", more = "I-%s (long).svg"}
            type:addShield("NY", Shield:new{exceptions = exceptions, by100 = by100})
        end -- New York
        
        do -- Big shield
            local big = Shield:new{formatStr = "I-%s (big).svg"}
            type:addShield("MT", big) -- Montana
            type:addShield("OR", big) -- Oregon
            type:addShield("UT", big) -- Utah
            type:addShield("WA", big) -- Washington
        end -- Big shield
    end -- Interstates
    
    do -- Future Interstates
        local ifexist = ShieldPreprocessors.ifexist
        local shield = Shield:new{formatStr = "%s", formatArgs = {"ifexist"}, ifexist = {try = "I-%s (Future).svg", otherwise = "I-%s.svg", args = {"route"}}}
        shield.preprocessors = {ifexist(shield)}
        shields:addType("Future", shield)
    end -- Future Interstates
    
    do -- State routes
        do -- California
            local shield = Shield:new{formatStr = "California %s.svg"}
            stateRoute("CA", shield, {"SR"})
        end -- California
    end -- State routes
    
end -- Definitions

p.shields = shields
return p