Přeskočit na obsah

Modul:Charmap

Z Wikipedie, otevřené encyklopedie
local p = {}

local namedRefTable = mw.loadData( 'Modul:Unicode2NamedRef' )
local needsNamedRefs, needsUTF16

local function getArgNums(pargs, prefix)
    -- 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, v in pairs(pargs) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        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)<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] = 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)>0x7f then			-- 2B
			utf8[2] = 0x80 + math.floor(tonumber(unicode,16)%0x1000/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)<0x110000 then
		if tonumber(unicode,16)>0xffff then			-- 2B
			utf16[2] = 0xd800 + 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:right'
        local maps = getArgNums(pargs,"map")
        local name,csname,image,encoding,unicode,utf8,utf16,numref,namedref,a,s = {},{},{},{},{},{},{},{},{}
        for i, _ in pairs(pargs) do if type(i) == "number" then
                table.insert (image,pargs["image" .. tostring(i)] or pargs["obrázek " .. tostring(i)] or ("&#x" .. 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 "")
                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/' .. pargs[i] .. " U+" .. pargs[i] .. "]")
                else
                        table.insert (unicode,"U+" .. pargs[i])
                end
                a = unicode2UTF8(pargs[i])
                s = {}
                for j, _ in pairs(a) do s[#s-j+1]=tostring(a[j]) end
                table.insert (utf8,table.concat(s," "))
                s = {}
                for j, _ in pairs(a) do s[#s-j+1]=string.format('xx',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('xxxx',a[2]) .. ' ' .. string.format('xxxx',a[1]))
                	needsUTF16 = true
                else
                	table.insert (utf16,a[1])
                	table.insert (utf16,string.format('xxxx',a[1]))
                end
                table.insert (utf16,unicode2UTF16(pargs[i]))
                table.insert (numref,'&amp;#' .. tonumber(pargs[i],16))
                table.insert (numref,'&amp;#' .. pargs[i])
                table.insert (namedref,unicode2Named(pargs[i]) or "")
                if unicode2Named(pargs[i]) then needsNamedRefs = true end
                -- ... =============TODO maps=================
        end end

        result = '<table class="' .. tableClass .. '" style="' .. tableStyle .. '"><thead><tr><th>Znak</th><th colspan="2">' .. table.concat (image, '</th><th colspan="2">') .. '</th></tr><tr><th>Název v Unicodu</th><td colspan="2">' .. table.concat (name, '</td><td colspan="2">') .. '</td></tr><tr><th>Český název</th><td colspan="2">' .. table.concat (csname, '</td><td colspan="2">') .. '</td></tr><tr><th>Kódování</th><th>' .. table.concat (encoding, '</th><th>') .. '</th></tr></thead><tbody><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
                result = result .. '<tr><th>[[UTF-16]]</th><td>' .. table.concat (utf16, '</td><td>') .. '</td></tr>'
        end
        result = result .. '<tr><th>[[HTML entita|Číselná entita]]</th><td>' .. table.concat (numref, '</td><td>') .. '</td></tr>'
        if needsNamedRefs then
        result = result .. '<tr><th>[[HTML entita|Názvová entita]]</th><td colspan="2">' .. table.concat (namedref, '</td><td colspan="2">') .. '</td></tr>'
        end
        -- ... =============TODO maps=================
        result = result .. '</tbody></table>'

        return result
end
 
return p