Modul:module categorization

A modult a Modul:module categorization/doc lapon tudod dokumentálni

local export = {}

local categoryKeywords = {
	common = "Utility",
	utilities = "Utility",
	headword = "Címszó",
	translit = "Átírási",
	decl = "Ragozási",
	conj = "Ragozási",
	pronunciation = "Kiejtési",
	pronunc = "Kiejtési",
	IPA = "Kiejtési",
	sortkey = "Rendezőkulcs-generáló",
}

-- 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

	if subpage == "doc" 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("^Modul:", "")
		mode = "testing"
		mw.log("arg", pagename)
	else
		if namespace ~= "Modul" then
			error("Ez a sablon csak a modulnévtérben használható.")
		end
		
		pagename = title.text
		
		if subpage ~= pagename then
			pagename = title.rootText
		end
	end

	local args
	if frame.args.is_template then
		local params = {
			[1] = {},
			[2] = {}, -- FIXME: used in several modules saying e.g. "per the Paiboon scheme"; ignored
			["cat"] = {},
		}
	
		local parent_args = frame:getParent().args
		args = require("Module:parameters").process(parent_args, params)
	else
		args = {}
	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("Nem sikerült felismerni a kategórianevet.")
	end

	local lang, sc
	
	if subpage == "sandbox" then
		table.insert(categories, "[[Kategória:Homokozómodulok]]")
	else
		local category = args.cat or categoryKeywords[categoryKeyword]
		if category == "Átírási" then
			code = args[1] or code
		end

		local origcode = code

		if code then
			if category then

				local getByCode = require("Module:languages").getByCode

				for stage=1,2 do
					lang = getByCode(code) or getByCode(code .. "-pro")
					
					if category == "Átírási" then
						if not lang then
							sc = require("Module:scripts").getByCode(code)
							
							if sc then
								table.insert(categories, "Kategória:Átírási modulok írásrendszer szerint|" .. sc:getCanonicalName())
							end
						end
					end

					if lang or sc then
						break
					end

					-- Some modules have names like [[Module:bho-Kthi-translit]] or
					-- [[Module:Deva-Kthi-translit]]. If we didn't recognize the code the
					-- first time, try chopping off the attached script and try again.
					code = code:gsub("%-[A-Z].*", "")
				end
				
				if not (sc or lang) then
					if category == "Átírási" then
						error('A lapcímben lévő „' .. origcode ..
							'” nyelvkódot nem ismerte fel a  [[Module:languages]] vagy a [[Module:scripts]].')
					else
						error('A lapcímben lévő „' .. origcode ..
							'” nyelvkódot nem ismerte fel a [[Module:languages]].')
					end
				end
				
				local function languageCategory(lang, sortkey)
					return lang:getCanonicalName() .. " modulok|" .. sortkey
				end
				
				local function generalCategory(category, sortkey)
					return category .. " modulok|" .. sortkey
				end
				
				if category == "Átírási" 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('A(z) „' .. category .. '” kategória-kulcsszó ismeretlen.')
			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