Modul:auto cat

Dokumentasjon for modulen kan opprettast på Modul:auto cat/dok

local export = {}


local function splitLabelLang(titleObject)
	titteltekstSmaae = mw.ustring.lower(titleObject.text)
	local words = mw.text.split(titteltekstSmaae, " ", true)
	local sisteOrdet = words[#words]
	local getByCanonicalName = require("Module:languages").getByCanonicalName
	local lang, lang1, lang2
	-- Progressively remove words from the end of the category name until it matches a language.
	while words[1] and not lang do
		words[#words] = nil
		lang1 = getByCanonicalName(table.concat(words, " "):gsub('e$', '')) -- fjern -e frå 'norske'
		lang2 = getByCanonicalName(table.concat(words, " "):gsub('^(.+ i )', '')) -- t.d. 'i bokmål'
		lang3 = getByCanonicalName(table.concat(words, " "):gsub('e$', 't')) -- frå -e til -t i 'norrøne'
		lang = lang1 or lang2 or lang3
	end
	local label
	if not lang and not titteltekstSmaae:match('arva') and not titteltekstSmaae:match('opphav')  then
		lang = getByCanonicalName(sisteOrdet) -- t.d. 'namnord i bokmål'
	end
	if lang1 then
		label = (lang and mw.ustring.sub(titteltekstSmaae, mw.ustring.len(lang:getCanonicalName() .. "e ") + 1) or titteltekstSmaae)
	elseif lang3 then
		label = (lang and mw.ustring.sub(titteltekstSmaae, mw.ustring.len(lang:getCanonicalName()) + 1) or titteltekstSmaae)
	elseif lang or lang2 then
		label = (lang and mw.ustring.gsub(titteltekstSmaae, ' i ' .. lang:getCanonicalName(), '') or titteltekstSmaae)
	else
		label = titteltekstSmaae
	end
	
	return label, lang
end


-- List of handler functions that try to match the page name.
-- A handler should return a table of template title plus arguments
-- that is passed to frame:expandTemplate.
-- If a handler does not recognise the page name, it should return nil.
-- Note that the order of functions matters!

local handlers = {}


-- Topical categories
table.insert(handlers, function(titleObject)
	if not mw.ustring.find(titleObject.text, "^[a-z-]+:.") then
		return nil
	end
	
	local code, label = mw.ustring.match(titleObject.text, "^([a-z-]+):(.+)$")
	return {title = "topic cat", args = {code, label}}
end)


-- Letter names
table.insert(handlers, function(titleObject)
	if not mw.ustring.find(titleObject.text, "letter names$") then
		return nil
	end
	
	local langCode = mw.ustring.match(titleObject.text, "^([^:]+):")
	local lang, cat
	
	if langCode then
		lang = require("Module:languages").getByCode(langCode) or error('The language code "' .. langCode .. '" is not valid.')
		cat = mw.ustring.match(titleObject.text, ":(.+)$")
	else
		cat = titleObject.text
	end
	
	return {title = "topic cat", args = {lang and lang:getCode() or nil, cat}}
end)


-- letter cat
table.insert(handlers, function(titleObject)
	if not (mw.ustring.len(titleObject.text) <= 3 or mw.ustring.len(titleObject.text) <= 6 and mw.ustring.find(titleObject.text, ":", 1, true)) then
		return nil
	end
	
	return {title = "letter cat"}
end)


-- Japanese kanji reading cat
table.insert(handlers, function(titleObject)
	if not mw.ustring.find(titleObject.text, "^Japanese kanji") then
		return nil
	end
	
	return {title = "ja-readings-cat"}
end)


-- Unicode block cat
table.insert(handlers, function(titleObject)
	if not mw.ustring.find(titleObject.text, "block$") then
		return nil
	end
	
	return {title = "Unicode block cat"}
end)


-- request cat
table.insert(handlers, function(titleObject)
	if not mw.ustring.find(titleObject.text, "^Requests") then
		return nil
	end
	
	return {title = "request cat"}
end)


-- PIE root cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[tT]erms derived from the PIE root .") then
		return nil
	end
	
	local after = mw.ustring.match(label, "^[Tt]erms derived from the PIE root (.+)$")
	local term, id
	
	if mw.ustring.find(after, ". %([^()]+%)$") then
		term, id = mw.ustring.match(after, "^(.+) %(([^()]+)%)$")
	else
		term = after
	end
	
	term = mw.ustring.gsub(term, "^%*(.+)%-$", "%1")
	
	return {title = "PIE root cat", args = {lang and lang:getCode() or nil, term, id = id}}
end)


-- PIE word cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[tT]erms derived from the PIE word %*") then
		return nil
	end
	
	local word = mw.ustring.match(label, "^[Tt]erms derived from the PIE word %*(.+)$")
	return {title = "PIE word cat", args = {lang and lang:getCode() or nil, word}}
end)


-- ar-root cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not (lang and lang:getCode() == "ar" and mw.ustring.find(label, "^terms belonging to the root .+")) then
		return nil
	end
	
	return {title = "ar-root cat"}
end)


--HE root cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not (lang and lang:getCode() == "he" and mw.ustring.find(label, "^terms belonging to the root .+")) then
		return nil
	end
	
	local root = mw.ustring.match(label, "^terms belonging to the root (.+)$")
	local parts = mw.text.split(root, "־", true)
	return {title = "HE root cat", args = parts}
end)


-- he-patterncat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not (lang and lang:getCode() == "he" and mw.ustring.find(label, "^terms in the pattern .+")) then
		return nil
	end
	
	return {title = "he-patterncat"}
end)


-- derivcatboiler
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Oo]rd med opphav i .") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "^[Oo]rd med opphav i (.+)$")
	local source
	
	if mw.ustring.find(sourcename, " [Sp]råk$") then
		sourcename = mw.ustring.gsub(sourcename, " språk$", "")
		source = require("Module:families").getByCanonicalName(sourcename)
	else
		source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	end
	
	if source then
		return {title = "derivcatboiler", args = {lang and lang:getCode() or nil, source:getCode()}}
	end
end)


-- inherited cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Oo]rd arva frå .") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "^[Oo]rd arva frå (.+)$")
	local source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	
	if source then
		return {title = "inherited cat", args = {lang and lang:getCode() or nil, source:getCode()}}
	end
end)


-- borrowed cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Tt]erms borrowed from .") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "^[Tt]erms borrowed from (.+)$")
	local source
	
	if mw.ustring.find(sourcename, " [Ll]anguages$") then
		sourcename = mw.ustring.gsub(sourcename, " languages$", "")
		source = require("Module:families").getByCanonicalName(sourcename)
	else
		source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	end
	
	if source then
		return {title = "borrowed cat", args = {lang and lang:getCode() or nil, source:getCode()}}
	end
end)


-- calque cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Tt]erms calqued from .") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "^[Tt]erms calqued from (.+)$")
	local source
	
	if mw.ustring.find(sourcename, " [Ll]anguages$") then
		sourcename = mw.ustring.gsub(sourcename, " languages$", "")
		source = require("Module:families").getByCanonicalName(sourcename)
	else
		source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	end
	
	if source then
		return {title = "calque cat", args = {lang and lang:getCode() or nil, source:getCode()}}
	end
end)
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Ss]emantic loans from .") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "^[Ss]emantic loans from (.+)$")
	local source
	
	if mw.ustring.find(sourcename, " [Ll]anguages$") then
		sourcename = mw.ustring.gsub(sourcename, " languages$", "")
		source = require("Module:families").getByCanonicalName(sourcename)
	else
		source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	end
	
	if source then
		return {title = "semantic loan cat", args = {lang and lang:getCode() or nil, source:getCode()}}
	end
end)

-- translitcatboiler
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^terms transliterated from other languages") then
		return nil
	end
	
	return {title = "translitcatboiler", args = {lang and lang:getCode() or nil}}
end)


-- translitcatboiler
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^[Tt]ransliterations of") then
		return nil
	end
	
	local sourcename = mw.ustring.match(label, "[Tt]ransliterations of (.+) terms")
	local source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
	
	if not lang then
		local lang = ""
	end
	
	if source then
		return {title = "translitcatboiler", args = {lang and lang:getCode(), source:getCode()}}
	end
end)


-- circumfixcat, infixcat, interfixcat, prefixcat, suffixcat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	local malar = {['førefestet'] = 'førefestekat', etterfestet = 'etterfestekat'}
	for _, affixtype in ipairs({"circumfix", "infix", "interfix", "førefestet", "etterfestet"}) do
		if mw.ustring.find(label, "^.+ med " .. affixtype .. " .") then
			local pos, after = mw.ustring.match(label, "^(.+) med " .. affixtype .. " (.+)$")
			
			if pos == "words" then
				pos = nil
			end
			
			local term, id
			
			if mw.ustring.find(after, ". %([^()]+%)$") then
				term, id = mw.ustring.match(after, "^(.+) %(([^()]+)%)$")
			else
				term = after
			end
			
			return {title = malar[affixtype], args = {lang:getCode(), term, pos = pos, id = id}}
		end
	end
end)


table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not lang then
		return nil
	end
	
	for _, nametype in ipairs({"surnames", "male given names", "female given names"}) do
		local sourcename = mw.ustring.match(label, "^" .. nametype .. " from (.+)$")
		
		if sourcename then
			local source = require("Module:languages").getByCanonicalName(sourcename)
			
			if source then
				return {title = "nameboiler", args = {nametype, lang:getCode(), source:getCode()}}
			end
		end
	end
end)


-- charactercat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^terms spelled with .+") then
		return nil
	end
	
	local term = mw.ustring.match(label, "^terms spelled with (.+)$")
	return {title = "charactercat", args = {lang:getCode(), term}}
end)


-- pbcatboiler
table.insert(handlers, function(titleObject)
	if titleObject.text == "Phrasebooks by language" then
		return {title = "pbcatboiler", args = {}}
	else
		local label, lang = splitLabelLang(titleObject)
		
		if label == "phrasebook" then
			return {title = "pbcatboiler", args = {lang:getCode()}}
		elseif mw.ustring.find(label, "^phrasebook/.") then
			label = mw.ustring.match(label, "^phrasebook/(.+)$")
			return {title = "pbcatboiler", args = {lang:getCode(), label}}
		end
	end
end)

-- no entry cat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "entries that don't exist$") then
		return nil
	end
	
	return { title = "no entry cat", args = { lang:getCode() } }
end)

-- poscatboiler
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if lang then
		return {title = "poscatboiler", args = {lang:getCode(), label}}
	elseif mw.ustring.find(label, ". by language$") then
		local label = mw.getContentLanguage():lcfirst(mw.ustring.match(label, "^(.+) by language$"))
		return {title = "poscatboiler", args = {nil, label}}
	end
end)


-- trredcat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^Terms with redundant transliterations") then
		return nil
	end
	
	langCode = mw.ustring.match(label, "/(.+)")
	
	if langCode then
		return {title = "trredcat", args = {langCode}}
	end
end)


-- trmandiffcat
table.insert(handlers, function(titleObject)
	local label, lang = splitLabelLang(titleObject)
	
	if not mw.ustring.find(label, "^Terms with manual transliterations different from the automated ones") then
		return nil
	end
	
	local langCode = mw.ustring.match(label, "/(.+)")
	
	if langCode then
		return {title = "trmandiffcat", args = {langCode}}
	end
end)


-- topic cat
table.insert(handlers, function(titleObject)
	return {title = "topic cat", args = {nil, titleObject.text}}
end)


function export.show(frame)
	require("Module:parameters").process(frame:getParent().args, {})
	local titleObject = mw.title.getCurrentTitle()
	
	if titleObject.nsText == "Template" then
		return "(This template should be used on pages in the Category: namespace.)"
	elseif titleObject.nsText ~= "Kategori" then
		error("This template/module can only be used on pages in the Category: namespace.")
	end
	
	for _, handler in ipairs(handlers) do
		local t = handler(titleObject)
		
		if t then
			return frame:expandTemplate(t)
		end
	end
end

return export