Module:Unicode chart/sandbox

This is an old revision of this page, as edited by Eievie (talk | contribs) at 02:47, 20 April 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local getArgs = require('Module:Arguments').getArgs
local unicodeLookupName = require('Module:Unicode data').lookup_name
local frame

local function expandTemplate(template, argslist)
	return frame:expandTemplate{
                    title = template,
                    args = argslist
                }
end

local function isReserved(hex)
	return unicodeLookupName(tonumber(hex, 16)):find("<reserved")
end

local function createTableBody(body, startHex, endHex)
	--header
	local labelRow = body:tag("tr")
	labelRow:tag("th")--empty corner cell
	for colIndex=0, 15 do
		labelRow:tag("th")
				:css("width", "20pt")
				:wikitext(string.format("%X", colIndex))
	end
	--fill cells
	local startInt = tonumber(startHex:sub(1, -2), 16)
	local endInt = tonumber(endHex:sub(1, -2), 16)
	for rowIndex=startInt, endInt do
		local row = body:tag("tr")
		local rowHex = string.format("%X", rowIndex)
		row:tag("th"):wikitext('U+'.. rowHex .. 'x')
		for colIndex=0, 15 do
			local hexStr = rowHex .. string.format("%X", colIndex)
			local cell = row:tag("td")
					:attr("title", unicodeLookupName(tonumber(hexStr, 16)))
			if isReserved(hexStr) then
				cell:addClass("reserved")
			else
				cell:wikitext('&#x'.. hexStr .. ';')
			end
		end
	end
	--return tostring(body)
end

local function createTableHeader(head, name, id)
	local name = "<b>" .. name .. "</b>"
					.. expandTemplate('ref label', {id .. '_as_of_Unicode_version', 1})
					.. expandTemplate('ref label', {id .. '_grey', 2})
	local url = "https://www.unicode.org/charts/PDF/" .. id .. ".pdf"
	
	head:tag("th")
			:attr("colspan", "100%")
			:wikitext(name .. "<br />"
				.. "[" .. url .. " Official Unicode Consortium code chart] (PDF)"
			)
end

local function createTableFooter(foot, latest, id)
	local th = foot:tag("th")
			:attr("colspan", "100%")
			:wikitext("'''Notes'''")
	local list = th:tag("ol")
	list:tag("li"):wikitext(
		 expandTemplate('note', {id .. '_as_of_Unicode_version'}),
		 expandTemplate('Unicode version', {prefix= 'Asof', version= '15.1'})
	)
	list:tag("li"):wikitext(
		 expandTemplate('note', {id .. '_grey'}),
		 "Grey areas indicate non-assigned code points"
	)
end

local function createTable(name, rangeStart, rangeEnd, latestChange, id)
	local tableHTML = mw.html.create("table")
					:addClass("wikitable")
					:addClass("unicode-block")
	
	if name then
		local head = tableHTML:tag("thead")
		createTableHeader(head, name, id)
	end

	if rangeStart and rangeEnd then
		local body = tableHTML:tag("tbody")
		createTableBody(body, rangeStart, rangeEnd)
	end
	
	if latestChange then
		local foot = tableHTML:tag("tfoot")
		createTableFooter(foot, latestChange, id)
	end
	return tostring(tableHTML)
end

function p.main(frameArg)
	frame = frameArg
	local args = getArgs(frame)

	if args['rangestart'] and args['rangeend'] then
		local id = 'U' .. args['rangestart']
		return tostring(
			createTable(args['blockname'],
						args['rangestart'],
						args['rangeend'],
						args['latest change'],
						id)
		)
	end
end

return p