Jump to content

Module:list of scripts

ពីWiktionary

Documentation for this module may be created at Module:list of scripts/doc

local export = {}
local filters = {}

function export.show(frame)
	local args = frame.args
	local filter = filters[args[1]]
	local m_scripts = mw.loadData("Module:scripts/data")

	local langs = {}
	local m_languages = mw.loadData("Module:languages/alldata")

	for code, _ in pairs(m_scripts) do
		langs[code] = {}
	end 

	for code, data in pairs(m_languages) do
		for i, sc in ipairs(data.scripts or {"None"}) do
			local sdata = langs[sc] or {}
			table.insert(sdata, code)
		end
	end

	local codes = {}
 
	for code, _ in pairs(m_scripts) do
		table.insert(codes, code)
	end 
	table.sort(codes, function (apple, orange)
		if apple == "None" then
			return true
		elseif orange == "None" then
			return false
		end
		local al, as =  apple:match("^(%w+)%-(%w+)$")
		local ol, os = orange:match("^(%w+)%-(%w+)$")
		if al then apple  = as .. '-' .. al end
		if ol then orange = os .. '-' .. ol end
		return apple < orange
	end)
 
	local rows = {}
	for i, code in ipairs(codes) do
		local data = m_scripts[code]

		local rt = nil
		if data.otherNames then
			local i = 1
			rt = {}
			while data.otherNames[i] do
				table.insert(rt, data.otherNames[i])
				i = i + 1
			end
		end

		if (not filter) or filter(code, data, args) then
			local namecol = ""
			if code == "None" then
				namecol = data.canonicalName
			else
				local catname = data.canonicalName .. (data.canonicalName:find("[Ss]cript$") and "" or " script")
				catname = catname:sub(1, 1):upper() .. catname:sub(2)
				namecol = "[[:Category:" .. catname .. "|" .. data.canonicalName .. "]]"
			end

			table.insert(rows, string.format(
				' id="%s"\n' ..
				'| <code>%s</code>\n' ..
				'| %s\n' ..
				'| %s\n' ..
				'| %s\n' ..
				'| %u\n',
				code, code,
				namecol,
				rt and table.concat(rt, ", ") or "&nbsp;",
				data.characters and "Yes" or "",
				#langs[code]
			))
		end
	end

	return
		"{| class=\"wikitable sortable\"\n" ..
		"! Code\n" ..
		"! Canonical name\n" ..
		"! Other names\n" ..
		"! Autodetection\n" ..
		"! Languages\n" ..
		"|-" .. table.concat(rows, "\n|-") .. "\n|}"
end

return export