local p = {} local unicode_convert = require('Module:Unicode convert') local numcr2namecr = mw.loadData('Module:Numcr2namecr') local yesno = require('Module:Yesno') local GB18030_cache = {} -- Input e.g. " A B FF ", output "10 11 255" local function hex2dec_words(s) local x = mw.text.split(mw.text.trim(s or ), '%s+') if #x 1 and x[1] then return end for i = 1, #x do x = tonumber(x, 16) or 0 end return table.concat(x, ' ') end -- Wrapper to let us replace Template:UTF-8 and Template:UTF-16 local function template(title, args) if title 'UTF-8' then return unicode_convert.getUTF8{ args args } elseif title 'UTF-16' then return unicode_convert.getUTF16{ args = args } elseif title 'hex2dec' then return tostring(tonumber(args[1], 16)) elseif title 'charmap/numcharref' then local format = args['base'] 'dec' and '&#%d;') .. (next(codes) nil and 'Character' or 'Unicode') .. " name" for i, n in ipairs(names) do -- Display the character in smallcaps output = output .. '\n| colspan="2" class="smallcaps-cm" | ' .. n end end output = output .. '\n|-\n! style="text-align:left;" | Encodings' .. string.rep('|| decimal || hex', #names) .. '\n|-\n| Unicode' for i, n in ipairs(names) do local code = codes or ; -- padleft, get to form U+XXXX or U+XXXX+XXXX. code = (#code >= 4) and code:gsub(' ', '+') or ('0000' .. (code or )):sub(-4) output = output .. ' || ' .. _multiUTF{codes or '0', 'hex2dec'} .. ' || ' .. (infos and 'U+%s' or 'U+%s'):format(code, code) end output = output .. '\n|-\n| UTF-8' for i, n in ipairs(names) do output = output .. _multiUTF{codes} end local outsideBMP = false -- Do we need to show surrogate pairs? for i, n in ipairs(names) do if (tonumber(codes or , 16) or 0) > 0xFFFF then outsideBMP = true break end end if outsideBMP then output = output .. '\n|-\n| UTF-16' for i, n in ipairs(names) do output = output .. _multiUTF{codes, 'UTF-16'} end end if yesno(args['IncludeGB']) then output = output .. '\n|-\n| GB 18030' for i, n in ipairs(names) do output = output .. _multiUTF{codes, 'GB18030'} end end output = output .. '\n|-\n| Numeric character reference' for i, n in ipairs(names) do output = output .. _multiUTF{codes, 'charmap/numcharref'} end return output end p.named = function(frame, n) n = n or frame.args[1] local args = frame:getParent.args local refchars = {} local empty = true; local namedref = (n 'html') and 'Named character reference' or args['namedref' .. n] or ; for i 1, 10 do if args['name' .. i] and args['name' .. i] ~ then local x (n 'html') and (numcr2namecr[tonumber(args, 16)] or ) or args['ref' .. n .. 'char' .. i] or empty = empty and #x == 0 refchars[1 + #refchars] = x end end if empty then return end return '|- style="text-align:center"\n| ' .. namedref .. (' || colspan="2" | %s'):rep(#refchars):format(unpack(refchars)) .. '\n' end -- Draw a preview block, possibly with multiple styles function preview(args, i) local result = for j = 1, math.huge do local label = paramCoalesce(args, 'style' .. i .. '-' .. j .. 'label', 'style' .. j .. 'label') if not label then break end local div = mw.html.create('div'):tag('div') div:tag('span'):wikitext(label) div:tag('span') :cssText(args['style' .. j]):cssText(args['style' .. i .. '-' .. j]) :wikitext(paramCoalesce(args, 'image' .. i .. '-' .. j, 'image' .. i) or _multiUTF{args or '20', 'charmap/showchar'}) result = result .. tostring(div) end -- Setting the font-size on the cell changes its padding. local size = (result ) and (paramCoalesce(args, 'size' .. i, 'size') or '150%') or '100%' local cell ' || colspan "2" style "font-size:' .. size .. '" | ' if result then return cell .. (paramCoalesce(args, 'image' .. i) or _multiUTF{args or '20', 'charmap/showchar'}) else return cell .. end end p.main = function(frame) -- text-align:center: local output = {'' end return p