Jump to content

Module:Unicode data/scripts/make

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 21:56, 11 March 2019 (wrong variable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local scripts_txt = "some Wikipedia page"
local property_value_aliases_txt = "some Wikipedia page"

local function pattern_escape(str)
	return (str:gsub('%p', '%%%1'))
end

function p.make_script_name_to_code(page_name)
	local content = mw.title.new(page_name):getContent()

	local script_aliases = property_value_aliases:match(
		pattern_escape '# Script (sc)'
		.. '%s+(.-)%s+'
		.. pattern_escape '# Script_Extensions (scx)')

	local script_name_to_code = {}
	
	for code, name in script_aliases:gmatch 'sc%s+;%s+(%a+)%s+;%s+([%a_]+)' do
		script_name_to_code[name] = code
	end
	
	return script_name_to_code
end

function p.main(frame)
	local script_name_to_code = p.make_script_name_to_code()
	setmetatable(script_name_to_code, { __index = function (self, k)
		error(('No code for "%s"'):format(k))
	end })
	
	local script_ranges = {}
	
	local prev_codepoint, prev_script_name, prev_script_range
	for codepoint1, codepoint2, script_name in script_data:gmatch '%f[^\n%z](%x+)%.?%.?(%x*)%s+;%s*([%w_]+)' do
		codepoint1, codepoint2 = tonumber(codepoint1, 16), tonumber(codepoint2, 16)
		local script_range
		if prev_script_range and script_name == prev_script_name and codepoint1 - prev_codepoint == 1 then
			prev_script_range[2] = codepoint2 or codepoint1
		else
			script_range = { codepoint1, codepoint2 or codepoint1, script_name_to_code[script_name] }
			table.insert(script_ranges, script_range)
		end
		prev_codepoint, prev_script_name, prev_script_range =
			codepoint2 or codepoint1, script_name, script_range or prev_script_range
	end
	
	local individual = {}
	local i = 1
	
	while script_ranges[i] do
		local low, high, script_code = table.unpack(script_ranges[i])
		if low == high then
			individual[low] = script_code
			table.remove(script_ranges, i)
		else
			i = i + 1
		end
	end
	
	table.sort(script_ranges,
		function (range1, range2)
			return range1[1] < range2[1]
		end)
	
	local template = [[
local data = {
	singles = {
...
	},
	
	ranges = {
...
	},
	-- Scripts.txt gives full names; here we consider them aliases to save space.
	aliases = {
...
	},
}
]]
	
	local printed_ranges = {}
	for _, range in ipairs(script_ranges) do
		local low, high, script_code = table.unpack(range)
		table.insert(printed_ranges, ('\t{ 0x%05X, 0x%05X, "%s" },'):format(low, high, script_code))
	end
	
	local printed_individual = {}
	for codepoint, script_code in require 'Module:table'.sortedPairs(individual) do
		table.insert(printed_individual, ('\t[0x%05X] = "%s",'):format(codepoint, script_code))
	end
	
	local printed_script_name_to_code = {}
	for name, code in require 'Module:table'.sortedPairs(script_name_to_code) do
		table.insert(printed_script_name_to_code, ('%s = "%s",'):format(code, name:gsub('_', ' ')))
	end
	
	local data = template
		:gsub('%.%.%.', table.concat(printed_ranges, '\n'), 1)
		:gsub('%.%.%.', table.concat(printed_individual, '\n'), 1)
		:gsub('%.%.%.', table.concat(printed_script_name_to_code, '\n'), 1)
	
	return data
end

return p