Module:Jctint/USA
Appearance
![]() | This Lua module is used on approximately 12,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 modules implements the US region specific Jctint templates.
local p = {}
local format = mw.ustring.format
local concat = table.concat
local insert = table.insert
local roadDataModule = require("Module:Road data")
-- TODO transition
-- begin transition code
local region_special = {
GA = "[[Georgia (U.S. state)|Georgia]]",
NY = "[[New York (state)|New York]]",
WA = "[[Washington (state)|Washington]]"
}
local indep_city = {
CA = {
["San Francisco"] = {
prefix = "City and County of "
}
},
CO = {
["Denver"] = {
prefix = "City and County of "
},
default = {
prefix = "City and County of ",
linksuffix = ", Colorado"
}
},
MD = {
["Baltimore"] = {
namesuffix = " City"
}
}
}
local sub1Config = {
LA = "parish"
}
local sub1name = {
LA = "Parish"
}
local sub1span = {
LA = {"cspan", "pspan"}
}
local subConfig = {
county = {
group = "county"
},
township = {
sub2area = "township",
group = "township",
single = true,
firstnonum = true
},
parish = {
group = "parish"
}
}
local function sub_special(args, subTypeConfig, specialPrefix, subType)
-- Find parameter.
local subParam
for _,p in ipairs(subTypeConfig) do
if args[p .. 1] or args[p] then
subParam = p
break
end
end
if not subParam then
return args[specialPrefix .. "_special"]
end
local config = subConfig[subParam] or {}
local subs = {}
-- Retrieve wikilinks for subdivisions.
local num = 1
while num == 1 or args[subParam .. num] do
-- Save parameters.
local saved = {}
saved[subType]= args[subType]
args[subType] = args[subParam .. num] or num == 1 and args[subParam]
saved.sub1dab = args.sub1dab
args.sub1dab = args["ctdab" .. num] or args.ctdab or num == 1 and config.firstnonum and args.county
saved.area = args.area
args.area = not (config.nosub1dab and args.sub1dab)
and (config.sub2area or args["area" .. num] or num == 1 and config.firstnonum and args.area)
local locns = roadDataModule.locations(args, "jctint", true)
-- Restore parameters.
args[subType] = saved[subType]
args.sub1dab = saved.sub1dab
args.area = saved.area
subs[num] = locns[subType]
num = num + 1
end
if #subs > 1 then
-- Construct wikitext for multiple subs.
local textParts = {}
insert(textParts, subs[1])
for i = 2, #subs do
insert(textParts, "–")
if i % 2 ~= 0 then
-- Odd subs after first begin a new line.
insert(textParts, "<br>")
end
insert(textParts, subs[i])
end
local groupSuffix = args[specialPrefix .. "_group"] or config.group
if groupSuffix then
insert(textParts,
format("%s%s", #subs % 2 == 0 and "<br>" or " ", groupSuffix))
end
if #subs == 2 then
insert(textParts, " line")
elseif #subs == 3 then
insert(textParts, " tripoint")
elseif #subs == 4 then
insert(textParts, " quadripoint")
else
insert(textParts, " [[Quadripoint#Multipoints of greater numerical complexity|multipoint]]")
end
return concat(textParts)
elseif #subs == 1 and config.single then
-- TODO transition
-- Save parameters.
local saved = {}
saved[subType]= args[subType]
args[subType] = args[subParam .. 1] or args[subParam]
saved.sub1dab = args.sub1dab
args.sub1dab = args.ctdab1 or args.ctdab or config.firstnonum and args.county
saved.area = args.area
args.area = not (config.nosub1dab and args.sub1dab)
and (config.sub2area or args.area1 or config.firstnonum and args.area)
local locns = roadDataModule.locations(args, "jctint")
-- Restore parameters.
args[subType] = saved[subType]
args.sub1dab = saved.sub1dab
args.area = saved.area
return locns[subType]
end
end
-- end transition code
function p._jctint(args)
-- Remove parameters already used in the template definition
if args.used_params then
for param in mw.text.gsplit(args.used_params, ",") do
args[param] = nil
end
args.used_params = nil
end
-- Track used arguments
local origArgs = args
args = {}
local mt = {
__index = function(t, k)
local result = origArgs[k]
origArgs[k] = nil
t[k] = result
return result
end
}
setmetatable(args, mt)
-- Extra parameters
args.country = "USA"
-- TODO transition
-- begin transition code
args.primary_topic = "no"
args.sub1name = sub1name[args.state] or "County"
args.region_special = region_special[args.state]
args.region = mw.loadData("Module:Jct/statename")[args.state]
-- Independent city
local indepCityText
if args.indep_city_special then
indepCityText = args.indep_city_special -- Overrides `indep_city` argument.
elseif args.indep_city then
local indepCity = args.indep_city
local spec = indep_city[args.state] and
(indep_city[args.state][indepCity] or indep_city[args.state].default)
if spec then
local link = format("%s%s%s",
spec.linkprefix or "", indepCity, spec.linksuffix or "")
local name = format("%s%s%s",
spec.nameprefix or "", indepCity, spec.namesuffix or "")
indepCityText = format("%s[[%s|%s]]",
spec.prefix or "", link, name)
else
-- Specialize independent city to the region.
local cityLink = format('[[%s, %s|%s]]', indepCity, args.region, indepCity)
indepCityText = "[[Independent city (United States)|City]] of " .. cityLink
end
args.indep_city = nil
end
local sub1config = sub1Config[args.state] or "county"
args.sub1_special = sub_special(args, {sub1config}, sub1config, "sub1")
local sub2Config = {}
if args.sub2param then
insert(sub2Config, args.sub2param)
end
insert(sub2Config, "location")
args.sub2_special = sub_special(args, sub2Config, "location", "sub2")
-- end transition code
local lengthUnit = args.unitdef or "mile"
args.unitdef = args.unitdef or "mi"
-- Tracked parameters
local msgs = {}
if args.ctdab2 and args[sub1config] and not (args.ctdab or args.ctdab1) then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "D"))
elseif args.township and args[sub1config] and not args.ctdab then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "D"))
end
if args.township2 and args.township then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "T"))
end
if args.type == "mplex" then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "M"))
end
if args.location == " " or args.altunit == " " then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "B"))
end
if args.cspan == "1" or args.lspan == "1" or args.xcolspan == "1" then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "S"))
end
local trackedParams = {
R = "length_ref",
X = "indep_city_special",
Y = sub1config .. "_special",
Z = "location_special"
}
for key,param in pairs(trackedParams) do
if args[param] then
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", key))
end
end
-- TODO transition
args.indep_city_special = indepCityText
-- Crossing
if args.xing then
local colType
local colAlignType
if args.xcolspan == "3" then
colType = "unitary"
colAlignType = "unitary_align"
elseif args.xcolspan == "2" then
colType = "indep_city_special"
colAlignType = "indep_city_align"
else
colType = "sub2_special"
colAlignType = "sub2_align"
end
args[colType] = args.xing
args[colAlignType] = "center"
args.sub2span = args.xspan
end
-- Parameters to be renamed
local paramSubst = {
region_special = "state_special",
regionspan = "sspan",
sub1 = sub1config,
sub1_note = sub1config .. "_note",
sub1span = sub1span[args.state] or "cspan",
sub1dab = "ctdab",
sub2 = "location",
sub2span = "lspan",
unit = lengthUnit,
unit2 = lengthUnit .. "2",
unit_ref = {lengthUnit .. "_ref", "length_ref" --[[TODO transition]]},
uspan = "mspan",
place = {"place", "bridge", "tunnel"}
}
-- Rename parameters.
for tgt,src in pairs(paramSubst) do
if type(src) == "table" then
for _,param in ipairs(src) do
args[tgt] = args[tgt] or args[param]
if param ~= tgt then args[param] = nil end
end
else
args[tgt] = args[tgt] or args[src]
args[src] = nil
end
end
-- Parameters to be removed
local nilParams = {
sub1config .. "_special", "location_special",
"xing", "xcolspan", "xspan"
}
-- Remove parameters.
for _,param in ipairs(nilParams) do
args[param] = nil
end
-- Parameter groups to be removed
local nilParamPrefixes = {
sub1config, "location", "township"
}
-- Remove parameters.
for _,paramPrefix in ipairs(nilParamPrefixes) do
args[paramPrefix] = nil
local i = 1
while i == 1 or args[paramPrefix .. i] do
args[paramPrefix .. i] = nil
i = i + 1
end
end
local coreModule = require("Module:Jctint/core")
local coreResult = coreModule._jctint(args)
-- Report unused arguments
local unusedArgs = {}
for key,_ in pairs(origArgs) do
insert(unusedArgs, key)
end
if #unusedArgs > 0 then
local msg = format('<tr style="display: none;"><td>Module:Jctint/USA warning: Unused argument(s): %s</td></tr>', concat(unusedArgs, ", "))
insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]%s", "U", msg))
end
local msg
if #msgs > 0 then
local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
msg = mw.ustring.gsub(concat(msgs), "%%page%%", page)
end
return coreResult .. (msg or "")
end
function p.jctint(frame)
-- Import module function to work with passed arguments
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return p._jctint(args)
end
return p