Jump to content

Module:Numbered equation

From Wikisource
--[=[
Implementation for [[Template:numbered equation]]
]=]

require('strict')

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local cats = require('Module:Categories')

--[=[
Function docs
]=]
function p.main(frame)
	local args = getArgs(frame)

	-- Try to differentiate between arguments not provided and empty arguments,
	-- and if three args are given but only the equation has any data, then add
	-- a tracking category.
	if (args[1] == nil) and (args[3] == nil) and (args[2] ~= nil) then
		cats:addCat("Pages using numbered equation with two empty arguments")
	end

	local left = args[1] or ""
	local math = args[2] or ""
	local right = args[3] or ""

	-- Special case: with just one param assume it is the math with no numbers.
	if left and (math == "") and (right == "") then
		math = left
		left = ""
		right = ""
	end
	
	local div = mw.html.create('div')
	div
		:addClass("wst-numbered-equation")
		:tag("div")
			:addClass("wst-numbered-equation-left")
			:wikitext(left)
			:done()
		:tag("div")
			:addClass("wst-numbered-equation-math")
			:wikitext(math)
			:done()
		:tag("div")
			:addClass("wst-numbered-equation-right")
			:wikitext(right)
			:done()
	return tostring(div) .. cats:getCats()
end

return p