Jump to content

Module:list of scripts

From Wiktionary, the free dictionary

This module generates the list of scripts in Wiktionary:List of scripts.


local export = {}
local filters = {}

local m_scripts = require("Module:scripts")
local m_scripts_data = require("Module:scripts/data")

local concat = table.concat
local insert = table.insert
local remove = table.remove
local split = require("Module:string utilities").split

local m_table               = require("Module:table")
local language_code_to_name = require("Module:languages/code to canonical name")

-- fa-Arab -> Arab-fa
local function rearrange_lang_and_script(script_code)
	local lang, script = script_code:match("^([%w-]+)%-(%w+)$")
	if lang then script_code = script .. '-' .. lang end
	return script_code
end

-- Determines the order in which the scripts are shown in the resulting table.
local function compare_codes(apple, orange)
	-- "None" before all other scripts
	if apple == "None" then
		return true
	elseif orange == "None" then
		return false
	end
	
	-- Sort language-code-prefixed script codes immediately after unmodified ones:
	-- fa-Arab after Arab.
	return rearrange_lang_and_script(apple) < rearrange_lang_and_script(orange)
end

local function map_to_limit(func, t, limit)
	local new_t = {}
	for i = 1, limit do
		new_t[i] = func(t[i], i)
	end
	return new_t
end

local function get_canonical_name(code)
	return language_code_to_name[code] or "???"
end

local function sort_by_canonical_name(code1, code2)
	return get_canonical_name(code1) < get_canonical_name(code2)
end

local function sort_and_return(t, comp)
	table.sort(t, comp)
	return t
end

local function print_langs(langs, total)
	local limit = 20
	local suffix = ""
	if total > limit then
		suffix = ", ..."
	else
		limit = total
	end
	return concat(
		map_to_limit(
			get_canonical_name,
			sort_and_return(
				langs,
				sort_by_canonical_name),
			limit),
		", ") .. suffix
end

function export.show(frame)
	local args        = frame.args
	local filter      = filters[args[1]]
	local m_languages = require("Module:languages/data/all")

	local script_to_langs = setmetatable(
		{},
		{
			__index = function(self, key) -- Initialize fields in table with 0.
				local val = {}
				self[key] = val
				return val
			end
		})
	
	local m_scripts_size = m_table.size(m_scripts_data)
	for code, data in pairs(m_languages) do
		local checkData = type(data[4]) == "table" and data[4] or type(data[4]) == "string" and split(data[4], ",") or {"None"}
		if m_table.size(checkData) ~= m_scripts_size then
			for _, sc in ipairs(checkData) do
				insert(script_to_langs[sc], code)
			end
		end
	end
	
	local rows = {}
	for code, data in m_table.sortedPairs(m_scripts_data, compare_codes) do
		if (not filter) or filter(code, data, args) then
			local sc = m_scripts.makeObject(code, data)
			local names = sc:getAllNames()
			local canonicalName = remove(names, 1)
			local catname = sc:getCategoryName()
			
			local namecol = ""
			if code == "None" then
				namecol = canonicalName
			else
				namecol = "[[:Category:" .. catname .. "|" .. canonicalName .. "]]"
			end
			
			local lang_count = #script_to_langs[code]

			insert(rows, string.format([=[
	id="%s"
| <code>%s</code>
| %s
| %s
| %s
| %s
]=],
				code, code,
				namecol,
				#names > 0 and concat(names, ", ") or "&nbsp;",
				data.characters and "Yes" or "",
				lang_count > 0 and
					(' title="%s" | [[:Category:%s languages|%i]]'):format(
						print_langs(script_to_langs[code], lang_count),
						catname,
						lang_count)
					or ("%i"):format(lang_count)))
		end
	end
	
	return [[
{| class="wikitable sortable"
! Code
! Canonical name
! Other names
! Autodetection
! Languages
|-]] .. concat(rows, "\n|-") .. [[

|}]]
end

return export