Module:Navbox

-- Load necessary modules. require('Module:No globals') local getArgs

local p = { }

-- Helper functions

local function union(t1, t2) local temp = {} for _, v in pairs(t1) do		temp[v] = true end for _, v in pairs(t2) do		temp[v] = true end local ret = {} for k in pairs(temp) do		table.insert(ret, k)	end table.sort(ret) return ret end

local function getArgNums(prefix) local ret = { } for k in pairs(mw.getCurrentFrame:getParent.args) do if string.match(k, "^" .. prefix .. "[0-9]%d*$") then table.insert(ret, tonumber(string.sub(k, string.len(prefix) + 1))) end end table.sort(ret) return ret end

-- Box class definition

local Navbox = {} Navbox.__index = Navbox

function Navbox.new(args) args = args or {} local obj = {}

-- Set the arguments obj.args = args mw.logObject(obj)

return setmetatable(obj, Navbox) end

function Navbox:setParameters local args = self.args self.title = args.title self.above = args.above

self.rows = self:setRows self.below = args.below end

function Navbox:setRows local args = self.args local nums = union(getArgNums('group'), getArgNums('list'))

local t = { } for _,v in pairs(nums) do		table.insert(t, 			{				group = args['group' .. v],				list = args['list' .. v]			}) end return t end

function Navbox:export local root = mw.html.create:tag('div') root:addClass('navbox') if self.title then local title = root:tag('div') title :addClass('navbox-title') :wikitext(self.title) end if self.above then local above = root:tag('div') above :addClass('navbox-above') :wikitext(self.above) end for _,v in ipairs(self.rows) do		if v.list then if v.group then local group = row:tag('div') group:addClass('navbox-group') group:wikitext(v.group) end local list = row:tag('div') list:addClass('navbox-list') list:wikitext(v.list) end end if self.below then local below = root:tag('div') below :addClass('navbox-below') :wikitext(self.below) end local templateStyle = mw.getCurrentFrame:extensionTag( 'templatestyles', '', { src = 'Template:Navbox/styles.css' } );

return templateStyle .. tostring(root) end

-- Exports

function p.navbox(frame) if not getArgs then getArgs = require('Module:Arguments').getArgs end local box = Navbox.new(getArgs(frame)) box:setParameters return box:export end

if mw.site.siteName:byte == 66 then return p end