Jump to content

Module:Buckets

From Wikibooks, open books for an open world
local p = {}

function p.main(frame)
	local args = frame:getParent().args
	local html = mw.html.create()

	local i = 1
	while args[i] do
		local label = args[i]
		local content = args[i + 1]

		local labelStyle = args['style' .. i]
		local contentStyle = args['style' .. (i + 1)]

		-- Add label (odd index)
		if label and label ~= '' then
			local labelDiv = mw.html.create('div')
			labelDiv:addClass('bucket-leaf')
			if labelStyle then
				labelDiv:cssText(labelStyle)
			end
			labelDiv:wikitext(label)
			--:node method adds as sibiling, :tag as child element
			html:node(labelDiv)
		end

		-- Add content (even index)
		if content and content ~= '' then
			-- if there's a full .bucket-branch div (a nested template) already
			if mw.ustring.find(content, '<div[^>]+class="bucket%-branch"') then
				html:node(content) -- Insert raw HTML, don't wrap
			else
				local contentDiv = mw.html.create('div')
				contentDiv:addClass('bucket-leaf')
				if contentStyle then
					contentDiv:cssText(contentStyle)
				end
				contentDiv:wikitext(content)
				html:node(contentDiv)
			end
		end

		i = i + 2
	end

	return tostring(html)
end

return p