Jump to content

Module:Unicode chart

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cobaltcigs (talk | contribs) at 16:01, 9 September 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local libUtil = require('libraryUtil')
local checkType = libUtil.checkType
local mTableTools = require('Module:TableTools')
local mUnicode = require('Module:Unicode data')
local mArguments = require('Module:Arguments')

local p = {} 

function p.plain(x) return mw.ustring.char(x) end
function p.link(x)
	str = p.plain(x)
	if mUnicode.is_valid_pagename(str) then return "[["..str.."]]"
	else return str end
end
function p.ifexist(x)
	if mw.title.new(x, 0).exists then return p.link(x)
	else return p.plain(x) end
end

function p.main( frame )
	local args = {}
	for k, v in pairs(mArguments.getArgs(frame)) do args[k] = v end
	local blockname, linkmode, pdf, version = args["name"], args["link"], args["pdf"], args["version"]
	refs = ''
	if version then refs = string.format('<ref name="version">As of Unicode version %s</ref>', version) end
	refs = refs..'<ref name="gray">Gray areas indicate non-assigned code points</ref>'
	refs = frame:preprocess(refs)
	local first, last = tonumber(args["start"],16), tonumber(args["end"],16)
	local fmt = p.plain
	if linkmode == "yes" then fmt = p.link end
	if linkmode == "ifexist" then fmt = p.ifexist end
	if first > last then first, last = last, first end
	local firstR = first - (first % 16)
	local lastR = last - (last % 16)
	pad = '03'
	if lastR > 65536 then pad = '' end
	local rhfmt = '\t\t<th class="row">U+%'..pad..'Xx</th>'
	wt = {}
	wt[#wt+1] = '<table class="wikitable nounderlines unicode-chart">'
	namelink = string.format('<div class="title">[[%s (Unicode block)|%s]]%s</div>', blockname, blockname, refs)
	fmtpdf = '<div class="pdf-link">[%s Official Unicode Consortium code chart] (PDF)</div>'
	if pdf then
		namelink = namelink..string.format(fmtpdf, pdf)
	end
	wt[#wt+1] = '<tr><th class="title-bar" colspan="17">'..namelink..'</th></tr>'
	th = {}
	th[#th+1] = '<tr><th class="empty"></th>';
	for c = 0,15,1 do
		th[#th+1] = string.format('<th class="column">%X</th>', c)
	end
	th[#th+1] = '</tr>';
	wt[#wt+1] = table.concat(th)
	for r = firstR, lastR, 16 do
		tr = {}
		tr[#tr+1] = '\t<tr>\n'
		tr[#tr+1] = string.format(rhfmt, r/16)
		for c = 0,15,1 do
			n = r+c
			charname = mUnicode.lookup_name(n)
			if n < first or n > last then
				tr[#tr + 1] = '\t\t<td class="excluded"></td>\n'
			elseif string.match(charname, '<') then
				tr[#tr + 1] = string.format('\t\t<td title="U+%X RESERVED" class="char reserved"></td>\n', n);
			else
				str = mw.ustring.char(n)
				if mUnicode.is_rtl(str) then dir = "rtl" else dir = "ltr" end
				tr[#tr + 1] = string.format('\t\t<td title="U+%X %s" class="char" dir="%s">%s</td>\n', n, charname, dir, str);
			end
		end
		tr[#tr+1] = '\t</tr>\n'
		wt[#wt+1] = table.concat(tr)
	end
	wt[#wt+1] = '<tr><td class="notes" colspan="17">'..frame:preprocess("'''Notes:'''\n{{reflist}}")..'</td></tr>'
	wt[#wt+1] = '</table>'
	tStyles = frame:extensionTag{ name = 'templatestyles', args = { src = 'Unicode chart/styles.css'} }
	return tStyles..table.concat(wt)
end
		
return p