Protected page

Module:Utility box

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

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

local function isNotEmpty(v)
	return v ~= nil and v ~= ''
end

function p.main(frame)
	local args = getArgs(frame, {
		removeBlanks = false
	})
	local image = args.image or '[[File:Banana aqua.png|40px|link=]]'
	local imageRight = args.imageright or args["image-right"] or ''
	local header = args.header or args.title or args.heading or ''
	local headerRight = args.headingright or args["heading-right"] or args.headerright or args["header-right"] or ''
	local message = args.text or args.message or '<span class="error">Error: no <code>text</code> or <code>message</code> provided!'
	local templatestyles = args.templatestyles or ''
	
	-- Stylesheets (must remain as raw strings)
	local stylesheets = {}
	table.insert(stylesheets, frame:extensionTag{ name = 'templatestyles', args = { src = 'Utility box/styles.css'} })
	if isNotEmpty(templatestyles) then
		table.insert(stylesheets, frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles } })
	end
	
	local bodyClass = args.class or args.bodyclass or ''
	local bodyStyle = args.style or ''

	local box = mw.html.create('div')
		:addClass('utility-box')
		:addClass(bodyClass ~= '' and bodyClass or nil)
		:cssText(bodyStyle)
	
	-- Left image
	if not (isNotEmpty(imageRight) and not isNotEmpty(args.image)) then
		box:tag('div')
			:addClass('utility-box-image')
			:wikitext(image)
	end
	
	-- Text container
	local textDiv = box:tag('div')
		:addClass('utility-box-text')
	
	-- Header row
	if isNotEmpty(header) or isNotEmpty(headerRight) then
		local topDiv = textDiv:tag('div')
			:addClass('utility-box-top')
		
		topDiv:tag('div')
			:addClass('utility-box-topleft')
			:wikitext(header)
		
		if isNotEmpty(headerRight) then
			topDiv:tag('div')
				:addClass('utility-box-topright')
				:wikitext(headerRight)
		end
	end
	
	-- Message
	textDiv:tag('div')
		:addClass('utility-box-bottom')
		:wikitext(message)
	
	-- Right image
	if isNotEmpty(imageRight) then
		box:tag('div')
			:addClass('utility-box-imageright')
			:wikitext(imageRight)
	end
	
	return table.concat(stylesheets) .. tostring(box)
end

return p