Module:Unicode chart/sandbox

This is an old revision of this page, as edited by Eievie (talk | contribs) at 19:42, 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 mUnicodeData = require('Module:Unicode data')
--local frame

local function unicodeChar(hex)
	if mUnicodeData.lookup_name(tonumber(hex, 16)):find("<reserved") then
		return "reserved"
	else
		return '&#x'.. hex .. ';'
	end
--	return frame:expandTemplate{
--				title = 'unichar',
--				args = {hex}
--			}
--	return '&#x'.. hex .. ';'
--			.. '<br>' .. lookup({'name', hex})
end

local function createTableBody(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")
				:cssText("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
			row:tag("td"):wikitext(
				unicodeChar(rowHex .. string.format("%X", colIndex))
			)
		end
	end
	return tostring(body)
end

local function createTable(blockRangeStart, blockRangeEnd)
	local tableHTML = mw.html.create("table")
					:addClass("wikitable")
					:addClass("unicode-block")
	
	tableHTML:tag("thead"):colspan("100%")
			:wikitext("name")

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

function p.main(frame)--Arg)
	--frame = frameArg
	local args = getArgs(frame)
	
	local tableHTML = mw.html.create("table")
					:addClass("wikitable")
					:addClass("unicode-block")

	if args['block-range-start'] and args['block-range-end'] then
		tableHTML:wikitext(
				createTableBody(args['block-range-start'],
								args['block-range-end'])
				)
	end
	
	return tostring(tableHTML)
end

return p