48,357
edits
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local | local m_str_utils = require("Module:string utilities") | ||
local cp = m_str_utils.codepoint | |||
local gcodepoint = m_str_utils.gcodepoint | |||
local gsub = string.gsub | |||
local u = m_str_utils.char | |||
local export = {} | |||
local udata = mw.loadData("Module:Unicode data/data") | |||
local floor = math.floor | local floor = math.floor | ||
local function errorf( | local function errorf(first_arg, ...) | ||
if type( | if type(first_arg) == "number" then | ||
return error(string.format(...), | return error(string.format(...), first_arg + 1) | ||
else | else | ||
return error(string.format( | return error(string.format(first_arg, ...), 2) | ||
end | end | ||
end | end | ||
| Line 13: | Line 21: | ||
local function binary_range_search(codepoint, ranges) | local function binary_range_search(codepoint, ranges) | ||
local low, mid, high | local low, mid, high | ||
low, high = 1, ranges.length or require "Module: | low, high = 1, ranges.length or require "Module:table".length(ranges) | ||
while low <= high do | while low <= high do | ||
mid = floor((low + high) / 2) | mid = floor((low + high) / 2) | ||
| Line 27: | Line 35: | ||
return nil, mid | return nil, mid | ||
end | end | ||
export.binary_range_search = binary_range_search | |||
local function linear_range_search(codepoint, ranges) | local function linear_range_search(codepoint, ranges) | ||
for i, range in ipairs(ranges) do | for i, range in ipairs(ranges) do | ||
if range[1] | if codepoint < range[1] then | ||
break | |||
elseif codepoint <= range[2] then | |||
return range | return range | ||
end | end | ||
end | end | ||
end | end | ||
-- Load a module by indexing "loader" with the name of the module minus the | -- Load a module by indexing "loader" with the name of the module minus the | ||
| Line 57: | Line 65: | ||
-- see "Hangul Syllable Name Generation" in section 3.12 of the | -- see "Hangul Syllable Name Generation" in section 3.12 of the | ||
-- Unicode Specification: | -- Unicode Specification: | ||
-- https://www.unicode.org/versions/ | -- https://www.unicode.org/versions/latest/ch03.pdf | ||
-- For most of the name rules given here, see the subsection | |||
-- "Unicode Name Property" in section 4.8 (Name) and the table 4-8 | |||
-- (Name Derivation Rule Prefix Strings): | |||
-- https://www.unicode.org/versions/latest/ch04.pdf | |||
local name_hooks = { | local name_hooks = { | ||
{ | { 0x0000, 0x001F, "<control-%04X>" }, -- C0 control characters | ||
{ | { 0x007F, 0x009F, "<control-%04X>" }, -- DEL and C1 control characters | ||
{ 0x3400, 0x4DBF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension A | { 0x3400, 0x4DBF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension A | ||
{ 0x4E00, 0x9FFF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph | { 0x4E00, 0x9FFF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph | ||
| Line 80: | Line 92: | ||
{ 0xF900, 0xFA6D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0xF900, 0xFA6D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0xFA70, 0xFAD9, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0xFA70, 0xFAD9, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0x17000, 0x187F7, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | { 0xFDD0, 0xFDEF, "<noncharacter-%04X>" }, | ||
{ 0xFE00, 0xFE0F, function (codepoint) -- Variation Selectors Supplement | |||
return ("VARIATION SELECTOR-%d"):format(codepoint - 0xFE00 + 1) | |||
end}, | |||
{ 0x13460, 0x143FA, "EGYPTIAN HIEROGLYPH-%04X" }, -- Egyptian Hieroglyphs Extended-A | |||
{ 0x17000, 0x187F7, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | |||
{ 0x18800, 0x18AFF, function (codepoint) | { 0x18800, 0x18AFF, function (codepoint) | ||
return ("TANGUT COMPONENT-%03d"):format(codepoint - 0x187FF) | return ("TANGUT COMPONENT-%03d"):format(codepoint - 0x187FF) | ||
end }, | end }, | ||
{ 0x18D00, 0x18D08, "TANGUT IDEOGRAPH-%04X" }, -- Tangut | { 0x18B00, 0x18CD5, "KHITAN SMALL SCRIPT CHARACTER-%04X" }, -- Khitan Small Script | ||
{ 0x18CFF, 0x18CFF, "KHITAN SMALL SCRIPT CHARACTER-%04X" }, | |||
{ 0x18D00, 0x18D08, "TANGUT IDEOGRAPH-%04X" }, -- Tangut Supplement | |||
{ 0x1B170, 0x1B2FB, "NUSHU CHARACTER-%04X" }, -- Nushu | { 0x1B170, 0x1B2FB, "NUSHU CHARACTER-%04X" }, -- Nushu | ||
{ 0x20000, 0x2A6DF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension B | { 0x20000, 0x2A6DF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension B | ||
| Line 91: | Line 110: | ||
{ 0x2B820, 0x2CEA1, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension E | { 0x2B820, 0x2CEA1, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension E | ||
{ 0x2CEB0, 0x2EBE0, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension F | { 0x2CEB0, 0x2EBE0, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension F | ||
{ 0x2EBF0, 0x2EE5D, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension I | |||
-- CJK Compatibility Ideographs Supplement (Supplementary Ideographic Plane) | -- CJK Compatibility Ideographs Supplement (Supplementary Ideographic Plane) | ||
{ 0x2F800, 0x2FA1D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | { 0x2F800, 0x2FA1D, "CJK COMPATIBILITY IDEOGRAPH-%04X" }, | ||
{ 0x30000, 0x3134A, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension G | |||
{ 0x31350, 0x323AF, "CJK UNIFIED IDEOGRAPH-%04X" }, -- CJK Ideograph Extension H | |||
{ 0xE0100, 0xE01EF, function (codepoint) -- Variation Selectors Supplement | { 0xE0100, 0xE01EF, function (codepoint) -- Variation Selectors Supplement | ||
return ("VARIATION SELECTOR-%d"):format(codepoint - 0xE0100 + 17) | return ("VARIATION SELECTOR-%d"):format(codepoint - 0xE0100 + 17) | ||
end}, | end}, | ||
{ 0xF0000, 0xFFFFD, "<private-use-%04X>" }, -- Plane 15 Private Use | { 0xF0000, 0xFFFFD, "<private-use-%04X>" }, -- Plane 15 Private Use | ||
{ 0x100000, 0x10FFFD, "<private-use-%04X>" } | { 0x100000, 0x10FFFD, "<private-use-%04X>" }, -- Plane 16 Private Use | ||
} | } | ||
name_hooks.length = #name_hooks | name_hooks.length = #name_hooks | ||
--[[ Add another - in this line to test the code point ordering in name_hooks. | |||
local i = 1 | |||
local function print_it(a, b, c) | |||
if type(c) == "string" then | |||
mw.log(c:format(a), c:format(b)) | |||
else | |||
mw.log(c(a), c(b)) | |||
end | |||
end | |||
while true do | |||
local first, second = name_hooks[i], name_hooks[i + 1] | |||
if not (first and second) then break end | |||
local message | |||
if not (first[1] < first[2] and first[2] < second[1] and second[1] < second[2]) then | |||
message = "Bad name label ordering at index " .. i .. ":" | |||
elseif second[1] == first[2] + 1 and second[3] == first[3] then | |||
message = "Name hooks can be merged at index " .. i .. ":" | |||
end | |||
if message then | |||
mw.log(message) | |||
print_it(unpack(first)) | |||
print_it(unpack(second)) | |||
end | |||
i = i + 1 | |||
end | |||
--]] | |||
local name_range_cache | local name_range_cache | ||
| Line 126: | Line 171: | ||
--]] | --]] | ||
function | -- https://www.unicode.org/versions/latest/ch04.pdf, section 4.8 | ||
function export.lookup_name(codepoint) | |||
-- U+FDD0-U+FDEF and all code points ending in FFFE or FFFF are Unassigned | -- U+FDD0-U+FDEF and all code points ending in FFFE or FFFF are Unassigned | ||
-- (Cn) and specifically noncharacters: | -- (Cn) and specifically noncharacters: | ||
-- https://www.unicode.org/faq/private_use.html#nonchar4 | -- https://www.unicode.org/faq/private_use.html#nonchar4 | ||
if codepoint >= 0xFFFE and floor(codepoint % 0x10000) >= 0xFFFE then | |||
return ("<noncharacter-%04X>"):format(codepoint) | return ("<noncharacter-%04X>"):format(codepoint) | ||
end | end | ||
| Line 167: | Line 206: | ||
end | end | ||
function | function export.lookup_image(codepoint) | ||
local data = loader[('images/%03X'):format(codepoint / 0x1000)] | local data = loader[('images/%03X'):format(codepoint / 0x1000)] | ||
| Line 175: | Line 214: | ||
end | end | ||
function export.lookup_image_emoji(codepoint) | |||
local data = loader[('emoji_images/%03X'):format(codepoint / 0x1000)] | |||
if data then | |||
return data[codepoint] | |||
end | |||
end | |||
-- Load [[Module:Unicode data/blocks]] if needed and assign it to this variable. | -- Load [[Module:Unicode data/blocks]] if needed and assign it to this variable. | ||
| Line 193: | Line 230: | ||
if data then | if data then | ||
-- Unpack doesn't work on tables loaded with mw.loadData. | -- Unpack doesn't work on tables loaded with mw.loadData. | ||
return i, data[ | return i, data[3], data[1], data[2] | ||
end | end | ||
end | end | ||
-- An ipairs-type iterator generator for the list of blocks. | -- An ipairs-type iterator generator for the list of blocks. | ||
function | function export.enum_blocks() | ||
local blocks = loader.blocks | local blocks = loader.blocks | ||
return block_iter, blocks, 0 | return block_iter, blocks, 0 | ||
end | end | ||
function | function export.get_block_range(name) | ||
for i, block in ipairs(loader.blocks) do | |||
if block[3] == name then | |||
return block[1], block[2] | |||
end | |||
end | |||
end | |||
function export.lookup_plane(codepoint) | |||
local i = floor(codepoint / 0x10000) | local i = floor(codepoint / 0x10000) | ||
return planes[i] or ("Plane %u"):format(i) | return udata.planes[i] or ("Plane %u"):format(i) | ||
end | end | ||
function | function export.lookup_block(codepoint) | ||
local blocks = loader.blocks | local blocks = loader.blocks | ||
local range = binary_range_search(codepoint, blocks) | local range = binary_range_search(codepoint, blocks) | ||
| Line 218: | Line 263: | ||
end | end | ||
function | function export.get_block_info(name) | ||
for i, block in ipairs(loader.blocks) do | for i, block in ipairs(loader.blocks) do | ||
if block[3] == name then | if block[3] == name then | ||
| Line 226: | Line 271: | ||
end | end | ||
function | function export.is_valid_pagename(pagename) | ||
local has_nonws = false | local has_nonws = false | ||
for | for codepoint in gcodepoint(pagename) do | ||
if ( | if (codepoint == 0x0023) -- # | ||
or ( | or (codepoint == 0x005B) -- [ | ||
or ( | or (codepoint == 0x005D) -- ] | ||
or ( | or (codepoint == 0x007B) -- { | ||
or ( | or (codepoint == 0x007C) -- | | ||
or ( | or (codepoint == 0x007D) -- } | ||
or ( | or (codepoint == 0x180E) -- MONGOLIAN VOWEL SEPARATOR | ||
or (( | or ((codepoint >= 0x2000) and (codepoint <= 0x200A)) -- spaces in General Punctuation block | ||
or ( | or (codepoint == 0xFFFD) -- REPLACEMENT CHARACTER | ||
then | then | ||
return false | return false | ||
end | end | ||
local printable, result = | local printable, result = export.is_printable(codepoint) | ||
if not printable then | if not printable then | ||
return false | return false | ||
| Line 287: | Line 332: | ||
-- already been found to match, or a range whose data is the default if there | -- already been found to match, or a range whose data is the default if there | ||
-- was no match. | -- was no match. | ||
local function | local function codepoint_lookup(data_module_subpage, match_func, ...) | ||
local dots = { ... } | local dots = { ... } | ||
local cache = {} | local cache = {} | ||
| Line 329: | Line 374: | ||
} | } | ||
end | end | ||
table.insert(cache, dots_range) | |||
table.sort(cache, compare_ranges) | table.sort(cache, compare_ranges) | ||
end | end | ||
return match_func(codepoint) | return match_func(codepoint, unpack(dots)) | ||
end | |||
end | |||
-- Return a character's combining class value from [[Module:Unicode data/combining classes]], | |||
-- or otherwise 0, which is treated as the default value. | |||
do | |||
local combining | |||
function export.combining_class(ch) | |||
combining = combining or mw.loadData("Module:Unicode data/combining classes") | |||
return combining[type(ch) == "number" and u(ch) or ch] or 0 | |||
end | end | ||
end | end | ||
-- | -- FIXME: Some combining characters have a combining class of 0, so this needs rethinking. | ||
function export.is_combining(ch) | |||
return export.combining_class(ch) ~= 0 | |||
end | |||
do | |||
local function dotted_circle(ch) | |||
if export.combining_class(ch) ~= 0 then | |||
return "◌" .. ch | |||
end | |||
end | |||
function export.add_dotted_circle(str) | |||
return (gsub(str, ".[\128-\191]*", dotted_circle)) | |||
end | |||
end | end | ||
local lookup_control = | local lookup_control = codepoint_lookup( | ||
"control", | "control", | ||
function (codepoint, ccc) | function (codepoint, ccc) | ||
| Line 364: | Line 416: | ||
end, | end, | ||
"assigned") | "assigned") | ||
export.lookup_control = lookup_control | |||
function | function export.is_assigned(codepoint) | ||
return lookup_control(codepoint) ~= "unassigned" | return lookup_control(codepoint) ~= "unassigned" | ||
end | end | ||
function | function export.is_printable(codepoint) | ||
local result = lookup_control(codepoint) | local result = lookup_control(codepoint) | ||
return (result == "assigned") or (result == "space-separator"), result | return (result == "assigned") or (result == "space-separator"), result | ||
end | end | ||
function | function export.is_whitespace(codepoint) | ||
local result = lookup_control(codepoint) | local result = lookup_control(codepoint) | ||
return (result == "space-separator"), result | return (result == "space-separator"), result | ||
end | end | ||
export.lookup_category = codepoint_lookup( | |||
"category", | "category", | ||
function (codepoint, category) | function (codepoint, category) | ||
return category | return category | ||
end, | end, | ||
"Cn") | "Cn" | ||
) | |||
function export.get_category_long_name(category) | |||
return loader["category"].long_names[category] | |||
end | |||
export.lookup_script = codepoint_lookup( | |||
"scripts", | "scripts", | ||
function (codepoint, | function (codepoint, script) | ||
return | return script | ||
end, | end, | ||
"Zzzz" | "Zzzz" | ||
) | |||
function export.get_script_alias(script) | |||
return loader["scripts"].aliases[script] | |||
function | |||
end | end | ||
function export.get_entry_title(codepoint) | |||
if udata.unsupported_title[codepoint] then | |||
return udata.unsupported_title[codepoint] | |||
function | |||
end | end | ||
if lookup_control(codepoint) ~= "assigned" then | |||
return nil | |||
return | |||
end | end | ||
return u(codepoint) | |||
end | end | ||
return | return export | ||