Difference between revisions of "Module:Unicode data/age/make"
< Module:Unicode data | age
Jump to navigation
Jump to search
blackwiki>Erutuon (generate "age" data from DerivedAge.txt in the Unicode Character Database) |
blackwiki>Erutuon (fixed) |
||
| Line 33: | Line 33: | ||
]] | ]] | ||
| + | table.sort( | ||
| + | ranges, | ||
| + | function(range1, range2) | ||
| + | return range1[1] < range2[1] | ||
| + | end) | ||
| + | |||
| + | local function make_padding(age) | ||
| + | return (" "):rep(#'"12.1"' - (#age + 2)) | ||
| + | end | ||
| + | |||
local Array = require "Module:array" | local Array = require "Module:array" | ||
local printed_ranges = Array() | local printed_ranges = Array() | ||
for _, range in ipairs(ranges) do | for _, range in ipairs(ranges) do | ||
local low, high, age = unpack(range) | local low, high, age = unpack(range) | ||
| − | printed_ranges:insert(('\t\t{ 0x% | + | printed_ranges:insert(('\t\t{ 0x%06X, 0x%06X, %s%q },') |
| + | :format(low, high, make_padding(age), age)) | ||
end | end | ||
local printed_singles = Array() | local printed_singles = Array() | ||
| − | for codepoint in require 'Module:TableTools'.sortedPairs(singles) do | + | for codepoint, age in require 'Module:TableTools'.sortedPairs(singles) do |
| − | printed_singles:insert(('\t\t[0x% | + | printed_singles:insert(('\t\t[0x%06X] = %s%q,') |
| + | :format(codepoint, make_padding(age), age)) | ||
end | end | ||
Revision as of 18:36, 18 September 2019
Lua error at line 5: assertion failed!.
local p = {}
function p.main(frame)
local page = frame.args[1] or "User:Erutuon/Unicode/DerivedAge.txt"
local text = assert(mw.title.new(page):getContent())
local singles, ranges = {}, {}
for code_point1, code_point2, age in text:gmatch("%f[^\n%z](%x+)%.?%.?(%x*)%s*;%s*([%d.]+)") do
code_point1, code_point2 = tonumber(code_point1, 16), tonumber(code_point2, 16)
local last_range = ranges[#ranges]
if last_range and last_range[2] == code_point1 - 1 then
last_range[2] = code_point2 or code_point1
elseif singles[code_point1 - 1] then
singles[code_point1 - 1] = nil
table.insert(ranges, { code_point1 - 1, code_point2 or code_point1, age })
else
if not code_point2 then
singles[code_point1] = age
else
table.insert(ranges, { code_point1, code_point2, age })
end
end
end
local template = [[
return {
singles = {
...
},
ranges = {
...
},
}
]]
table.sort(
ranges,
function(range1, range2)
return range1[1] < range2[1]
end)
local function make_padding(age)
return (" "):rep(#'"12.1"' - (#age + 2))
end
local Array = require "Module:array"
local printed_ranges = Array()
for _, range in ipairs(ranges) do
local low, high, age = unpack(range)
printed_ranges:insert(('\t\t{ 0x%06X, 0x%06X, %s%q },')
:format(low, high, make_padding(age), age))
end
local printed_singles = Array()
for codepoint, age in require 'Module:TableTools'.sortedPairs(singles) do
printed_singles:insert(('\t\t[0x%06X] = %s%q,')
:format(codepoint, make_padding(age), age))
end
local data = template
:gsub('%.%.%.', printed_singles:concat('\n'), 1)
:gsub('%.%.%.', printed_ranges:concat('\n'), 1)
return mw.getCurrentFrame():extensionTag{
name = "syntaxhighlight",
content = data,
args = { lang = "lua" }
}
end
return p