Difference between revisions of "Module:Jcon"
Jump to navigation
Jump to search
blackwiki>BrandonXLF |
blackwiki>Anne drew Andrew and Drew (update per [https://en.wikipedia.org/wiki/Template_talk:Jcon#Edit_Request_December_1,_2018 talk page request]) |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local getArgs = require("Module:Arguments").getArgs | local getArgs = require("Module:Arguments").getArgs | ||
| − | local data = mw.loadData('Module:Jcon/data') | + | local data = mw.loadData('Module:Jcon/data') |
local miscTable = data.miscTable | local miscTable = data.miscTable | ||
| − | local typeTable | + | local typeTable = data.typeTable |
| − | local | + | local divisionTable = data.divisionTable |
local pictureTable = data.pictureTable | local pictureTable = data.pictureTable | ||
--[[ | --[[ | ||
| − | R E T U R N P I C T U | + | R E T U R N P I C T U R E |
Return the picture of text | Return the picture of text | ||
]]-- | ]]-- | ||
| − | local function | + | local function returnPicture(frame,Type,args,division) |
| − | + | if not pictureTable[division] then -- Return nothing if region doen't have pictures | |
| − | if not pictureTable[division] then | ||
return "" | return "" | ||
end | end | ||
| − | local | + | local export = pictureTable[division][1]..args[2]..pictureTable[division][2] or "" -- Set picture |
| − | if args[2] == "52" and division == "Simcoe" then | + | |
| + | if args[2] == "52" and division == "Simcoe" then -- Picture exceptions | ||
export = "Simcoe county road 52.png" | export = "Simcoe county road 52.png" | ||
elseif args[2] == "407ETR" and division == "Highway" then | elseif args[2] == "407ETR" and division == "Highway" then | ||
| Line 25: | Line 25: | ||
elseif args[2] == "QEW" and division == "Highway" then | elseif args[2] == "QEW" and division == "Highway" then | ||
export = "Ontario QEW.svg" | export = "Ontario QEW.svg" | ||
| − | + | else | |
| − | + | routenumber = string.gsub(args[2],"%D","") | |
| − | + | if tonumber(routenumber) == nil then | |
| − | + | return "" -- Return nothing if there's no route number | |
| + | elseif tonumber(routenumber) >= 500 and division == "Highway" then -- If highway is secondary | ||
| + | export = "Ontario Highway "..args[2]..".svg" | ||
| + | end | ||
end | end | ||
| − | if frame:callParserFunction('#ifexist', 'Media:' .. export, '1') ~= '' then | + | if export == "" or nil then return "" end -- Return nothing if export is nothing |
| − | if args["size"] then | + | if frame:callParserFunction('#ifexist', 'Media:' .. export, '1') ~= '' then -- Set picture sizes |
| + | if args["size"] then | ||
return table.concat({"[[File:",export,"|alt=|link=|",args["size"],"]]"}) | return table.concat({"[[File:",export,"|alt=|link=|",args["size"],"]]"}) | ||
| − | elseif | + | elseif args[2] == "407ETR" then |
| − | return table.concat({"[[File:",export,"|alt=|link=| | + | return table.concat({"[[File:",export,"|alt=|link=|24px]]"}) |
elseif division == "Kawartha Lakes" then | elseif division == "Kawartha Lakes" then | ||
| − | return table.concat({"[[File:",export,"|alt=|link=|21px]]"}) | + | return table.concat({"[[File:",export,"|alt=|link=|21px]]"}) |
else | else | ||
| − | return table.concat({"[[File:",export,"|alt=|link=| | + | return table.concat({"[[File:",export,"|alt=|link=|x20px]]"}) |
end | end | ||
else | else | ||
| − | return "" | + | return "" -- Retrun nothing if the picture doesn't exist |
end | end | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| Line 65: | Line 54: | ||
]]-- | ]]-- | ||
| − | local function returnText(frame,Type,args,division) | + | local function returnText(frame,Type,args,division,showred) |
| − | local export; | + | local export; -- Link |
| − | local | + | local display; -- Display (if different from link) |
| − | + | if args[2] == "407ETR" then -- Exception | |
| − | + | export = "Ontario Highway".." ".."407" | |
| − | if args[2] == "407ETR" then -- | + | display = "Highway".." ".."407" |
| − | export = "Ontario Highway".." | + | elseif args[2] == "QEW" then -- Exception |
| − | |||
| − | elseif args[2] == "QEW" then -- | ||
export = "Queen Elizabeth Way" | export = "Queen Elizabeth Way" | ||
| − | elseif Type == "Highway" then | + | elseif Type == "Highway" then -- Highways |
| − | export = "Ontario Highway".." | + | export = "Ontario Highway".." "..args[2] |
| − | + | display = "Highway".." "..args[2] | |
| − | elseif | + | elseif division == "Kawartha Lakes" then |
| − | export = | + | export ="Kawartha Lakes" .." ".."Road".." "..args[2] |
| − | + | display = Type.." ".."Road".." "..args[2] | |
| − | + | elseif Type == "County Highway" or Type == "Regional Highway" then | |
| − | + | export = division.." "..args[2] | |
| − | + | display = Type.." "..args[2] | |
| − | + | else | |
| − | + | export = division.." "..Type.." ".."Road".." "..args[2] | |
| − | + | display = Type.." ".."Road".." "..args[2] | |
| − | |||
| − | |||
| − | export = division.. | ||
| − | |||
end | end | ||
| − | if frame:callParserFunction('#ifexist',export, '1') ~= '' then | + | if (frame:callParserFunction('#ifexist',export, '1') ~= '' or showred) and not args["nolink"] then |
| − | if | + | if display then |
| − | export = "[["..export.."|".. | + | export = "[["..export.."|"..display.."]]" -- Show display |
else | else | ||
| − | export = "[["..export.."]]" | + | export = "[["..export.."]]" -- Show export |
end | end | ||
| − | + | ||
| − | + | return export | |
| − | + | elseif display then | |
| − | + | return display | |
| − | + | else | |
| − | + | return export | |
| − | |||
| − | export | ||
| − | elseif | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | else | ||
| − | export | ||
end | end | ||
| − | |||
| − | |||
| − | |||
end | end | ||
--[[ | --[[ | ||
| − | + | R E T U R N P L A C E | |
Add name/link for a city/town | Add name/link for a city/town | ||
]]-- | ]]-- | ||
| − | local function | + | local function returnPlace(frame,export,place,after,showred) |
local preExport | local preExport | ||
| − | if frame:callParserFunction('#ifexist',place..", Ontario", '1') ~= '' then | + | if frame:callParserFunction('#ifexist',place..", Ontario", '1') ~= '' or showred then |
preExport = "[["..place..", Ontario|"..place.."]]" | preExport = "[["..place..", Ontario|"..place.."]]" | ||
else | else | ||
| Line 148: | Line 116: | ||
function p.jcon (frame) | function p.jcon (frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
| − | local | + | args[1] = args[1] or "" -- Unnil args[1] |
| − | local division = | + | local Remove = {"regional","region","county","country", |
| + | "municipality of","city of","^ "," $"} -- Stuff to remove from lowercase input | ||
| + | for _,v in ipairs(Remove) do | ||
| + | args[1] = mw.ustring.gsub(args[1]:lower(),v,'') | ||
| + | end | ||
| + | args[2] = args[2] or "" | ||
| + | local division = divisionTable[args[1]:lower()] or args[1] | ||
| + | local Type = typeTable[division] | ||
local export = "" | local export = "" | ||
| − | |||
| − | |||
| − | |||
| − | |||
if miscTable[args[1]:lower()] or miscTable[args[2]:lower()] then | if miscTable[args[1]:lower()] or miscTable[args[2]:lower()] then | ||
return miscTable[args[1]:lower()] or miscTable[args[2]:lower()] | return miscTable[args[1]:lower()] or miscTable[args[2]:lower()] | ||
end | end | ||
| − | + | if not typeTable[division] then -- Region not in typeTable | |
| − | if not typeTable[ | ||
return "​" | return "​" | ||
end | end | ||
| − | + | if args[2] == "" then -- Need args[2] after this point | |
| − | if args[2] == "" then | ||
return "​" | return "​" | ||
end | end | ||
| − | + | if args["ot"] then -- If output should only be text | |
| − | if args["ot"] then | ||
args["nosh"] = "yes" | args["nosh"] = "yes" | ||
args["nolink"] = "yes" | args["nolink"] = "yes" | ||
end | end | ||
| − | + | args["2A"] = args[2] -- road1 is args[2] | |
| − | args["2A"] = args[2] | + | if args["con"] then args["2B"] = args["con"] end -- road 2 is args["con"] |
| − | if args["con"] then args["2B"] = args["con"] end | + | if args["con2"] then args["2C"] = args["con2"] end -- road 3 is args["con2"] |
| − | if args["con2"] then args["2C"] = args["con2"] end | + | -- [[ G E T P I C T U R E ]] -- |
| − | + | if not args["nosh"] then -- If allowed to add shield | |
| − | if not args["nosh"] then | + | args[2] = args["2A"] -- Set args[2] to road 1 |
| − | + | picture = returnPicture(frame,Type,args,division) -- Return picture of road 1 | |
| − | |||
| − | |||
if args["con"] then | if args["con"] then | ||
| − | args[2] = args["2B"] | + | args[2] = args["2B"] -- Set args[2] to road 2 |
| − | picture = picture.." "..returnPicture(frame,Type,args,division) | + | picture = picture.." "..returnPicture(frame,Type,args,division) -- Return picture of road 2 |
end | end | ||
| − | |||
if args["con2"] then | if args["con2"] then | ||
| − | args[2] = args["2C"] | + | args[2] = args["2C"] -- Set args[2] to road 3 |
| − | picture = picture.." "..returnPicture(frame,Type,args,division) | + | picture = picture.." "..returnPicture(frame,Type,args,division) -- Return picture of road 3 |
end | end | ||
| − | + | if picture ~= "" and not args["pic aft"] then -- If a picture was returned and picture goes first | |
| − | if picture ~= "" then | + | picture = picture.." " -- Add a space |
| − | picture = picture.." " | ||
end | end | ||
end | end | ||
| − | + | -- [[ A D D P I C T U R E ]] (If it goes before) -- | |
| − | if not args["pic aft"] and picture then | + | if not args["pic aft"] and picture then -- If picure goes first |
export = picture | export = picture | ||
end | end | ||
| − | + | -- [[ A D D T E X T ]] -- | |
| − | if not args["notext"] then | + | if not args["notext"] then -- If allowed to show text |
args[2] = args["2A"] | args[2] = args["2A"] | ||
| − | export = export..returnText(frame,Type,args,division) | + | export = export..returnText(frame,Type,args,division,args['showred']) |
| − | |||
if args["con"] then | if args["con"] then | ||
args[2] = args["2B"] | args[2] = args["2B"] | ||
| − | export = export.." ".."/".." "..returnText(frame,Type,args,division) | + | export = export.." ".."/".." "..returnText(frame,Type,args,division,args['showred']) |
end | end | ||
| − | |||
if args["con2"] then | if args["con2"] then | ||
args[2] = args["2C"] | args[2] = args["2C"] | ||
| − | export = export.." ".."/".." "..returnText(frame,Type,args,division) | + | export = export.." ".."/".." "..returnText(frame,Type,args,division,args['showred']) |
end | end | ||
end | end | ||
| − | |||
if args["dir"] then -- Direction | if args["dir"] then -- Direction | ||
export = export.." "..args["dir"] | export = export.." "..args["dir"] | ||
| Line 220: | Line 181: | ||
end | end | ||
end | end | ||
| − | |||
if args[3] then -- Name (argument 3) | if args[3] then -- Name (argument 3) | ||
export = export.." ("..args[3]..")" | export = export.." ("..args[3]..")" | ||
end | end | ||
| − | |||
if args["city"] then -- City 1 | if args["city"] then -- City 1 | ||
| − | export = | + | export = returnPlace(frame,export,args["city"],false,args['showred']) |
elseif args["town"] then -- Or town 1 | elseif args["town"] then -- Or town 1 | ||
| − | export = | + | export = returnPlace(frame,export,args["town"],false,args['showred']) |
end | end | ||
| − | |||
if args["city2"] then -- City 1 | if args["city2"] then -- City 1 | ||
| − | export = | + | export = returnPlace(frame,export,args["city2"],true,args['showred']) |
elseif args["town2"] then -- Or town 1 | elseif args["town2"] then -- Or town 1 | ||
| − | export = | + | export = returnPlace(frame,export,args["town2"],true,args['showred']) |
end | end | ||
| − | + | -- [[ A D D P I C T U R E ]] (If it goes after) -- | |
if args["pic aft"] and picture then | if args["pic aft"] and picture then | ||
export = export.." "..picture | export = export.." "..picture | ||
end | end | ||
| − | |||
return export | return export | ||
end | end | ||
| Line 250: | Line 207: | ||
function p.supported (frame) | function p.supported (frame) | ||
| − | local export = "<u>'''Supported 'Regions''''</u>" | + | local export = "'''Note: All inputs are converted to lowercase'''<br /><u>'''Supported 'Regions':'''</u>" -- Add header |
| − | for | + | local supportedTable = {} -- Used to store all regional tables |
| − | + | local index = {} -- Used to sort alphabetically | |
| + | for correct,_ in pairs(typeTable) do -- Create tables for each region | ||
| + | supportedTable[correct] = {correct} | ||
| + | end | ||
| + | for improper,proper in pairs(divisionTable) do -- Add improper to regional tables | ||
| + | if supportedTable[proper] then | ||
| + | table.insert(supportedTable[proper],improper) | ||
| + | else | ||
| + | mw.log(proper.." doesn't have a type specified") -- Log regions that do not have a type | ||
| + | supportedTable[proper] = {proper,improper} -- Create table | ||
| + | end | ||
end | end | ||
| − | + | for correct,_ in pairs(supportedTable) do -- Add keys to sortable index | |
| − | export = export.."<br /> • "..k | + | table.insert(index,correct) |
| + | end | ||
| + | table.sort(index) -- Sort index | ||
| + | for _,correct in ipairs(index) do -- Go over each proper region | ||
| + | for _,name in ipairs(supportedTable[correct]) do -- Go over each region name | ||
| + | table.sort(supportedTable[correct]) | ||
| + | if name == correct then -- Add to list if proper name | ||
| + | export = export.."<br />• "..name | ||
| + | else -- Add to list if improper name | ||
| + | export = export.."<br />".." ".."• "..name | ||
| + | end | ||
| + | end | ||
| + | end | ||
| + | export = export.."<br /><u>'''Supported MTO signs:'''</u>" -- Add MTO header | ||
| + | for k,v in pairs(miscTable) do -- Add MTO signs | ||
| + | export = export.."<br />• "..k | ||
end | end | ||
| − | return export | + | return export -- Return list |
end | end | ||
Revision as of 23:59, 13 December 2018
Implements {{Jcon}}. Add data to Module:Jcon/data.
local p = {}
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData('Module:Jcon/data')
local miscTable = data.miscTable
local typeTable = data.typeTable
local divisionTable = data.divisionTable
local pictureTable = data.pictureTable
--[[
R E T U R N P I C T U R E
Return the picture of text
]]--
local function returnPicture(frame,Type,args,division)
if not pictureTable[division] then -- Return nothing if region doen't have pictures
return ""
end
local export = pictureTable[division][1]..args[2]..pictureTable[division][2] or "" -- Set picture
if args[2] == "52" and division == "Simcoe" then -- Picture exceptions
export = "Simcoe county road 52.png"
elseif args[2] == "407ETR" and division == "Highway" then
export = "Highway407crest.png"
elseif args[2] == "QEW" and division == "Highway" then
export = "Ontario QEW.svg"
else
routenumber = string.gsub(args[2],"%D","")
if tonumber(routenumber) == nil then
return "" -- Return nothing if there's no route number
elseif tonumber(routenumber) >= 500 and division == "Highway" then -- If highway is secondary
export = "Ontario Highway "..args[2]..".svg"
end
end
if export == "" or nil then return "" end -- Return nothing if export is nothing
if frame:callParserFunction('#ifexist', 'Media:' .. export, '1') ~= '' then -- Set picture sizes
if args["size"] then
return table.concat({"[[File:",export,"|alt=|link=|",args["size"],"]]"})
elseif args[2] == "407ETR" then
return table.concat({"[[File:",export,"|alt=|link=|24px]]"})
elseif division == "Kawartha Lakes" then
return table.concat({"[[File:",export,"|alt=|link=|21px]]"})
else
return table.concat({"[[File:",export,"|alt=|link=|x20px]]"})
end
else
return "" -- Retrun nothing if the picture doesn't exist
end
end
--[[
R E T U R N T E X T
Returns the text/link
]]--
local function returnText(frame,Type,args,division,showred)
local export; -- Link
local display; -- Display (if different from link)
if args[2] == "407ETR" then -- Exception
export = "Ontario Highway".." ".."407"
display = "Highway".." ".."407"
elseif args[2] == "QEW" then -- Exception
export = "Queen Elizabeth Way"
elseif Type == "Highway" then -- Highways
export = "Ontario Highway".." "..args[2]
display = "Highway".." "..args[2]
elseif division == "Kawartha Lakes" then
export ="Kawartha Lakes" .." ".."Road".." "..args[2]
display = Type.." ".."Road".." "..args[2]
elseif Type == "County Highway" or Type == "Regional Highway" then
export = division.." "..args[2]
display = Type.." "..args[2]
else
export = division.." "..Type.." ".."Road".." "..args[2]
display = Type.." ".."Road".." "..args[2]
end
if (frame:callParserFunction('#ifexist',export, '1') ~= '' or showred) and not args["nolink"] then
if display then
export = "[["..export.."|"..display.."]]" -- Show display
else
export = "[["..export.."]]" -- Show export
end
return export
elseif display then
return display
else
return export
end
end
--[[
R E T U R N P L A C E
Add name/link for a city/town
]]--
local function returnPlace(frame,export,place,after,showred)
local preExport
if frame:callParserFunction('#ifexist',place..", Ontario", '1') ~= '' or showred then
preExport = "[["..place..", Ontario|"..place.."]]"
else
preExport = place
end
if after == true then
return export..", "..preExport
else
return export.." – "..preExport
end
end
--[[
P . J C O N
Return final picture(s)/text(s)
]]--
function p.jcon (frame)
local args = getArgs(frame)
args[1] = args[1] or "" -- Unnil args[1]
local Remove = {"regional","region","county","country",
"municipality of","city of","^ "," $"} -- Stuff to remove from lowercase input
for _,v in ipairs(Remove) do
args[1] = mw.ustring.gsub(args[1]:lower(),v,'')
end
args[2] = args[2] or ""
local division = divisionTable[args[1]:lower()] or args[1]
local Type = typeTable[division]
local export = ""
if miscTable[args[1]:lower()] or miscTable[args[2]:lower()] then
return miscTable[args[1]:lower()] or miscTable[args[2]:lower()]
end
if not typeTable[division] then -- Region not in typeTable
return "​"
end
if args[2] == "" then -- Need args[2] after this point
return "​"
end
if args["ot"] then -- If output should only be text
args["nosh"] = "yes"
args["nolink"] = "yes"
end
args["2A"] = args[2] -- road1 is args[2]
if args["con"] then args["2B"] = args["con"] end -- road 2 is args["con"]
if args["con2"] then args["2C"] = args["con2"] end -- road 3 is args["con2"]
-- [[ G E T P I C T U R E ]] --
if not args["nosh"] then -- If allowed to add shield
args[2] = args["2A"] -- Set args[2] to road 1
picture = returnPicture(frame,Type,args,division) -- Return picture of road 1
if args["con"] then
args[2] = args["2B"] -- Set args[2] to road 2
picture = picture.." "..returnPicture(frame,Type,args,division) -- Return picture of road 2
end
if args["con2"] then
args[2] = args["2C"] -- Set args[2] to road 3
picture = picture.." "..returnPicture(frame,Type,args,division) -- Return picture of road 3
end
if picture ~= "" and not args["pic aft"] then -- If a picture was returned and picture goes first
picture = picture.." " -- Add a space
end
end
-- [[ A D D P I C T U R E ]] (If it goes before) --
if not args["pic aft"] and picture then -- If picure goes first
export = picture
end
-- [[ A D D T E X T ]] --
if not args["notext"] then -- If allowed to show text
args[2] = args["2A"]
export = export..returnText(frame,Type,args,division,args['showred'])
if args["con"] then
args[2] = args["2B"]
export = export.." ".."/".." "..returnText(frame,Type,args,division,args['showred'])
end
if args["con2"] then
args[2] = args["2C"]
export = export.." ".."/".." "..returnText(frame,Type,args,division,args['showred'])
end
end
if args["dir"] then -- Direction
export = export.." "..args["dir"]
if args["condir"] then
export=export.."/"..args["condir"]
end
end
if args[3] then -- Name (argument 3)
export = export.." ("..args[3]..")"
end
if args["city"] then -- City 1
export = returnPlace(frame,export,args["city"],false,args['showred'])
elseif args["town"] then -- Or town 1
export = returnPlace(frame,export,args["town"],false,args['showred'])
end
if args["city2"] then -- City 1
export = returnPlace(frame,export,args["city2"],true,args['showred'])
elseif args["town2"] then -- Or town 1
export = returnPlace(frame,export,args["town2"],true,args['showred'])
end
-- [[ A D D P I C T U R E ]] (If it goes after) --
if args["pic aft"] and picture then
export = export.." "..picture
end
return export
end
--[[
P . S U P P O R T E D
Return all supported "regions" in a list format
]]--
function p.supported (frame)
local export = "'''Note: All inputs are converted to lowercase'''<br /><u>'''Supported 'Regions':'''</u>" -- Add header
local supportedTable = {} -- Used to store all regional tables
local index = {} -- Used to sort alphabetically
for correct,_ in pairs(typeTable) do -- Create tables for each region
supportedTable[correct] = {correct}
end
for improper,proper in pairs(divisionTable) do -- Add improper to regional tables
if supportedTable[proper] then
table.insert(supportedTable[proper],improper)
else
mw.log(proper.." doesn't have a type specified") -- Log regions that do not have a type
supportedTable[proper] = {proper,improper} -- Create table
end
end
for correct,_ in pairs(supportedTable) do -- Add keys to sortable index
table.insert(index,correct)
end
table.sort(index) -- Sort index
for _,correct in ipairs(index) do -- Go over each proper region
for _,name in ipairs(supportedTable[correct]) do -- Go over each region name
table.sort(supportedTable[correct])
if name == correct then -- Add to list if proper name
export = export.."<br />• "..name
else -- Add to list if improper name
export = export.."<br />".." ".."• "..name
end
end
end
export = export.."<br /><u>'''Supported MTO signs:'''</u>" -- Add MTO header
for k,v in pairs(miscTable) do -- Add MTO signs
export = export.."<br />• "..k
end
return export -- Return list
end
--[[
Return Output
End of module
]]--
return p;