Difference between revisions 18626592 and 18626593 on frwiktionary

local export = {}

-- Detect the script based on the first alphabetical characters of a string
function export.detect_script(text, lang)
	local m_scripts = mw.loadData("Module:scripts/tempdata")
	local scripts = mw.loadData("Module:languages/alldata")[lang:getCode()].scripts
	local scFix = false
	
	-- Does this language have more than one script?
	-- If not, we can bypass the detection for a speed bonus.
	-- But always do the detection if the script is "None" or "Zyyy"
	if not (scripts[2] or scripts[1] == "None" or scripts[1] == "Zyyy") then
		return scripts[1], scFix
	end
	
	for i, script in ipairs(scripts) do
		local script2check = script:gsub(".-%-", "") -- removes the language code from script name, e.g. "nv-Latn" > "Latn"
		if script2check == "Latf" or script2check == "Latinx" or script2check == "unicode" then
			script2check = "Latn"
		elseif script2check == "Hans" or script2check == "Hant" then
			script2check = "Hani"
	lang:getScripts()
	
	-- Try to match every script against the text,
	-- and return the one with the most matching characters.
	local bestcount = 0
	local bestscript = nil
	
	for i, script in ipairs(scripts) do
		local count = script:countCharacters(text)
		
		if count > bestcount then
			bestcount = count
			bestscript = script
		end
	end
			
		local scriptinfo = m_scripts[script2check] or error("The script code \"" .. script .. "\" is not valid.")
		if scriptinfo.characters and mw.ustring.match(text, "[%[%d%p%s]-[" .. scriptinfo.characters .. "]") then
	if bestscript then
		-- TODO: return the object itself, not the code
		return bestscript, scFix
		end
	end
	
	scFix = (scripts[1] ~= "Zyyy" and scripts[1] ~= "None")
	
	-- not written in native script(s); check for all scripts
	-- TODO: This is slow; we really shouldn't be doing this!
	for script, scriptinfo in pairs(m_scripts) do
		if scriptinfo.characters and mw.ustring.match(text, "[%[%d%p%s]-[" .. scriptinfo.characters .. "]") then
			return script, scFix
		end
	end
	
	return scripts[1], scFix:getCode()
	end
	
	-- No matching script was found. Return "None".
	return "None" --require("Module:scripts").getScriptByCode("None")
end

-- Format the categories with the appropriate sort key
function export.format_categories(categories, lang, sort_key, sort_base)
	NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
	
	if NAMESPACE == "" or NAMESPACE == "Appendix" then
(contracted; show full)		i = i + 1
		cat = args[i]
	end
	
	return export.format_categories(categories, lang, sort_key)
end

return export