Module:SimpleHTMLBuilder

From Predecessor Wiki
Jump to navigation Jump to search
Template-noinfo.svg Documentation
This module has no documentation. If you know how to use this module, please add some. Module:SimpleHTMLBuilder/doc

local concat	= table.concat
local gsub		= string.gsub
local sub		= string.sub

local p = {}
local f = {}

local function return_table(self, index)
	if self[index] == "" then
		self[index] = {count = 0}
	elseif self[index] == nil then
		error("Tag cannot be edited once closed.")
	end
	
	return self[index]
end

function f:node(node)
	self:wikitext(node)

	if node.parents == nil then
		node.parents = {count = 0}
	end
	
	node.parents.count = node.parents.count+1
	node.parents[node.parents.count] = {parent = self, nodePtr = self[11].count}

	return self
end

function f:wikitext(text, text2)
	if text2 ~= nil then
		error("Wikitext does not work with multiple arguments.")
	end
	
	local t = return_table(self, 11)
	
	t.count = t.count + 1
	t[t.count] = text
	
	return self
end

function f:newline()
	return self:wikitext("\n")
end
	
function f:tag(tag, args)
	local node = p.create(tag, args)
	self:node(node)

	return node
end

function f:attr(name, value)
	if(value == nil) then
		return self
	end
	if type(name) == "table" then
		for i,v in pairs(name) do
			self:attr(i,v)
		end

		return self
	end
	
	if self[1] ~= "<" then
		error("Attempting to create attribute without a tag")
	end

	local t = return_table(self, 9)
	
	t.count = t.count + 1
	t[t.count] = ' '

	t.count = t.count + 1
	t[t.count] = gsub(name, "%<[^%>]*%>", "") --removing tags
	
	t.count = t.count + 1
	t[t.count] = '="'

	t.count = t.count + 1
	t[t.count] = gsub(value, "%<[^%>]*%>", "")
	
	t.count = t.count + 1
	t[t.count] = '"'
	
	return self
end

function f:addClass(value)
	if self[1] ~= "<" then
		error("Attempting to create class without a tag")
	end
	
	local t = return_table(self, 4)
	
	t.count = t.count + 1
	t[t.count] = gsub(value, "%<[^%>]*%>", "")
	
	t.count = t.count + 1
	t[t.count] = ' '
	
	return self
end

function f:cssText(value)
	if self[1] ~= "<" then
		error("Attempting to create style without a tag")
	end
	
	local t = return_table(self, 7)
	
	t.count = t.count + 1
	t[t.count] = gsub(value, "%<[^%>]*%>", "")
	
	t.count = t.count + 1
	t[t.count] = ';'
	
	return self
end

function f:css(name, value)
	if type(name) == "table" then
		for i,v in pairs(name) do
			self:css(i,v)
		end

		return self
	end

	if self[1] ~= "<" then
		error("Attempting to create style without a tag")
	end
	
	local t = return_table(self, 7)

	t.count = t.count + 1
	t[t.count] = gsub(name, "%<[^%>]*%>", "")
	
	t.count = t.count + 1
	t[t.count] = ':'
	
	t.count = t.count + 1
	t[t.count] = gsub(value, "%<[^%>]*%>", "")
	
	t.count = t.count + 1
	t[t.count] = ';'
	
	return self
end

function f:done()
	local parent = self.parents and self.parents[self.parents.count].parent or nil
	tostring(self)

	return parent or self
end

function f:allDone()
	local node = self
	
	while node ~= node:done() do
		node = node:done()
	end
	
	return node
end

local meta_main = {
	__index = f,
	__tostring = function(self)
		if self.result then
			return self.result
		end

		if self[4] ~= "" then
			self[3] = ' class="'
			self[4] = concat(self[4])
			self[5] = '"'
		end
		
		if self[7] ~= "" then
			self[6] = ' style="'
			self[7] = concat(self[7])
			self[8] = '"'
		end
		
		if self[9] ~= "" then
			self[9] = concat(self[9])
		end

		if self[11] ~= "" then
			for i = 1, self[11].count do
				if type(self[11][i]) == "table" then
				   self[11][i] = tostring(self[11][i])
				end
			end

			self[11] = concat(self[11])
		end

		self.result = concat(self)
		
		if self.parents ~= nil then
			for i = 1, self.parents.count do
				self.parents[i].parent[11][self.parents[i].nodePtr] = self.result
			end
			
			self.parents = nil
		end

		for i = 1, 11 do
			self[i] = nil
		end

		return self.result
	end,
}

function p.create(tag, args)
	if args ~= nil then
		error("Tag creation does not accept multiple arguments.")
	end

	--struct to give to functions.
	local tag_table

	if tag == nil then
		tag_table = {"", "", "", "", "", "", "", "", "", "", ""}
	else
		-- 3 = class start, 4 = class table, 5 = class end, 6 = css start, 7 = css table, 8 = css end, 9 = attributes table, 11 = text table
		tag_table = {"<", tag, "", "", "", "", "", "", "", ">", "", "</", tag, ">"}
	end

	setmetatable(tag_table, meta_main)

	return tag_table
end

return p