local export = {}
setmetatable(_G, { __index = function (self, k)
if k == "Unicode_data" then
local val = require "Module:Unicode data"
self = val
return val
end
end })
local function unique_matches(pattern, text)
local matches = {}
for match in text:gmatch(pattern) do
matches = true
end
local list = require "Module:array"()
for match in pairs(matches) do
list:insert(match)
end
list:sort(require "Module:collation".laborious_comp)
return list
end
local function superscript_edit_link(title)
return "<sup class=\"plainlinks\">]</sup>"
end
local function display_datum(title, jsonl)
local output = require "Module:array"()
output:insert("; ] " .. superscript_edit_link(title) .. "\n")
for json in jsonl:gmatch "+" do
local data = mw.text.jsonDecode(json)
output:insert("* <code>" .. mw.text.nowiki(data.template) .. "</code>"
.. "<br>incorrect characters in <code>" .. mw.text.nowiki(data.tested_string)
.. "</code>\n")
for _, incorrect in ipairs(data.incorrect) do
output:insert("** <code>" .. mw.text.nowiki(incorrect.char) .. "</code>: "
.. incorrect.name .. "\n")
end
end
return output:concat()
end
local function iter_chars(str)
return mw.ustring.gmatch(str, "()(.)")
end
local function all_matches(str, pattern)
local matches = require "Module:array"()
for match in str:gmatch(pattern) do
matches:insert(match)
end
return matches
end
local function display_datum_new(json)
local output = require "Module:array"()
local ok, data = pcall(mw.text.jsonDecode, json)
if not ok then
mw.log(json)
return ""
end
output:insert("; ] " .. superscript_edit_link(data.title) .. "\n")
for _, template in ipairs(data.templates) do
output:insert("* <code>" .. mw.text.nowiki(template.template) .. "</code>"
.. "<br>incorrect characters in <code>" .. mw.text.nowiki(template.text)
.. "</code>:")
if type(template.chars) == "string" and all_matches(template.text, "%f%a+%f"):concat "" == template.chars then
output:insert(" <code>" .. template.chars .. "</code>\n")
else
output:insert("\n")
for _, incorrect in (type(template.chars) == "string" and iter_chars or ipairs)(template.chars) do
local char, name
if type(incorrect) == "string" then
char = incorrect
name = Unicode_data.lookup_name(mw.ustring.codepoint(char))
else
char = incorrect.char
name = incorrect.name
end
output:insert("** <code>" .. mw.text.nowiki(char) .. "</code>: "
.. name .. "\n")
end
end
end
return output:concat()
end
local function display_data(data, new)
if new then
return (data:gsub("+", display_datum_new))
else
return (unique_matches(
"%f\t.-%f",
data)
:concat()
:gsub("\t(+)\n(.-)%f", display_datum))
end
end
local function get_format(json)
local ok, data = pcall(mw.text.jsonDecode, json)
local is_new_format = ok and data.title ~= nil
return is_new_format
end
function export.show(frame)
local title = mw.title.getCurrentTitle().fullText
local data_subpage = title .. "/data"
local data = frame.args and mw.text.trim(mw.text.unstripNoWiki(frame.args))
or mw.title.new(data_subpage):getContent():match("<pre><nowiki>(.+)</nowiki></pre>")
local match = data:match("new format\n(.+)")
data = match or data
local is_new_format = get_format(data:match "+")
return display_data(data, is_new_format)
end
return export