Module:Sandbox/Hike395/WWF ecoregions

require('strict') local getArgs = require('Module:Arguments').getArgs local yesNo = require('Module:Yesno') local p = {}

local data = mw.loadData('Module:Sandbox/Hike395/WWF ecoregions/config')

local function biomeTitle(biome) return mw.ustring.format('%s %s ecoregions',		data.realmLinks[biome.realm] or 'Category:WMF ecoregion with unknown realm', biome.realm,		data.biomeLinks[biome.biome] or 'Category:WMF ecoregion with unknown biome', biome.biome) end

function p._navbox(args) local search = mw.ustring.lower(args[1] or mw.title.getCurrentTitle.rootText) local foundBiome = nil for _, biome in ipairs(data.biomes) do		for _, ecoregion in ipairs(biome) do			if ecoregion.ecoregion and mw.ustring.match(mw.ustring.lower(ecoregion.ecoregion), search) then foundBiome = biome break end end if foundBiome then break end end if not foundBiome then return nil end local list = '' for _, ecoregion in ipairs(foundBiome) do		if not ecoregion.missing then list = list .. '* '..ecoregion.ecoregion..'\n' end end local frame = mw.getCurrentFrame local navboxArgs = {title=biomeTitle(foundBiome), state=args.state or 'autocollapse', listclass='hlist', navbar='plain', titlestyle='background: lightgreen', list1=list} return frame:expandTemplate{title='Navbox', args=navboxArgs} end

local function titleStyle(html) html:attr('colspan',2) html:css('background','lightgreen') html:css('text-align','center') html:css('position','relative') html:css('white-space','nowrap') end

function p._table(args) if not args[1] then return nil end local search = mw.ustring.lower(args[1]) local state = mw.ustring.lower(args.state or '') local collapsed = (state == 'collapsed') local collapsible = (state == 'collapsible') or (state == 'autocollapse') local result = mw.html.create('table') result:addClass('wikitable') result:addClass((collapsed or collapsible) and 'mw-collapsible' or '') result:addClass(collapsed and 'mw-collapsed' or '') result:css('width','70%') result:css('margin','0 auto') local title = yesNo(args.title,args.title) if title or collapsed or collapsible then local title = result:tag('tr'):tag('th') titleStyle(title) title:wikitext(type(title) == 'string' and title or			(search:gsub("^%l", string.upper))..' ecoregions') end for _, biome in ipairs(data.biomes) do		if (biome.realm and mw.ustring.match(mw.ustring.lower(biome.realm), search)) or		  (biome.biome and mw.ustring.match(mw.ustring.lower(biome.biome), search)) then local header = result:tag('tr'):tag('th') titleStyle(header) header:wikitext(biomeTitle(biome)) for _, ecoregion in ipairs(biome) do		  		local row = result:tag('tr') local entry = row:tag('td') entry:wikitext(..ecoregion.ecoregion..) local countryList = {} for _, country in ipairs(ecoregion) do		  			table.insert(countryList,..country..) end local countries = row:tag('td') countries:wikitext(mw.text.listToText(countryList)) end end end return result end

function p.navbox(frame) local args = getArgs(frame) return p._navbox(args) or '' end

function p.table(frame) local args = getArgs(frame) return p._table(args) end

return p