Pergi ke kandungan

Modul:list of scripts

Daripada Wikikamus
local export = {}
local filters = {}

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 table.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_scripts   = require("Module:scripts/data")
	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)
	for code, data in pairs(m_languages) do
		checkData = type(data[4]) == "table" and data[4] or type(data[4]) == "string" and split(data[4], "%s*,%s*") or {"None"}
		if m_table.size(checkData) ~= m_scripts_size then
			for _, sc in ipairs(checkData) do
				table.insert(script_to_langs[sc], code)
			end
		end
	end
	
	local rows = {}
	for code, data in m_table.sortedPairs(m_scripts, compare_codes) do
		if (not filter) or filter(code, data, args) then
			local canonicalName = data[1]
			local namecol = ""
			if code == "None" then
				namecol = canonicalName
			else
				local catname = canonicalName .. ((canonicalName:find("^[Tt]ulisan") or canonicalName:find("^[Kk]od") or canonicalName:find("[Ss]emafora")) and "" or "Tulisan ")
				catname = catname:gsub("^.", string.upper)
				namecol = "[[:Kategori:" .. catname .. "|" .. canonicalName .. "]]"
			end
			
			local lang_count = #script_to_langs[code]

			table.insert(rows, string.format([=[
	id="%s"
| <code>%s</code>
| %s
| %s
| %s
| %s
]=],
				code, code,
				namecol,
				data.otherNames and table.concat(data.otherNames, ", ") or "",
				data.characters and "Yes" or "",
				lang_count > 0 and
					(' title="%s" | [[:Kategori:Bahasa-bahasa tulisan %s|%i]]'):format(
						print_langs(script_to_langs[code], lang_count),
						canonicalName,
						lang_count)
					or ("%i"):format(lang_count)))
		end
	end
	
	return [[
{| class="wikitable sortable"
! Kod
! Nama berkanun
! Nama lain
! Autokesan
! Bahasa-bahasa
|-]] .. table.concat(rows, "\n|-") .. [[

|}]]
end

return export