Module:Unicode chart
Appearance
![]() | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
![]() | This module uses TemplateStyles: |
Implements {{unicode chart}}
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 = {}
local scriptCode = nil
function getScriptCode(n)
for i,r in ipairs(mScripts.ranges) do
if n >= r[1] and n <= r[2] then scriptCode = r[3] end
end
return scriptCode
end
function getRange(blockname)
blockname = string.lower(blockname)
r = {}
for i,b in ipairs(mBlocks) do
if blockname == string.lower(b[3]) then
r.first = b[1]
r.last = b[2]
end
end
return r
end
function parseHex(s) if s then return tonumber(s,16) else return nil end end
function scriptTemplate(n, linkmode)
code = getScriptCode(n)
s = mw.ustring.char(n)
if code == nil or s == nil then return '' end
doLink = mUnicode.is_valid_pagename(s) and (linkmode=="yes" or (linkmode=="ifexist" and mw.title.new(s, 0).exists))
if doLink then s = string.format("[[%s]]", s) end
return string.format("{{Script|%s|%s}}",code,s)
end
function p.main( frame )
local args = {}
for k, v in pairs(mArguments.getArgs(frame)) do args[k] = v end
local blockname, linkmode, version = args["name"], args["link"], 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
pdf = args["pdf"] or string.format('https://www.unicode.org/charts/PDF/U%04X.pdf', first)
if first > last then first, last = last, first end
local firstR = first - (first % 16)
local lastR = last - (last % 16)
wt = {}
wt[#wt+1] = '<table class="wikitable nounderlines unicode-chart">'
titlebar = 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
titlebar = titlebar..string.format(fmtpdf, pdf)
end
wt[#wt+1] = '\t<tr><th class="title-bar" colspan="17">'..titlebar..'</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('\t\t<th class="row">U+%03Xx</th>', 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, linkmode)
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