Jump to content

Module:Unicode chart

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Renamed user awfwvowjvwrvnwio (talk | contribs) at 16:56, 2 February 2018 (Create a Lua module for Unicode charts (pre-alpha)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module constructs Unicode tables.

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p._main(args)
	local cellpadding = tonumber(args.cellpadding) or 5
	local header0 = '{| border="1" cellspacing="0" cellpadding="' .. cellpadding
	local titlesize = args.titlesize or "large"
	local header1 = '" class="wikitable nounderlines Unicode" style="border-collapse:collapse;background:#FFFFFF;font-size:' .. titlesize
	local rowbackground = args.rowbackground or "#F8F8F8"
	local header2 = ';text-align:center"\n| colspan="17" style="background:' .. rowbackground
	local header3 = ';font-size:small" |'
	local name = args.name
	if type(name) ~= "string" then
		error("name must be a string", 1)
	end
	local headtext
	if args.headtext then
		headtext = args.headtext
	else
		if type(args.link) == "string" then
			local titleObject = mw.title.new(args.link)
			headtext = "'''[[" .. args.link .. "|" .. args.name .. "]]'''"
		else
			headtext = args.name
		end
	end
	local noterefs = args.noterefs or ""
	local header4 = '<br />[https://www.unicode.org/charts/PDF/U'
	-- start must be a number or a decimal string
	if not (type(args.start) == "number" or type(args.start) == "string") then
		error("start is not a string or a number")
	end
	local start = tonumber(args.start)
	local header5 = '.pdf Official Unicode Consortium chart] (PDF)\n|- style="background:'
	local header6 = ';font-size:small"\n| style="width:'
	local firstColumnWidth = args.first_column_width or "45pt"
	local header7 = '" &nbsp;'
	local columnWidth = args.column_width or "20pt"
	-- do not hardcode the hexadecimal part: generate it here
	local hexchars = "0123456789ABCDEF"
	local header8 = ''
	for i = 1, 16 do
		local c = string.sub(hexchars, i, i)
		header8 = header8 .. ' || style="width:' .. columnWidth .. ';"  | ' .. c
	end
	-- Finally, create the header.
	local header =
	   header0 .. header1       .. header2 .. header3
	.. name    .. noterefs      .. header4 .. string.format("%04X", start)
	.. header5 .. rowbackground .. header6 .. firstColumnWidth
	.. header7 .. header8
	-- Use the positional arguments as the names of the characters.
	local argnum = 1
	local current = start
	while args[argnum] ~= nil do
		local row = '|-\n| style="background:' .. rowbackground .. ';font-size:small" | '
		row = row .. string.format('U+%04Xx\n', current / 16)
		local nxt = current + 16
		while current < nxt do
			local prefix = ""
			local suffix = ""
			local ch = mw.ustring.char(border)
			if args[argnum] == "border" then
				argnum = argnum + 1
				prefix = '<span style="display: inline-block; border: 1px dashed' .. (args.borderColor or "black") .. '>'
				suffix = '</span>'
			end
			if args.show_codepoint == false or args.show_codepoint == "false" then
				row = row .. mw.ustring.format('| title="%s" | %s&#x%04X;%s\n', args[argnum], prefix, current, suffix)
			else
				row = row .. mw.ustring.format('| title="U+%04X: %s" | %s&#x%04X;%s\n', current, args[argnum], prefix, current, suffix)
			end
			current = current + 1
			argnum = argnum + 1
		end
	end
	-- Make the final return string.
	local ret
	=  '|-\n| colspan="17" style="background:' .. rowbackground
	.. ";font-size:small\" | '''Notes'''"
	.. notes .. "\n|}"
end

function p.main(frame)
	return p._main(getArgs(frame))
end

return p