Module:Char

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

Usage

char

char(…) takes a variable number of arguments. Each argument is a Unicode codepoint number, which may be expressed in either decimal or hexadecimal form (such as 0x1ec5). The function converts each codepoint number into the corresponding character and concatenates the characters. This function can be used as a workaround for the bug that this MediaWiki installation truncates page content at the first surrogate pair, affecting many emoji for instance. [1]

  • {{#invoke:char|char|0x1f3f3|0xfe0f|0x200d|0x1f308}} → 🏳️‍🌈

flag

flag(…) takes an ISO 3166-1 alpha-2 country code, other emoji flag sequence, or ISO 3166-2 region code and returns the corresponding emoji flag. Compared to {{Flagicon}}, this function is more accessible and has less impact on page loading time but potentially displays incorrectly on some devices. This function can be used to reduce the number of Wikimedia Commons images on the page, mitigating a bug where too many such images can cause the page to fail to load. [2]

  • {{#invoke:char|flag|GB}} → 🇬🇧
  • {{#invoke:char|flag|GB-WLS}} → 🏴󠁧󠁢󠁷󠁬󠁳󠁿
  • {{#invoke:char|flag|US}} → 🇺🇸
  • {{#invoke:char|flag|US-TX}} → 🏴󠁵󠁳󠁴󠁸󠁿
  • {{#invoke:char|flag|EU}} → 🇪🇺
  • {{#invoke:char|flag|UN}} → 🇺🇳

See also

local p = {}

function p.char(frame)
	local chars = {}
	for i, ch in ipairs(frame.args) do
		table.insert(chars, tonumber(ch))
	end
	if #chars < 1 then
		local template = frame:getParent()
		for i, ch in ipairs(template.args) do
			table.insert(chars, tonumber(ch))
		end
	end
	return mw.ustring.char(unpack(chars))
end

function p.flag(frame)
	local code = frame.args[1]
	if not code or #code < 1 then
		code = frame:getParent().args[1] or ""
	end
	code = mw.ustring.gsub(mw.ustring.upper(code), "-", "")
	local codepoints = {mw.ustring.codepoint(code, 1, -1)}
	local a = 0x1F1E6
	if #codepoints > 2 then
		a = 0xE0061
	end
	for i, codepoint in ipairs(codepoints) do
		codepoints[i] = a + (codepoint - 65)
	end
	if #codepoints > 2 then
		table.insert(codepoints, 1, 0x1F3F4)
		table.insert(codepoints, 0xE007F)
	end
	return mw.ustring.char(unpack(codepoints))
end

return p