Moduł:Charmap

Z Wikipedii, wolnej encyklopedii
 Dokumentacja modułu [stwórz] [odśwież]
local p = {}

local namedRefTable = mw.loadData( 'Moduł:Unicode2NamedRef' )
local needsNamedRefs, needsUTF16, needsPlName

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 'zbiór', and
    -- 'zbiór1', 'zbiór', and 'zbiór5' 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 znakClass = pargs['znakClass'] or ''
        local znakStyle = pargs['znakStyle'] or 'font-size:120%'
        local nazwaClass = pargs['nazwaClass'] or ''
        local nazwaStyle = pargs['nazwaStyle'] or 'text-transform:uppercase;font-size:smaller'
		local cpPageNames = {
			['dos'] = "CP",
			['windows'] = "Windows-",
			['iso8859'] = "ISO-8859-",
			['ebcdic'] = "EBCDIC ",
			['mapa'] = "",
		}
        local maps = {}
        local nazwa,plname,image,encoding,unicode,utf8,utf16,numref,namedref,mapped,a,s = {},{},{},{},{},{},{},{},{},{}
        for i, _ in pairs(cpPageNames) do
        	maps[i] = getArgNums(pargs,i,'znak%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 (nazwa,pargs["nazwa" .. tostring(i)] or pargs["Nazwa w Unikodzie " .. tostring(i)] or "")
                table.insert (plname,pargs["plname" .. tostring(i)] or pargs["Unicode česky " .. tostring(i)] or "")
                if pargs["plname" .. tostring(i)] or pargs["Unicode česky " .. tostring(i)] then needsPlName = true end
                table.insert (encoding,"dziesiętnie")
                table.insert (encoding,"szesnastkowo")
                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,'&amp;#' .. tonumber(pargs[i],16) .. ';')
                table.insert (numref,'&amp;#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 'mapa' .. tostring(j)) or j .. maps[j][k]) .. 'znak' .. tostring(i)] or '',16) or '—')
					table.insert (mapped[j][k],pargs[((j=='' and 'mapa' .. tostring(j)) or j .. maps[j][k]) .. 'znak' .. 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="' .. znakClass .. '" style="' .. znakStyle .. '">' .. table.concat (image, '</th><th colspan="2" class="' .. znakClass .. '" style="' .. znakStyle .. '">') .. '</th></tr><tr><th>Nazwa w Unikodzie</th><td colspan="2" class="' .. nazwaClass .. '" style="' .. nazwaStyle .. '">' .. table.concat (nazwa, '</td><td colspan="2" class="' .. nazwaClass .. '" style="' .. nazwaStyle .. '">') .. '</td></tr>')
        if needsPlName then
        	table.insert(result,'<tr><th>Nazwa polska</th><td colspan="2" class="' .. nazwaClass .. '" style="' .. nazwaStyle .. '">' .. table.concat (plname, '</td><td colspan="2" class="' .. nazwaClass .. '" style="' .. nazwaStyle .. '">') .. '</td></tr>')
        end
        table.insert(result,'<tr><th>Kodowanie</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>[[Odwołania znakowe SGML]]</th><td>' .. table.concat (numref, '</td><td>') .. '</td></tr>')
        if needsNamedRefs then
        	table.insert(result,'<tr><th>[[Odwołania znakowe SGML]]</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=='mapa' then outCpName = pargs['mapa' .. maps['mapa'][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