Accordion

From Wikiyouth
Jump to navigation Jump to search

Documentation for this module may be created at Module:Accordion/doc

local p = {}

local function trim(s)
	return mw.ustring.gsub(mw.ustring.gsub(s, '%s', ' '), '^%s*(.-)%s*$', '%1')
end

local function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function p.fn(frame)


	local origArgs = (type(frame.getParent) == 'function') and frame:getParent().args or frame
    
 
	local args = {}
	for k, v in pairs(origArgs) do
		if v ~= '' then
			args[k] = v
			mw.log(v)
		end
	end
	
		
		
	local tbl = mw.html.create('div')
	tbl:addClass('mw-collapsible-container')
	
	-- toccolours mw-collapsible mw-collapsed
	-- mw-collapsible-content
	
	 mw.log('args')
    mw.log(args)

	
	--local rows = math.ceil(table.getn(args) / 2)
--local rows = math.ceil(origArgs / 2)
	local rows =  math.ceil(tablelength(args) / 2)
mw.log('rows')
mw.log(rows)
	for i = 1, rows do
		local a = trim(args[i*2 - 1] or '')
		local b = trim(args[i*2] or '')

		if a ~= '' and b ~= '' then
			--table.insert(p,  a .. b )
			local item = tbl:tag('div')
			item:addClass('toccolours mw-collapsible mw-collapsed')
			if(i ~= 1) then
				item:css('border-top', 'none')
			end
			item:wikitext(a)
			item = item:tag('div')
			item:addClass('mw-collapsible-content')
			item:wikitext(b)
		end
	end
	
	return tostring(tbl)

end
--=p.fn({["a"]="1",["b"]="2",["c"]="3"})
-- =p.fn({"a","b","c"})
-- =p.fn({"a","b","c","d","e"})
return p