Module:Region topic
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
This module contains the main code for Lua-based continent-/region-topic navboxes.
Usage
To create a customizable navbox based on this module, create a wrapper template invoking it with the parameters "data" and "name":
{{#invoke:Region topic|main|data=Module:DataPageName|name=
}}
{{{name|{{subst:PAGENAME}}}
}}
|name=
is necessary for correct "V•T•E" links; the parameter {{{name}}}
allows creation of further wrapper templates. |data=
is the page name of the data module for the region names and other options.
See also
- Sandboxes using this module:
- Wikipedia:Lua/Requests/Archive 5 § Re-write Template:World topic as a Lua module
local p = {}
function p.main(frame)
--Produces the navbox
local function blank(s)
--Replaces string consisting of only whitespace with nil
if string.find(s or "","^%s*$") then
return nil
else
return s
end
end
local function yn(s,def)
--Normalizes "boolean" inputs to 1 (yes), 0 (no),
--nil (unrecognized), or the value of the 'def' parameter (nil)
if s then
local yn_map = {yes=1, y=1, ["true"]=1, ["1"]=1, no=0, n=0, ["false"]=0, ["0"]=0, [""]=0}
return yn_map[string.lower(s)]
else
return def
end
end
local function xor(a,b)
--A logical XOR function
if a then
return not b
else
return b
end
end
local args = require("Module:Arguments").getArgs(frame, {removeBlanks = false, trim = false})
--Navbox parameters
local nbargs = {
name = args.name or error("No name parameter"),
state = args.state or "autocollapse",
titlestyle = args.titlestyle,
bodystyle = args.bodystyle,
abovestyle = args.abovestyle,
belowstyle = args.belowstyle,
groupstyle = args.groupstyle,
liststyle = args.liststyle,
listclass = "hlist",
image = args.image,
above = args.above,
border = args.border,
navbar = args.navbar
}
--Load data page
local data
if blank(args.data) then
data = mw.loadData(args.data)
else
error("No data page specified")
end
--Link parameters
local pref = blank(args.prefix or args[1])
local suff = blank(args.suffix or args[2])
pref = (pref or "")..((yn(args.noprefixspace,0) or 1)<1 and pref and " " or "")
suff = ((yn(args.nosuffixspace,0) or 1)<1 and suff and " " or "")..(suff or "")
local art
if args.article then
art = (yn(args.article,0)~=0)
else
art = (pref~="" and suff=="")
end
local nrl = yn(args.noredlinks,0.5) or 0.5
--Navbox title
if args.title then
nbargs.title = args.title
else
local linkart = (art and data.region_the) and (pref=="" and "The " or "the ") or ""
local link = pref..linkart..(data.region or error("No region parameter"))..suff
if nrl~=0 and mw.title.new(link).exists then
nbargs.title = "[["..link.."]]"
else
nbargs.title = link
end
end
--Loop over groups
for i = 1, (data.ngroups or 1), 1 do
--If group is not hidden or excluded
if not data["group"..i].switch
or yn(args[data["group"..i].switch],2)==2 and not data["group"..i].hidden
or yn(args[data["group"..i].switch],2)~=2 and xor((yn(args[data["group"..i].switch]))==1,data["group"..i].negate_switch) then
local gdata = data["group"..i].data
--Create list & loop over entries
local list = mw.html.create("ul")
for j,country in ipairs(gdata) do
local code = country[1]
local name = args[code.."_name"] or country[2]
local citem, cunl
--Determine if country should be included or not
if yn(args[code],1)~=0
and (yn(args[code],2)~=2
or (not country.switch or yn(args[country.switch],2)==2) and not country.hidden
or country.switch and yn(args[country.switch],2)~=2 and xor((yn(args[country.switch]))==1,country.negate_switch)) then
--Determine link target
local lname = country.link or country[2]
local lart = (art and country.the) and "the " or ""
local link
if yn(args[code],1)==1 then
link = pref..lart..lname..suff
else
link = args[code] or (pref..lart..lname..suff)
end
--Create list item if not nonexisting
if yn(args[code],2)~=2 or not (nrl==1 or nrl~=0 and (yn(args[code.."_noredlink"],0)==1 or country.noredlink)) or mw.title.new(link).exists then
citem = list:tag("li"):wikitext("[["..link.."|"..name.."]]")
else
cunl = true
end
else--Mark that unlinked list item will need to be created in case of sub-list
cunl = true
end
--Create sub-list if present
if country.subgroup then
local subgroup = country.subgroup
local sublist = mw.html.create("ul")
for k,scountry in ipairs(subgroup) do
--Similar to main item code
local scode = scountry[1]
local sname = args[scode.."_name"] or scountry[2]
local slname = scountry.link or scountry[2]
local slart = (art and scountry.the) and "the " or ""
local slink
if yn(args[scode],1)==1 then
slink = pref..slart..slname..suff
else
slink = args[scode] or (pref..slart..slname..suff)
end
if (yn(args[code],2)~=2 or not (nrl==1 or nrl~=0 and (yn(args[code.."_noredlink"],0)==1 or scountry.noredlink)) or mw.title.new(slink).exists)
and yn(args[scode],1)~=0
and (yn(args[scode],2)~=2
or ((not subgroup.switch or yn(args[subgroup.switch],2)==2) and not subgroup.hidden
or yn(args[subgroup.switch],2)~=2 and xor((yn(args[subgroup.switch]))==1,subgroup.negate_switch))
and (not scountry.switch or yn(args[scountry.switch],2)==2) and not scountry.hidden
or scountry.switch and yn(args[scountry.switch],2)~=2 and xor((yn(args[scountry.switch]))==1,scountry.negate_switch)) then
sublist:tag("li"):wikitext("[["..slink.."|"..sname.."]]")
end
end
--If non-empty sub-list, add it to country item
if tostring(sublist) ~= "<ul></ul>" then
if cunl then
citem = list:tag("li"):wikitext(name):node(sublist)
else
citem:node(sublist)
end
end
end
end
--Add group name and data to navbox args
if string.match(data["group"..i].name,"%{%{") then
nbargs["group"..i] = frame:preprocess(data["group"..i].name)
else
nbargs["group"..i] = data["group"..i].name
end
--Move list to navbox parameters if not empty
if tostring(list)~="<ul></ul>" or yn(args.showemptygroups,0)==1 then
nbargs["list"..i] = tostring(list)
end
end
end
--Invoke navbox module
return require("Module:Navbox")._navbox(nbargs)
end
function p.list(frame)
--Produces a list of entities and associated parameters (use in template documentation)
--Get input paramers and load data page
local args = require("Module:Arguments").getArgs(frame)
local data
if args.data then
data = mw.loadData(args.data)
else
error("No data page specified")
end
--Create table and header row
local tab = mw.html.create("table"):addClass("wikitable collapsible"):css("color","#000")
local head = tab:tag("tr"):css("font-weight","bold")
head:tag("th"):css("background-color","#e8e8e8"):wikitext("Code")
head:tag("th"):css("background-color","#e8e8e8"):wikitext("Display name [link name]")
head:tag("th"):css("background-color","#e8e8e8"):wikitext("Switch")
head:tag("th"):css("background-color","#e8e8e8"):wikitext("Hidden?")
--Loop over groups
for i = 1, (data.ngroups or 1), 1 do
--Add group data
local grow = tab:tag("tr"):css("background-color","#eaf1fe")
grow:tag("td")
if string.match(data["group"..i].name,"%{%{") then
grow:tag("td"):css("font-weight","bold"):wikitext(frame:preprocess(data["group"..i].name))
else
grow:tag("td"):css("font-weight","bold"):wikitext(data["group"..i].name)
end
grow:tag("td"):cssText(data["group"..i].negate_switch and "text-decoration:overline;" or "")
:wikitext(data["group"..i].switch or "")
grow:tag("td"):wikitext(data["group"..i].hidden and "Yes" or "")
--Loop over group entries
for j,ddata in ipairs(data["group"..i].data) do
--Add single entry data
local drow = tab:tag("tr"):css("background-color","#f8f8f8")
drow:tag("td"):wikitext(ddata[1])
local dname = drow:tag("td"):css("padding-left","1em"):wikitext(ddata[2])
if ddata.the or ddata.link then
dname:wikitext(" ["..(ddata.the and "the" or "")..(ddata.the and ddata.link and " " or "")..(ddata.link or "").."]")
end
drow:tag("td"):cssText(ddata.negate_switch and "text-decoration:overline;" or ""):wikitext(ddata.switch or "")
drow:tag("td"):wikitext(ddata.hidden and "Yes" or (ddata.noredlink and "Depends on existence" or ""))
--Add subgroup data if exists
if ddata.subgroup then
local shead = tab:tag("tr"):css("background-color","#fefce2")
shead:tag("td")
shead:tag("td"):css("padding-left","2em"):css("font-weight","bold"):wikitext("Subgroup")
shead:tag("td"):cssText(ddata.subgroup.negate_switch and "text-decoration:overline;" or ""):wikitext(ddata.subgroup.switch or "")
shead:tag("td"):wikitext(ddata.subgroup.hidden and "Yes" or "")
for k,sdata in ipairs(ddata.subgroup) do
local srow = tab:tag("tr"):css("background-color","#fdfcf4")
srow:tag("td"):wikitext(sdata[1])
local sname = srow:tag("td"):css("padding-left","2em"):css("font-style","italic"):wikitext(sdata[2])
if sdata.the or sdata.link then
sname:wikitext(" ["..(sdata.the and "the" or "")..(sdata.the and sdata.link and " " or "")..(sdata.link or "").."]")
end
srow:tag("td"):cssText(sdata.negate_switch and "text-decoration:overline;" or ""):wikitext(sdata.switch or "")
srow:tag("td"):wikitext(sdata.hidden and "Yes" or (sdata.noredlink and "Depends on existence" or ""))
end
end
end
end
return tab
end
return p