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 = {}
local function blankToNil(s)
--Replaces string consisting of only whitespace with nil
return s and string.find(s, '%S') and s or nil
end
local function yn(s,map)
--Converts a "yes"/"no" string s to a boolean. map is a table that specifies
--what each type of input (yes, no, empty, other, unspecified) should be
--interpreted as (1=true, 0=false); its defaults are consistent with {{yesno}}
map = map or {}
local fmap = {y = map.y or 1,
n = map.n or 0,
o = map.o or map.y or 1,
e = map.e or map.n or 0,
u = map.u or map.n or 0}
local yn_map = {yes="y", y="y", ["true"]="y", ["1"]="y", no="n", n="n", ["false"]="n", ["0"]="n", [""]="e"}
local num = s and fmap[yn_map[s] or "o"] or fmap.u
return num ~= 0
end
local function xor(a,b)
--A logical XOR function
return not a ~= not b
end
function p.luamain(args)
--Produces the navbox (for other Lua scripts)
--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 = (type(args.data)=="table" and args.data)
or (type(args.data)=="string" and blankToNil(args.data) and mw.loadData(args.data))
or error("No data page or table specified")
--Link parameters
local pref = blankToNil(args.prefix or args[1])
local suff = blankToNil(args.suffix or args[2])
pref = (pref or "")..(not yn(args.noprefixspace) and pref and " " or "")
suff = (not yn(args.nosuffixspace) and suff and " " or "")..(suff or "")
local art
if args.article then
art = yn(args.article)
else
art = (pref~="" and suff=="")
end
local nrl = (yn(args.noredlinks,{o=0}) and 1) or (yn(args.noredlinks,{o=0,u=1}) and 0.5) or 0
--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
local igroup = 0
for i = 1, (data.ngroups or 1), 1 do
--If group is not hidden or excluded
if not data["group"..i].switch
or not args[data["group"..i].switch] and not data["group"..i].hidden
or args[data["group"..i].switch] and xor(yn(args[data["group"..i].switch],{o=0}),data["group"..i].negate_switch)
then
igroup = igroup + 1
local gdata = data["group"..i].data
--Create list & loop over entries
local list = {}
local listsort = {}
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],{u=1})
and (args[code]
or not (country.switch and args[country.switch]) and not country.hidden
or country.switch and args[country.switch] and xor(yn(args[country.switch],{o=0}),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 = not yn(args[code],{o=0}) and args[code] or (pref..lart..lname..suff)
--Create list item if not nonexisting
if args[code]
or not (nrl==1 or nrl~=0 and (yn(args[code.."_noredlink"],{o=0}) or country.noredlink))
or mw.title.new(link).exists
then
citem = "[["..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 = {}
local sublistsort = {}
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 = not yn(args[scode],{o=0}) and args[scode] or (pref..slart..slname..suff)
if (args[code]
or not (nrl==1 or nrl~=0 and (yn(args[code.."_noredlink"],{o=0}) or scountry.noredlink))
or mw.title.new(slink).exists)
and yn(args[scode],{u=1})
and (args[scode]
or (not (subgroup.switch and args[subgroup.switch]) and not subgroup.hidden
or subgroup.switch and args[subgroup.switch] and xor(yn(args[subgroup.switch],{o=0}),subgroup.negate_switch))
and not (scountry.switch and args[scountry.switch]) and not scountry.hidden
or scountry.switch and args[scountry.switch]
and xor(yn(args[scountry.switch],{o=0}),scountry.negate_switch))
then
sublist[#sublist+1] = "<li>[["..slink.."|"..sname.."]]</li>"
sublistsort[#sublistsort+1] = {args[scode.."_sort"] or args[scode.."_name"] or scountry[2], #sublistsort+1}
end
end
--If non-empty sub-list, add it to country item
if #sublist>0 then
table.sort(sublistsort, function(t1,t2) return t1[1]<t2[1] end)
local ssorted = {}
for li,sle in ipairs(sublistsort) do
ssorted[li] = sublist[sle[2]]
end
citem = (cunl and name or citem).."\n<ul>\n"..table.concat(ssorted,"\n").."\n</ul>"
end
end
if citem then
list[#list+1] = "<li>"..citem.."</li>"
listsort[#listsort+1] = {args[code.."_sort"] or args[code.."_name"] or country[2], #listsort+1}
end
end
--Add group name and data to navbox args
if data["group"..i].name then
if string.match(data["group"..i].name,"%{%{") then
nbargs["group"..igroup] = frame:preprocess(data["group"..i].name)
else
nbargs["group"..igroup] = data["group"..i].name
end
end
--Sort list and move to navbox parameters if not empty
if #list>0 or yn(args.showemptygroups) then
table.sort(listsort, function(t1,t2) return t1[1]<t2[1] end)
local sorted = {}
for li,sle in ipairs(listsort) do
sorted[li] = list[sle[2]]
end
nbargs["list"..igroup] = "<ul>\n"..table.concat(sorted,"\n").."\n</ul>"
end
end
end
--Invoke navbox module
return require("Module:Navbox")._navbox(nbargs)
end
function p.main(frame)
--Produces the navbox (for wikitext usage)
local args = require("Module:Arguments").getArgs(frame, {removeBlanks = false, trim = false})
return p.luamain(args)
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 = args.data and mw.loadData(args.data) or error("No data page specified")
--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 do
--Add group data
local grow = tab:tag("tr"):css("background-color","#eaf1fe")
grow:tag("td")
if data["group"..i] and 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 or "<i>Unnamed group</i>")
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