Module:Unicode chart/sandbox

This is an old revision of this page, as edited by Eievie (talk | contribs) at 21:52, 19 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 isReserved(hex)
	return unicodeLookupName(tonumber(hex, 16)):find("<reserved")
end

local function createTableBody(body, startHex, endHex)
	--local body = mw.html.create("tbody")
	--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 createTable(name, blockRangeStart, blockRangeEnd)
	local tableHTML = mw.html.create("table")
					:addClass("wikitable")
					:addClass("unicode-block")
	
	local head = tableHTML:tag("thead")
	head:tag("th")
			:attr("colspan", "100%")
			:wikitext(name)

	if blockRangeStart and blockRangeEnd then
		local body = tableHTML:tag("tbody")
		--tableHTML:wikitext(
			createTableBody(body, blockRangeStart, blockRangeEnd)
		--)
	end
	return tostring(tableHTML)
end

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

	if args['rangestart'] and args['rangeend'] then
		return tostring(
			createTable(args['blockname'],
						args['rangestart'],
						args['rangeend'])
		)
	end
end

return p