Модуль:Character code

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

Реализация шаблона {{Character code}}.

Выдаёт для символа (или их последовательности) их коды в различных записях.

В настоящий момент модулю, помимо строки, можно передать способ кодирования (сейчас — unicode / numeric; по умолчанию — unicode) и репрезентацию (dec / hex; по умолчанию — dec).

  • {{#invoke:Character code|main|β|unicode|dec}} → 946
  • {{#invoke:Character code|main|β|unicode|hex}} → U+3B2
  • {{#invoke:Character code|main|β|numeric|dec}} → β
  • {{#invoke:Character code|main|β|numeric|hex}} → β

  • {{#invoke:Character code|main|ⱱ̟|unicode|dec}} → 11377 799
  • {{#invoke:Character code|main|ⱱ̟|unicode|hex}} → U+2C71 U+31F
  • {{#invoke:Character code|main|ⱱ̟|numeric|dec}} → ⱱ̟
  • {{#invoke:Character code|main|ⱱ̟|numeric|hex}} → ⱱ̟

  • {{#invoke:Character code|main|ʎ̥˔|unicode|dec}} → 654 805 724
  • {{#invoke:Character code|main|ʎ̥˔|unicode|hex}} → U+28E U+325 U+2D4
  • {{#invoke:Character code|main|ʎ̥˔|numeric|dec}} → ʎ̥˔
  • {{#invoke:Character code|main|ʎ̥˔|numeric|hex}} → ʎ̥˔
local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local char = args[1]
	local encoding = args[2] or 'unicode'
	local representation = args[3] or 'dec'
	
	local code = {}
	for codepoint in mw.ustring.gcodepoint(char) do
		table.insert(code, codepoint)
	end
	
	local s = ''
	encoding = encoding:sub(1, 7)
	representation = representation:sub(1, 3)
	if representation == 'dec' then
		if encoding == 'unicode' then
			s = table.concat(code, ' ')
		elseif encoding == 'numeric' then
			for i, v in ipairs(code) do
				s = s .. '&#' .. v .. ';'
			end
		end
	elseif representation == 'hex' then
		for i, v in ipairs(code) do
			if encoding == 'unicode' then
				s = s .. 'U+' .. string.format('%X', v) .. ' '
			elseif encoding == 'numeric' then
				s = s .. '&#x' .. string.format('%X', v) .. ';'
			end
		end
	end
	
	return mw.text.trim(s)
end

return p