There are no reviewed versions of this page, so it may not have been checked for adherence to standards.

Documentation for this module may be created at Module:Infobox road/name/USA/doc

local p = {}

local format = string.format

local typesModule = require "Module:Infobox road/name/defs"
Country = typesModule.Country
Type = typesModule.Type
Name = typesModule.Name

Preprocessors = {}

function Preprocessors.subtype(subtypes, formatStr)
    local module = require "Module:Infobox road/meta/mask/subtype2"
    local subtypeFunc = module._subtype
    return function (args)
        local subtype = args.subtype
        local subtypeStr = subtype and (subtypeFunc(subtype, subtypes) or '') or ''
        args.maskedSubtype = (subtypeStr ~= '') and format(formatStr, subtypeStr) or ''
    end
end

do
    local statesModule = require "Module:U.S. States"
    local mapping = statesModule.stateAbbrToName
    function Preprocessors.stateName(args)
        args.stateName = mapping[args.state]
    end
end

local names = Country:new()
do -- Name definitions
    function names:typeOverride(args)
        local type = args.type
        local state = args.state
        if not(state) then return end
        local stateVarTypes = {"%s", "%s-old", "%s 1926", "%s 1948", "%s 1955"}
        local typesToCheck = {}
        for i,v in ipairs(stateVarTypes) do
            local type = format(v, state)
            typesToCheck[type] = true
        end
        if typesToCheck[type] then
            return self["SR"]
        else
            local ISR = format("I%s", state)
            if type == ISR then
                return self["ISR"]
            else
                return nil
            end
        end
    end
    
    local subtype = Preprocessors.subtype
    do -- Interstates
        local type = Type:new{formatStr = "Interstate %s"}
        names["I"] = type
        names["Interstate"] = type
        names["Future"] = type
        names["I 1957"] = type
        do -- Hawaii
            type["HI"] = Name:new{formatStr = "Interstate H-%s"}
        end -- Hawaii
        do -- Puerto Rico
            type["PR"] = Name:new{formatStr = "Interstate PR%s"}
        end -- Puerto Rico
    end -- Interstates
    
    do -- Business Loops
        local type = Type:new{formatStr = "Interstate %s Business"}
        names["BL"] = type
        names["BL 1957"] = type
        do -- Michigan
            type["MI"] = Name:new{formatStr = "Business Loop Interstate %s", exceptions = {["496"] = "Capitol Loop"}}
        end -- Michigan
    end -- Business Loops
    
    do -- Business Spurs
        local type = Type:new{formatStr = "Interstate %s Business"}
        names["BS"] = type
        names["BS 1957"] = type
        do -- Michigan
            type["MI"] = Name:new{formatStr = "Business Spur Interstate %s"}
        end -- Michigan
    end -- Business Spurs
    
    do -- U.S. Routes
        local type = Type:new{formatStr = "U.S. Route %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
        names["US"] = type
        names["US 1926"] = type
        names["US 1948"] = type
        names["US 1950"] = type
        names["US 1961"] = type
        do -- Arkansas
            local subtype = subtype({Bus = "B", Spur = "S", Truck = "T"}, "%s")
            type["AR"] = Name:new{formatStr = "U.S. Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype}}
        end -- Arkansas
        do -- U.S. Highway
            local name = Name:new{formatStr = "U.S. Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            type["FL"] = name
            type["GA"] = name
            type["KY"] = name
            type["LA"] = name
            type["MN"] = name
            type["NE"] = name
            type["WI"] = name
        end -- U.S. Highway
        do -- Michigan, Texas
            local name = Name:new{formatStr = "%sUS Highway %s", formatArgs = {"maskedSubtype", "route"}, preprocessors = {subtype({}, "%s ")}}
            type["MI"] = name
            type["TX"] = name
        end -- Michigan, Texas
    end -- U.S. Routes
    
    do -- New England Routes
        names["NER"] = Name:new{formatStr = "New England Route %s"}
    end -- New England Routes
    
    do -- State highways
        local type = Type:new{formatStr = "%s Route %s%s", formatArgs = {"stateName", "route", "maskedSubtype"}, preprocessors = {Preprocessors.stateName, subtype({}, " %s")}}
        do
            local types = {"SH", "SR", "Route", "M", "K", "U", "SH-old", "SR-old", "Route-old", "LA 1990", "M-old", "M 1919", "M 1926", "M 1948",
                           "MN 1950", "NJ 1956", "NY 1927", "NY 1960", "NC 1945", "NC 1957", "Toll", "Both", "Supp", "Town", "SRCR", "BLSR", "2state"}
            for i,v in ipairs(types) do
                names[v] = type
            end
        end
        do -- "State Route"
            local states = {"AL", "CA", "GA", "ME", "NV", "OH", "TN", "UT", "VA", "WA"}
            local name = Name:new{formatStr = "State Route %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "State Route"
        do -- "State Highway"
            local states = {"CO", "ID", "OK", "TX"}
            local name = Name:new{formatStr = "State Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "State Highway"
        do -- "Route"
            local states = {"CT", "HI", "MA", "MO", "NJ", "RI"}
            local name = Name:new{formatStr = "Route %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "Route"
        do -- "State Road"
            local states = {"FL", "IN", "NM"}
            local name = Name:new{formatStr = "State Road %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "State Road"
        do -- "Highway"
            local states = {"PR", "SD", "VI"}
            local name = Name:new{formatStr = "Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "Highway"
        do -- "<state> Highway"
            local states = {"AS", "GU", "IA", "LA", "MT", "NE", "ND", "WY"}
            local name = Name:new{formatStr = "%s Highway %s%s", formatArgs = {"stateName", "route", "maskedSubtype"}, preprocessors = {Preprocessors.stateName, subtype({}, " %s")}}
            for i,v in ipairs(states) do
                type[v] = name
            end
        end -- "<state> Highway"
        do -- Arizona
            local exceptions = {["101"] = "State Loop 101", ["202"] = "State Loop 202", ["303"] = "State Loop 303"}
            type["AZ"] = Name:new{formatStr = "State Route %s%s", exceptions = exceptions, formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
        end -- Arizona
        do -- Carolinas
            local name = Name:new{formatStr = "%s %s%s", formatArgs = {"state", "route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
            type["NC"] = name
            type["SC"] = name
        end -- Carolinas
        do -- Illinois
            local exceptions = {["Elgin–O'Hare Expressway"] = "Elgin–O'Hare Expressway"}
            type["IL"] = Name:new{formatStr = "Illinois Route %s", exceptions = exceptions}
        end -- Illinois
        do -- Michigan
            local subtype = subtype({Alt = "ALT", Bus = "BUS", Conn = "CONN", Truck = "TRUCK"}, "%s ")
            type["MI"] = Name:new{formatStr = "%sM-%s", formatArgs = {"maskedSubtype", "route"}, preprocessors = {subtype}}
        end -- Michigan
        do -- Split types
            local function processTypeExceptions(exceptions, default, formatArgs)
                return function (args)
                    local formatArguments = {}
                    for i,v in ipairs(formatArgs) do
                        formatArguments[i] = args[v]
                    end
                    local type = args.type
                    local formatStr = exceptions[type] or default
                    args.override = format(formatStr, unpack(formatArguments))
                end
            end
            do -- Arkansas
                local subtype = subtype({Bus = "B", Spur = "S", Truck = "T"}, "%s")
                local typeExceptions = {["AR 1926"] = "State Road %s%s"}
                local preprocessor = processTypeExceptions(typeExceptions, "Highway %s%s", {"route", "maskedSubtype"})
                type["AR"] = Name:new{formatStr = "%s", formatArgs = {"override"}, preprocessors = {subtype, preprocessor}}
            end -- Arkansas
            do -- Pennsylvania
                local typeExceptions = {["Toll"] = "Penna. Tpk. %s%s"}
                local preprocessor = processTypeExceptions(typeExceptions, "PA Route %s%s", {"route", "maskedSubtype"})
                type["PA"] = Name:new{formatStr = "%s", formatArgs = {"override"}, preprocessors = {subtype({}, " %s"), preprocessor}}
            end -- Pennsylvania
        end -- Split types
        type["KS"] = Name:new{formatStr = "K-%s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}} -- Kansas
        type["MN"] = Name:new{formatStr = "Trunk Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}} -- Minnesota
        type["MS"] = Name:new{formatStr = "MS Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}} -- Mississippi
        type["NY"] = Name:new{formatStr = "NYS Route %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}} -- New York
        type["WI"] = Name:new{formatStr = "State Trunk Highway %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}} -- Wisconsin
    end -- State highways
    
    do -- Secondary
        local type = Type:new()
        names["Sec"] = type
        names["Secondary"] = type
        do -- Tennessee, Virginia
            local name = Name:new{formatStr = "State Route %s"}
            type["TN"] = name
            type["VA"] = name
        end -- Tennessee, Virginia
        type["MT"] = Name:new{formatStr = "Secondary Highway %s"}
        type["PR"] = Name:new{formatStr = "Highway %s"}
    end -- Secondary
    
    do -- Tertiary
        local name = Name:new{formatStr = "Highway %s"}
        names["Ter"] = name
        names["Tertiary"] = name
    end -- Tertiary
    
    do -- Dual
        local type = Type:new()
        names["Dual"] = type
        type["TN"] = Name:new{formatStr = "State Route %s"}
        type["VT"] = Name:new{formatStr = "Vermont Route %s"}
    end -- Dual
    
    do -- County routes
        local function oldSubtype(args)
            local subtype = args.subtype
            args.maskedOld = (subtype == "Old") and "Old " or ""
        end
        local formatArgs = {"maskedOld", "route", "maskedSubtype"}
        local subtype = subtype({Old = ''}, " %s")
        local preprocessors = {oldSubtype, subtype}
        local type = Type:new{formatStr = "%sCounty Road %s%s", formatArgs = formatArgs, preprocessors = preprocessors}
        names["CR"] = type
        do -- Louisiana
            type["LA"] = Name:new{formatStr = "%sParish Road %s%s", formatArgs = formatArgs, preprocessors = preprocessors}
        end -- Louisiana
        do -- California, New Jersey, New York
            local name = Name:new{formatStr = "%sCounty Route %s%s", formatArgs = formatArgs, preprocessors = preprocessors}
            type["CA"] = name
            type["NJ"] = name
            type["NY"] = name
        end -- California, New Jersey, New York
    end -- County routes
    
    do -- County Designated Highways (Michigan)
        names["CDH"] = Name:new{formatStr = "%s"}
    end -- County Designated Highways (Michigan)
    
    do -- County State-Aid Highways (Minnesota)
        names["CSAH"] = Name:new{formatStr = "County State-Aid Highway %s"}
    end -- County State-Aid Highways (Minnesota)
    
    do -- Forest Highways
        local type = Type:new{formatStr = "Forest Highway %s"}
        names["FR"] = type
        names["FH"] = type
        names["FFH"] = type
        do -- Michigan
            type["MI"] = Name:new{formatStr = "Federal Forest Highway %s"}
        end -- Michigan
    end -- Forest Highways
    
    do -- Parkways
        local type = Type:new()
        names["Parkway"] = type
        do --Kentucky
            local exceptions = {["Mountain"] = "Bert T. Combs Mountain Parkway", ["Natcher"] = "William H. Natcher Parkway"}
            type["KY"] = Name:new{formatStr = "%s Parkway", exceptions = exceptions}
        end -- Kentucky
        do -- New York
            local exceptions = {}
            do -- Exceptions
                exceptions["Arden Valley"] = "Arden Valley Road"
                exceptions["Tiorati Brook"] = "Tiorati Brook Road"
                exceptions["FDR"] = "Franklin D. Roosevelt East River Drive"
                exceptions["Harlem River"] = "Harlem River Drive"
                exceptions["Seven Lakes"] = "Seven Lakes Drive"
                exceptions["Palisades"] = "Palisades Interstate Parkway"
                exceptions["Saw Mill"] = "Saw Mill River Parkway"
                exceptions["Robert Moses Cswy"] = "Robert Moses Causeway"
                exceptions["Ponquogue"] = "Ponquogue Causeway"
                local stateParkways = {"Northern", "Southern", "Taconic", "Bear Mountain", "Lake Ontario", "Sagtikos", "Meadowbrook",
                                       "Bethpage", "Wantagh", "Heckscher", "Sunken Meadow", "Caumsett", "Robert Moses"}
                for i,v in ipairs(stateParkways) do
                    exceptions[v] = format("%s State Parkway", v)
                end
            end -- Exceptions
            type["NY"] = Name:new{formatStr = "%s Parkway", exceptions = exceptions}
        end -- New York
    end -- Parkways
    
    do -- 1924 Louisiana State Routes
        names["LA 1924"] = Name:new{formatStr = "State Route %s"}
    end -- 1924 Louisiana State Routes
    
    do -- Farm to Market Roads (Texas)
        local name = Name:new{formatStr = "Farm to Market Road %s%s", formatArgs = {"route", "maskedSubtype"}, preprocessors = {subtype({}, " %s")}}
        names["Farm"] = name
        names["FM"] = name
    end -- Farm to Market Roads (Texas)
    
    do -- Links (Nebraska)
        names["Link"] = Name:new{formatStr = "Link %s"}
    end -- Links (Nebraska)
    
    do -- Loops (Texas)
        local name = Name:new{formatStr = "State Highway Loop %s", exceptions = {["8"] = "State Highway Beltway 8"}}
        names["Loop"] = name
        names["Toll Loop"] = name
        names["BothLoop"] = name
    end -- Loops (Texas)
    
    do -- Park Roads (Texas)
        names["Park"] = Name:new{formatStr = "Park Road %s"}
    end -- Park Roads (Texas)
    
    do -- Ranch to Market Roads (Texas)
        local name = Name:new{formatStr = "Ranch to Market Road %s", exceptions = {["1"] = "Ranch Road %s"}}
        names["Ranch"] = name
        names["RM"] = name
    end -- Ranch to Market Roads (Texas)
    
    do -- Recreational Roads (Texas)
        local name = Name:new{formatStr = "Recreational Road %s"}
        names["Rec"] = name
        names["RE"] = name
    end -- Recreational Roads (Texas)
    
    do -- Spurs
        local type = Type:new()
        names["Spur"] = type
        type["NE"] = Name:new{formatStr = "Spur %s"} -- Nebraska
        type["TX"] = Name:new{formatStr = "State Highway Spur %s"} -- Texas
    end -- Spurs
    
    do -- Turnpikes
        local type = Type:new{formatStr = "%s Turnpike", formatArgs = {"stateName"}, preprocessors = {Preprocessors.stateName}}
        names["Turnpike"] = type
        type["CT"] = Name:new{formatStr = "Gov. John Davis Lodge Turnpike", formatArgs = {}}
        type["FL"] = Name:new{formatStr = "Florida's Turnpike", formatArgs = {}, exceptions = {["HEFT"] = "Homestead Extension<br>of Florida's Turnpike"}}
        type["NH"] = Name:new{formatStr = "%s Turnpike"}
    end -- Turnpikes
    
    do -- Interstate and State Route combo
        local function stateRoute(args)
            local state = args.state
            local type = names:type{type = state, state = state}
            args.stateRoute = type:name(args)
        end
        local name = Name:new{formatStr = "Interstate %s and %s", formatArgs = {"route", "stateRoute"}, preprocessors = {stateRoute}}
        names["ISR"] = name
        names["ISH"] = name
    end -- Interstate and State Route combo
    
    do -- Primary State Highways (Washington)
        names["PSH"] = Name:new{formatStr = "Primary State Highway %s"}
    end -- Primary State Highways (Washington)
    
    do -- Secondary State Highways (Washington)
        names["SSH"] = Name:new{formatStr = "Secondary State Highway %s"}
    end -- Secondary State Highways (Washington)
    
    do -- Urban Primary Highways (Puerto Rico)
        local name = Name:new{formatStr = "Highway %s"}
        names["Urban"] = name
        names["Urban primary"] = name
    end -- Urban Primary Highways (Puerto Rico)
    
    do -- Great Lakes Circle Tours
        local default = {formatStr = "Great Lakes Circle Tour", formatArgs = {}}
        local type = Type:new(default)
        names["Tour"] = type
        do -- Lake Superior
            local name = Name:new{formatStr = "Lake Superior Circle Tour", formatArgs = {}}
            type["LSCT"] = name
            type["LSCT-Spur"] = name
        end -- Lake Superior
        do -- Lake Michigan
            local name = Name:new{formatStr = "Lake Michigan Circle Tour", formatArgs = {}}
            type["LMCT"] = name
            type["LMCT-Spur"] = name
        end -- Lake Michigan
        type["LHCT"] = Name:new{formatStr = "Lake Huron Circle Tour", formatArgs = {}}
        type["LECT"] = Name:new{formatStr = "Lake Erie Circle Tour", formatArgs = {}}
        type["GLCT"] = Name:new(default)
    end -- Great Lakes Circle Tours
    
    do -- Maryland Scenic Byways
        local type = Type:new()
        names["SB"] = type
        local exceptions = {["1"] = "&#35;1: Historic National Road", ["2"] = "&#35;2: Mountain Maryland", ["3"] = "&#35;3: Chesapeake and Ohio Canal",
                            ["4"] = "&#35;4: Antietam Campaign", ["5"] = "&#35;5: Catoctin Mountain", ["6"] = "&#35;6: Old Main Streets",
                            ["7"] = "&#35;7: Mason and Dixon", ["8"] = "&#35;8: Falls Road", ["9"] = "&#35;9: Horses and Hounds",
                            ["10"] = "&#35;10: Lower Susquehanna", ["11"] = "&#35;11: Charles Street", ["12"] = "&#35;12: National Historic Seaport",
                            ["13"] = "&#35;13: Star-Spangled Banner", ["14"] = "&#35;14: Booth's Escape", ["15"] = "&#35;15: Roots and Tides",
                            ["16"] = "&#35;16: Religious Freedom Tour", ["17"] = "&#35;17: Chesapeake Country",
                            ["18"] = "&#35;18: Harriet Tubman Underground Railroad", ["19"] = "&#35;19: Blue Crab"}
        type["MD"] = Name:new{formatStr = '', formatArgs = {}, exceptions = exceptions}
    end -- Maryland Scenic Byways
end -- Name definitions

p.names = names
return p