模組:module categorization

呢個模組嘅解說可以喺模組:module categorization/doc度開

local export = {}

local categoryKeywords = {
	common = "Utility",
	utilities = "Utility",
	headword = "Headword-line",
	translit = "Transliteration",
	decl = "Inflection",
	conj = "Inflection",
	pronun = "Pronunciation",
	pronunc = "Pronunciation",
	pronunciation = "Pronunciation",
	IPA = "Pronunciation",
	sortkey = "Sortkey-generating",
}

-- returnTable set to true makes function return table of categories with
-- "[[Category:" and "]]" stripped away. It is used by [[Module:documentation]].
function export.categorize(frame, returnTable)
	local title = mw.title.getCurrentTitle()
	local subpage = title.subpageText
	
	-- To ensure no categories are added on documentation pages.
	if subpage == "documentation" then
		return ""
	end
	
	local output, categories = {}, {}
	local namespace = title.nsText
	local pagename, mode
	
	if frame.args[1] then
		pagename = frame.args[1]
		pagename = pagename:gsub("^Module:", "")
		mode = "testing"
		mw.log("arg", pagename)
	else
		if namespace ~= "模組" then
			error("This template should only be used in the Module namespace.")
		end
		
		pagename = title.text
		
		if subpage ~= pagename then
			pagename = title.rootText
		end
	end
	
	--[[
		If this is a transliteration module, parameter 1 is used as the code,
		rather than the code in the page title.
	]]
	local code, categoryKeyword = pagename:match("([-%a]+)[- ]([^/]+)$")
	
	if not code then
		error("Category name was not recognized.")
	end
	
	local lang, sc
	
	if subpage == "sandbox" then
		table.insert(categories, "Sandbox modules")
	else
		local category = categoryKeywords[categoryKeyword]
		if category == "Transliteration" then
			code = frame:getParent().args[1] or code
		end
		
		if code then
			if category then
				local getByCode = require("Module:languages").getByCode
				lang = getByCode(code) or getByCode(code .. "-pro")
				
				if category == "Transliteration" then
					if not lang then
						sc = require("Module:scripts").getByCode(code)
						
						if sc then
							table.insert(categories, "Transliteration modules by script|" .. sc:getCanonicalName())
						else
							error('The language or script code "' .. code .. '" in the page title is not recognized by [[Module:languages]] or [[Module:scripts]].')
						end
					end
				end
				
				if not ( sc or lang ) then
					error('The language code "' .. code .. '" in the page title is not recognized by Module:languages.')
				end
				
				local function languageCategory(lang, sortkey)
					return lang:getCanonicalName() .. " modules|" .. sortkey
				end
				
				local function generalCategory(category, sortkey)
					return category .. " modules|" .. sortkey
				end
				
				if category == "Transliteration" then
					local langs = require("Module:languages/byTranslitModule")(pagename)
					
					local sortkey = category
					
					if sc then
						sortkey = sortkey .. ", " .. sc:getCanonicalName()
					end
					
					if langs[1] then
						for i, lang in ipairs(langs) do
							table.insert(categories, languageCategory(lang, sortkey))
						end
					elseif lang then
						table.insert(categories, languageCategory(lang, sortkey))
					end
					
					if sc then
						table.insert(categories, generalCategory(category, sc:getCanonicalName()))
					else
						table.insert(categories, generalCategory(category, lang:getCanonicalName()))
					end
				else
					table.insert(categories, languageCategory(lang, category))
					table.insert(categories, generalCategory(category, lang:getCanonicalName()))
				end
			else
				error('The category keyword "' .. categoryKeyword .. '" was not recognized.')
			end
		end
	end
	
	if returnTable then
		return categories
	else
		categories = table.concat(
			require "Module:fun".map(
				function (category)
					return "[[Category:" .. category .. "]]"
				end,
				categories))
	end
	
	if testing then
		table.insert(output, pagename)
		
		if categories == "" then
			categories = '<span class="error">failed to generate categories for ' .. pagename .. '</span>'
		else
			categories = mw.ustring.gsub(categories, "%]%]%[%[", "]]\n[[")
			categories = frame:extensionTag{ name = "syntaxhighlight", content = categories }
		end
	end
	
	return table.concat(output) .. categories
end

return export