Modul:Charmap
Vzhled
local p = {}
local namedRefTable = mw.loadData( 'Modul:Unicode2NamedRef' )
local needsNamedRefs, needsUTF16, needsCsName
local function getArgNums(pargs, prefix, sufix)
-- Returns an ordered table containing the numbers (>0) of the arguments
-- that exist for the specified prefix. Includes 0 if prefix without number
-- is used as argument. For example, if the prefix was 'seznam', and
-- 'seznam1', 'seznam', and 'seznam5' exist, it would return {0, 1, 5}.
local nums = { }
if pargs[prefix] then table.insert(nums, 0) end
for k, _ in pairs(pargs) do
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)' .. sufix .. '$')
for i, v in pairs(nums) do if num == tostring(v) then num = nil end end
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
local function unicode2Named(unicode)
-- Returns named character reference for given Unicode code point or nil.
local named = namedRefTable[tonumber(unicode,16)]
return named
end
local function unicode2UTF8(unicode)
local utf8 = {}
if (tonumber(unicode,16) or 0x110000)<0x110000 then
if tonumber(unicode,16)>0xffff then -- 4B
utf8[4] = 0xf0 + math.floor(tonumber(unicode,16)/0x40000)
utf8[3] = 0x80 + math.floor(tonumber(unicode,16)%0x40000/0x1000)
utf8[2] = 0x80 + math.floor(tonumber(unicode,16)%0x1000/0x40)
utf8[1] = 0x80 + tonumber(unicode,16)%0x40
elseif tonumber(unicode,16)>0x7ff then -- 3B
utf8[3] = 0xe0 + math.floor(tonumber(unicode,16)/0x1000)
utf8[2] = 0x80 + math.floor(tonumber(unicode,16)%0x1000/0x40)
utf8[1] = 0x80 + tonumber(unicode,16)%0x40
elseif tonumber(unicode,16)>0x7f then -- 2B
utf8[2] = 0xc0 + math.floor(tonumber(unicode,16)/0x40)
utf8[1] = 0x80 + tonumber(unicode,16)%0x40
else -- 1B
utf8[1] = tonumber(unicode,16)
end
end
return utf8
end
local function unicode2UTF16(unicode)
local utf16 = {}
if (tonumber(unicode,16) or 0x110000)<0x110000 then
if tonumber(unicode,16)>0xffff then -- 2B
utf16[2] = 0xd800 + math.floor(tonumber(unicode,16)/0x400)
utf16[1] = 0xdc00 + tonumber(unicode,16)%0x400
else -- 1B
utf16[1] = tonumber(unicode,16)
end
end
return utf16
end
function p.charmap(frame)
pargs = frame:getParent().args
local tableClass = pargs['tableClass'] or 'wikitable'
local tableStyle = pargs['tableStyle'] or 'text-align:center'
local charClass = pargs['charClass'] or ''
local charStyle = pargs['charStyle'] or 'font-size:300%'
local nameClass = pargs['nameClass'] or ''
local nameStyle = pargs['nameStyle'] or 'text-transform:uppercase;font-size:smaller'
local cpPageNames = {
['dos'] = "CP",
['windows'] = "Windows-",
['iso8859'] = "ISO-8859-",
['ebcdic'] = "EBCDIC ",
['map'] = "",
}
local maps = {}
local name,csname,image,encoding,unicode,utf8,utf16,numref,namedref,mapped,a,s = {},{},{},{},{},{},{},{},{},{}
for i, _ in pairs(cpPageNames) do
maps[i] = getArgNums(pargs,i,'char%d*')
mapped[i] = {}
for j,_ in pairs(maps[i]) do mapped[i][j] = {} end
end
for i, _ in pairs(pargs) do if (type(i) == "number") and tonumber(pargs[i],16) then
table.insert (image,pargs["image" .. tostring(i)] or pargs["obrázek " .. tostring(i)] or ("&#x" .. mw.text.trim(pargs[i]) .. ";"))
table.insert (name,pargs["name" .. tostring(i)] or pargs["Unicode název " .. tostring(i)] or "")
table.insert (csname,pargs["csname" .. tostring(i)] or pargs["Unicode česky " .. tostring(i)] or "")
if pargs["csname" .. tostring(i)] or pargs["Unicode česky " .. tostring(i)] then needsCsName = true end
table.insert (encoding,"dec")
table.insert (encoding,"hex")
table.insert (unicode,tonumber(pargs[i],16))
if pargs["info" .. tostring(i)] or (pargs[info] and (pargs["info" .. tostring(i)]==nil)) then
table.insert (unicode,'[http://www.fileformat.info/info/unicode/char/' .. mw.text.trim(pargs[i]) .. " U+" .. mw.text.trim(pargs[i]) .. "]")
else
table.insert (unicode,"U+" .. mw.text.trim(pargs[i]))
end
a = unicode2UTF8(pargs[i])
s = {}
for j, _ in pairs(a) do table.insert(s,1,tostring(a[j])) end
table.insert (utf8,table.concat(s," "))
s = {}
for j, _ in pairs(a) do table.insert(s,1,string.format('%.2x',a[j])) end
table.insert (utf8,table.concat(s," "))
a = unicode2UTF16(pargs[i])
if tonumber(pargs[i],16)>0xffff then
table.insert (utf16,a[2] .. ' ' .. a[1])
table.insert (utf16,string.format("%.4x",a[2]) .. ' ' .. string.format("%.4x",a[1]))
needsUTF16 = true
else
table.insert (utf16,a[1])
table.insert (utf16,string.format("%.4x",a[1]))
end
table.insert (numref,'&#' .. tonumber(pargs[i],16) .. ';')
table.insert (numref,'&#x' .. mw.text.trim(pargs[i]) .. ';')
table.insert (namedref,unicode2Named(pargs[i]) or "")
if unicode2Named(pargs[i]) then needsNamedRefs = true end
for j, _ in pairs(maps) do for k, _ in pairs(maps[j]) do
table.insert (mapped[j][k],tonumber(pargs[((j=='' and 'map' .. tostring(j)) or j .. maps[j][k]) .. 'char' .. tostring(i)] or '',16) or '—')
table.insert (mapped[j][k],pargs[((j=='' and 'map' .. tostring(j)) or j .. maps[j][k]) .. 'char' .. tostring(i)] or '—')
end end
end end
local result = {}
table.insert(result,'<table class="' .. tableClass .. '" style="' .. tableStyle .. '"><tr><th>Znak</th><th colspan="2" class="' .. charClass .. '" style="' .. charStyle .. '">' .. table.concat (image, '</th><th colspan="2" class="' .. charClass .. '" style="' .. charStyle .. '">') .. '</th></tr><tr><th>Název <div style="white-space: nowrap">v Unicodu</div></th><td colspan="2" class="' .. nameClass .. '" style="' .. nameStyle .. '">' .. table.concat (name, '</td><td colspan="2" class="' .. nameClass .. '" style="' .. nameStyle .. '">') .. '</td></tr>')
if needsCsName then
table.insert(result,'<tr><th>Český název</th><td colspan="2" class="' .. nameClass .. '" style="' .. nameStyle .. '">' .. table.concat (csname, '</td><td colspan="2" class="' .. nameClass .. '" style="' .. nameStyle .. '">') .. '</td></tr>')
end
table.insert(result,'<tr><th>Kódování</th><th>' .. table.concat (encoding, '</th><th>') .. '</th></tr><tr><th>[[Unicode]]</th><td>' .. table.concat (unicode, '</td><td>') .. '</td></tr><tr><th>[[UTF-8]]</th><td>' .. table.concat (utf8, '</td><td>') .. '</td></tr>')
if needsUTF16 then
table.insert(result,'<tr><th>[[UTF-16]]</th><td>' .. table.concat (utf16, '</td><td>') .. '</td></tr>')
end
table.insert(result,'<tr><th>[[HTML entita|Číselná entita]]</th><td>' .. table.concat (numref, '</td><td>') .. '</td></tr>')
if needsNamedRefs then
table.insert(result,'<tr><th>[[HTML entita|Názvová entita]]</th><td colspan="2">' .. table.concat (namedref, '</td><td colspan="2">') .. '</td></tr>')
end
local mapresult={}
for i, _ in pairs(maps) do for j, _ in pairs(maps[i]) do
if i=='map' then outCpName = pargs['map' .. maps['map'][j]] or '?' else outCpName = '[[' .. cpPageNames[i] .. maps[i][j] .. ']]' end
if tonumber(mapped[i][j][1]) then
table.insert(mapresult,1,'<tr><th>' .. outCpName ..'</th><td>' .. table.concat (mapped[i][j], '</td><td>') .. '</td></tr>')
end
end end
return table.concat(result) .. table.concat(mapresult) .. '</table>'
end
return p