Leaguepedia | League of Legends Esports Wiki
Advertisement

Edit the documentation or categories for this module.

In September 2019 this module was moved from HTMLUtil to HtmlUtil to accommodate regex replacements of utility module names with consistent casing conventions.


local i18n = require('Module:i18nUtil')
local util_table = require("Module:TableUtil")
local p = {}

local h = {}

function p.makeFootnoteN(n)
	if n and n ~= "" then
		return string.format('<sup>%s</sup>', n)
	end
	return ""
end

function p.vsAlign(team1, team2, vs, tbl)
	if not tbl then
		tbl = mw.html.create("div")
	end
	tbl:addClass('vs-align-outer')
	tbl:tag("div")
		:addClass('vs-align-left')
		:wikitext(team1)
	:done()
	:tag("div")
		:addClass('vs-align-vs')
		:wikitext(vs or " vs ")
	:done()
	:tag("div")
		:addClass('vs-align-right')
		:wikitext(team2)
	:done()
		
	return tbl
end

function p.blockBox(content, padding)
	local tbl = mw.html.create('table')
		:addClass('blockbox')
	local tr = tbl:tag('tr')
	local td = tr:tag('td')
	for k, v in ipairs(content) do
		local div = td:tag('div')
		div:css({
			display = "inline-block",
			['vertical-align'] = 'top',
		})
		if padding and k ~= #content then
			div:css('padding-right',padding)
		end
		div:wikitext(v)
	end
	return tbl
end

function p.printEmptySortRow(tbl, n)
	local tr = tbl:tag('tr'):css('font-size','60%')
	for i = 1, n do
		tr:tag('th')
		:wikitext('&nbsp;')
	end
	return
end

function p.printEmptyWidthRow(tbl, widths)
	local tr = tbl:tag('tr'):addClass('empty-width-row')
	for _, v in ipairs(widths) do
		tr:tag('td'):css('width', v):wikitext('&nbsp;')
	end
	return
end

function p.printEmptyWidthRowPX(tbl, widths)
	local tr = tbl:tag('tr'):addClass('empty-width-row')
	for _, v in ipairs(widths) do
		tr:tag('td'):css('width', v .. 'px'):wikitext('&nbsp;')
	end
	return
end

function p.printColspanHeader(tbl, display, colspan, class, width)
	local tr = h.printRowIfNeeded(tbl)
	tr:addClass(class)
	local th = tr:tag('th'):addClass('colspan-cell'):attr('colspan', colspan):wikitext(display)
	if width then
		th:cssText(('width:%spx'):format(width))
	end
	return tr
end

function h.printRowIfNeeded(tbl)
	local tr
	if not tbl then
		tr = mw.html.create('tr')
	elseif tbl.tagName == 'tr' then
		tr = tbl
	else
		tr = tbl:tag('tr')
	end
	return tr
end

function p.printColspanCell(tbl, display, colspan, class, width)
	local tr = h.printRowIfNeeded(tbl)
	tr:addClass(class)
	local td = tr:tag('td')
		:attr('colspan', colspan)
		:addClass('colspan-cell')
		:wikitext(display)
	if width then
		td:cssText(('width:%spx'):format(width))
	end
end

function p.printHeaderFromI18n(tbl, data)
	local classes = data.classes or {}
	local class = type(classes) == 'string' and classes
	local tr = h.printRowIfNeeded(tbl)
	tr:addClass(classes.row)
	p.printRowAttrs(tr, data)
	for _, v in  ipairs(data) do
		local th = tr:tag('th')
			:addClass(class or classes[v] or '')
			:wikitext(i18n.print(v) or v)
		p.printCellAttrs(th, data, v)
	end
	return tr
end

function p.printRowsByList(tbl, data, list)
	-- data is an array of tables
	for _, row in ipairs(data) do
		p.printRowByList(tbl, row, list)
	end
	return tbl
end

function p.printRowByList(tbl, row, list)
	if not row.classes then row.classes = {} end
	util_table.mergeAndConcat(row.classes, ' ', list.classes)
	local tr = h.printRowIfNeeded(tbl)
	p.printRowClasses(tr, row)
	p.printRowAttrs(tr, row)
	for _, item in ipairs(list) do
		local td = tr:tag('td')
			:addClass(row.classes[item] or '')
		p.printCellAttrs(td, row, item)
		if row[item] then
			td:wikitext(tostring(row[item]))
		end
	end
	return tr
end

function p.printRowClasses(obj, row)
	if not row then return end
	for _, v in ipairs(row.classes or {}) do
		obj:addClass(v)
	end
end

function p.printRowAttrs(obj, row)
	if not row then return end
	if not row.attrs then return end
	-- cell names go as the top-level keys in attrs, so for a row itself, put
	-- a table with index 1 and put the attrs in that
	p.printAttrs(obj, row.attrs[1])
end

function p.printAttrs(obj, attrs)
	for attr, val in pairs(attrs or {}) do
		obj:attr(attr, val)
	end
end

function p.printCellAttrs(obj, row, cellKey)
	if not row then return end
	if not row.attrs then return end
	p.printAttrs(obj, row.attrs[cellKey])
end

function p.printHelpText(div, text)
	local questionmark = div:tag('div')
		:addClass('helptext-questionmark')
	questionmark:tag('div')
		:addClass('helptext')
		:wikitext(text)
end

function p.clear(tbl)
	if not tbl then tbl = mw.html.create() end
	tbl:tag('div'):addClass('clear')
	return tbl
end

function p.innerColspanTable(tbl, colspan)
	local th = p.innerColspanCellOnly(tbl, colspan)
	local tblNew = th:tag('table'):addClass('nested-table wikitable')
	return tblNew
end

function p.innerColspanCellOnly(tbl, colspan)
	local th = tbl:tag('tr'):tag('th')
		:attr('colspan', colspan)
		:addClass('nested-table-outer')
	return th
end

function p.makeFlatlist(tbl)
	local div = mw.html.create('div')
		:addClass('hlist')
	local ul = div:tag('ul')
	for _, v in ipairs(tbl) do
		ul:tag('li'):wikitext(v)
	end
	return div
end

function p.lastChild(tbl)
	return tbl.nodes[#tbl.nodes]
end

function p.mapChildren(tbl, f, ...)
	for _, node in ipairs(tbl.nodes) do
		f(node, ...)
	end
end

function p.printVerticalData(tbl, data, list)
	for _, v in ipairs(list) do
		if data[v] then
			local tr = tbl:tag('tr')
			tr:tag('th'):wikitext(i18n.print(v))
			tr:tag('td'):wikitext(data[v])
		end
	end
end

return p
Advertisement