Jump to content

Module:Region topic

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SiBr4 (talk | contribs) at 19:06, 31 August 2015 (Moved from my sandbox). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

function p.main(frame)

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 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,
}

--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 "").." ") or blank(args[1]) or ""
local suff = blank(" "..(args.suffix or "")) or blank(args[2]) 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)~=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 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
  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 switch = country.switch
    local citem, cunl
    --If not excluded via code or switch parameter
    if (yn(args[code],1)~=0 and (yn(args[switch or ""],1)~=0 or yn(args[code],2)~=2)) 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 hidden or nonexisting
      if (not nrl or mw.title.new(link).exists)
         and not (country.hidden and yn(args[code],0)==0 and yn(args[switch],0)==0)
      then citem = list:tag("li"):wikitext("[["..link.."|"..name.."]]")
      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 sublist = mw.html.create("ul")
      local nsub = 0 --Count items
      for k,scountry in ipairs(country.subgroup) do
        --Similar to main item code
        local scode = scountry[1]
        local sswitch = scountry.switch
        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--changed
          then slink = pref..slart..slname..suff
          else slink = args[scode] or (pref..slart..slname..suff)
        end
        if (not nrl or mw.title.new(slink).exists)
           and (not country.subgroup.hidden and not scountry.hidden or yn(args[scode],0)~=0
                or yn(args[scode],2)==2 and (yn(args[country.subgroup.switch or ""],0)~=0
                                             or yn(args[sswitch or ""],0)~=0))
        then
          sublist:tag("li"):wikitext("[["..slink.."|"..sname.."]]")
          nsub = nsub + 1
        end
      end
      --If non-empty sub-list, add it to country item
      if nsub > 0 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
  nbargs["group"..i] = data["group"..i].name
  nbargs["list"..i] = tostring(list)
end

--Invoke navbox module
return require("Module:Navbox")._navbox(nbargs)

end

return p