Difference between revisions of "Module:Unichar"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(rudimentary Template:Unichar-ish function)
 
blackwiki>Erutuon
(adapted version of show function from Module:Unicode data/testcases)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 +
 +
local Unicode_data = require "Module:Unicode data"
  
 
local function errorf(level, ...)
 
local function errorf(level, ...)
Line 6: Line 8:
 
else -- level is actually the format string.
 
else -- level is actually the format string.
 
return error(string.format(level, ...), 2)
 
return error(string.format(level, ...), 2)
 +
end
 +
end
 +
 +
local U = mw.ustring.char
 +
local function show(codepoint)
 +
if Unicode_data.is_printable(codepoint) then
 +
local printed_codepoint = U(codepoint)
 +
if mw.ustring.toNFC(printed_codepoint) ~= printed_codepoint then
 +
printed_codepoint = ("&#x%X;"):format(codepoint)
 +
end
 +
if Unicode_data.is_combining(codepoint) then
 +
printed_codepoint = "◌" .. printed_codepoint
 +
end
 +
return ("%s"):format(printed_codepoint)
 +
else
 +
return ""
 
end
 
end
 
end
 
end
Line 11: Line 29:
 
function p.unichar(frame)
 
function p.unichar(frame)
 
local codepoint = frame.args[1]
 
local codepoint = frame.args[1]
codepoint = tonumber(codepoint)
+
codepoint = tonumber(codepoint, 16)
 
or errorf("hexadecimal number expected, got '%s'", codepoint)
 
or errorf("hexadecimal number expected, got '%s'", codepoint)
 
 
return ("U+%04X %#x%X; %s"):format(codepoint, codepoint,
+
return ("U+%04X %s %s"):format(codepoint, show(codepoint),
require "Module:Unicode data".lookup_name(codepoint))
+
Unicode_data.lookup_name(codepoint))
 
end
 
end
  
 
return p
 
return p

Revision as of 05:02, 29 June 2018

Tests

  • U+00A9 © COPYRIGHT SIGN
  • U+0378 <reserved-0378>
  • U+AC00 가 HANGUL SYLLABLE GA



local p = {}

local Unicode_data = require "Module:Unicode data"

local function errorf(level, ...)
	if type(level) == number then
		return error(string.format(...), level + 1)
	else -- level is actually the format string.
		return error(string.format(level, ...), 2)
	end
end

local U = mw.ustring.char
local function show(codepoint)
	if Unicode_data.is_printable(codepoint) then
		local printed_codepoint = U(codepoint)
		if mw.ustring.toNFC(printed_codepoint) ~= printed_codepoint then
			printed_codepoint = ("&#x%X;"):format(codepoint)
		end
		if Unicode_data.is_combining(codepoint) then
			printed_codepoint = "◌" .. printed_codepoint
		end
		return ("%s"):format(printed_codepoint)
	else
		return ""
	end
end

function p.unichar(frame)
	local codepoint = frame.args[1]
	codepoint = tonumber(codepoint, 16)
		or errorf("hexadecimal number expected, got '%s'", codepoint)
	
	return ("U+%04X %s %s"):format(codepoint, show(codepoint),
		Unicode_data.lookup_name(codepoint))
end

return p