Difference between revisions of "Module:Unichar"

From blackwiki
Jump to navigation Jump to search
blackwiki>Erutuon
(parent args, for Template:unichar/sandbox)
blackwiki>Erutuon
(highlight spaces in light blue)
Line 9: Line 9:
 
return error(string.format(level, ...), 2)
 
return error(string.format(level, ...), 2)
 
end
 
end
 +
end
 +
 +
-- from [[Template:Unichar]]
 +
local styles = {
 +
smallcaps = 'class="smallcaps" style="font-variant: small-caps; font-size: smaller;"',
 +
monospace = 'style="font-family: monospace, monospace;"', -- why repeat it?
 +
background_color = 'style="background: lightblue;"', -- for space characters
 +
}
 +
local function style(text, type)
 +
if not styles[type] then
 +
errorf("Style %s not recognized", type)
 +
end
 +
return ('<span %s>%s</span>'):format(styles[type], text)
 
end
 
end
  
Line 16: Line 29:
 
local printed_codepoint = U(codepoint)
 
local printed_codepoint = U(codepoint)
 
if mw.ustring.toNFC(printed_codepoint) ~= printed_codepoint then
 
if mw.ustring.toNFC(printed_codepoint) ~= printed_codepoint then
 +
-- Prevent MediaWiki software from normalizing the character.
 
printed_codepoint = ("&#x%X;"):format(codepoint)
 
printed_codepoint = ("&#x%X;"):format(codepoint)
 
end
 
end
 
if Unicode_data.is_combining(codepoint) then
 
if Unicode_data.is_combining(codepoint) then
 
printed_codepoint = "◌" .. printed_codepoint
 
printed_codepoint = "◌" .. printed_codepoint
 +
end
 +
if Unicode_data.is_whitespace(codepoint) then
 +
printed_codepoint = style(printed_codepoint, "background_color")
 
end
 
end
 
return printed_codepoint
 
return printed_codepoint
Line 25: Line 42:
 
return ""
 
return ""
 
end
 
end
end
 
 
-- from [[Template:Unichar]]
 
local styles = {
 
smallcaps = 'class="smallcaps" style="font-variant: small-caps; font-size: smaller;"',
 
monospace = 'style="font-family: monospace, monospace;"', -- why repeat it?
 
}
 
local function style(text, type)
 
if not styles[type] then
 
errorf("Style %s not recognized", type)
 
end
 
return ('<span %s>%s</span>'):format(styles[type], text)
 
 
end
 
end
  

Revision as of 03:26, 8 July 2018

Tests

  • U+00A9 © COPYRIGHT SIGN
  • U+0378 <reserved-0378>
  • U+AC00HANGUL 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

 -- from [[Template:Unichar]]
local styles = {
	smallcaps = 'class="smallcaps" style="font-variant: small-caps; font-size: smaller;"',
	monospace = 'style="font-family: monospace, monospace;"', -- why repeat it?
	background_color = 'style="background: lightblue;"', -- for space characters
}
local function style(text, type)
	if not styles[type] then
		errorf("Style %s not recognized", type)
	end
	return ('<span %s>%s</span>'):format(styles[type], text)
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
			-- Prevent MediaWiki software from normalizing the character.
			printed_codepoint = ("&#x%X;"):format(codepoint)
		end
		if Unicode_data.is_combining(codepoint) then
			printed_codepoint = "◌" .. printed_codepoint
		end
		if Unicode_data.is_whitespace(codepoint) then
			printed_codepoint = style(printed_codepoint, "background_color")
		end
		return printed_codepoint
	else
		return ""
	end
end

local function u_plus(codepoint)
	return ("U+%04X"):format(codepoint)
end

function p.unichar(frame)
	local args = frame.args[1] and frame.args or frame:getParent().args
	
	local codepoint = args[1]
	if type(codepoint) ~= "string" then
		errorf("hexadecimal number expected, got %s", type(codepoint))
	end
	codepoint = codepoint and tonumber(codepoint, 16)
		or errorf("hexadecimal number expected, got %q", codepoint)
	
	local name_or_label = Unicode_data.lookup_name(codepoint)
	local is_label = name_or_label:sub(1, 1) == "<"
	
	return ("%s %s %s"):format(
		style(u_plus(codepoint), "monospace"),
		show(codepoint),
		is_label and name_or_label or style(name_or_label, "smallcaps"))
end

return p