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 20:02, 9 September 2019 (redundant to dir in script template <span>). 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 mBlocks = require('Module:Unicode data/blocks')
local mScripts = require('Module:Unicode data/scripts')
local mArguments = require('Module:Arguments')
local p = {} 

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

function getScriptCode(n)
	for i,r in ipairs(mScripts.ranges) do
		if n >= r[1] and n <= r[2] then
			return r[3]
		end
	end
end

function getRange(blockname)
	blockname = string.lower(blockname)
	range = {}
	for i,b in ipairs(mBlocks) do
		if blockname == string.lower(b[3]) then
			range.first = b[1]
			range.last =  b[2]
		end
	end
	return range
end
function parseHex(s) if s then return tonumber(s,16) else return nil end end
function scriptTemplate(n)
	return string.format("{{Script|%s|%s}}", getScriptCode(n), mw.ustring.char(n))
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>'
	
	range = getRange(blockname)
	local first, last = parseHex(args["start"]) or range.first, parseHex(args["end"]) or range.last

	local fmt = plain
	if linkmode == "yes" then fmt = link end
	if linkmode == "ifexist" then fmt = 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] = '\t<tr><th class="title-bar" colspan="17">'..namelink..'</th></tr>\n'
	th = {}
	th[#th+1] = '\t<tr>\n\t\t<th class="empty"></th>\n';
	for c = 0,15,1 do
		th[#th+1] = string.format('<th class="column">%X</th>', c)
	end
	th[#th+1] = '\t</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 = scriptTemplate(n)
				tr[#tr + 1] = string.format('\t\t<td title="U+%X %s" class="char">%s</td>\n', n, charname, 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">'.."'''Notes:'''\n{{reflist}}"..'</td></tr>\n'
	wt[#wt+1] = '</table>'
	tStyles = frame:extensionTag{ name = 'templatestyles', args = { src = 'Unicode chart/styles.css'} }
	return tStyles..frame:preprocess(table.concat(wt))
end
		
return p