Module:auto cat: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
adding trredcat and trmandiffcat
catch "letter names" categories before trying to get initial lang, though I am still irritated by the description text of, for instance, Category:th:Thai letter names
Line 1: Line 1:
local export = {}
local export = {}


local function getInitialLang(PAGENAME)
local function getInitialLang(pagename)
local words = mw.text.split(PAGENAME, " ", true)
local words = mw.text.split(pagename, " ", true)
local getByCanonicalName = require("Module:languages").getByCanonicalName
local getByCanonicalName = require("Module:languages").getByCanonicalName
local lang
local lang
Line 30: Line 30:
local code, label = mw.ustring.match(pagename, "^([a-z-]+):(.+)$")
local code, label = mw.ustring.match(pagename, "^([a-z-]+):(.+)$")
return frame:expandTemplate{title = "topic cat", args = {code, label}}
return frame:expandTemplate{title = "topic cat", args = {code, label}}
end
if mw.ustring.find(pagename, "letter names$") then
local lang = mw.ustring.match(pagename, "^([^:]*):")
local cat
if lang then
lang = require("Module:languages").getByCode(lang) or error('The language code "' .. langCode .. '" is not valid.')
cat = mw.ustring.match(pagename, ":(.+)$")
else
cat = pagename
end
return frame:expandTemplate{ title = "topic cat", args = { lang and lang:getCode() or nil, cat } }
end
end

Revision as of 11:54, 27 March 2017

This module implements {{auto cat}}.


local export = {}

local function getInitialLang(pagename)
	local words = mw.text.split(pagename, " ", true)
	local getByCanonicalName = require("Module:languages").getByCanonicalName
	local lang
	
	-- 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
		lang = getByCanonicalName(table.concat(words, " "))
	end
	
	return lang
end

function export.show(frame)
	require("Module:parameters").process(frame:getParent().args, {})
	local titleObject = mw.title.getCurrentTitle()
	local pagename = titleObject.text
	local namespace = titleObject.nsText
	
	if namespace == "Template" then
		return "(This template should be used on pages in the Category: namespace.)"
	elseif namespace ~= "Category" then
		error("This template/module can only be used on pages in the Category: namespace.")
	end
	
	if mw.ustring.find(pagename, "^[a-z-]+:.") then
		local code, label = mw.ustring.match(pagename, "^([a-z-]+):(.+)$")
		return frame:expandTemplate{title = "topic cat", args = {code, label}}
	end
	
	if mw.ustring.find(pagename, "letter names$") then
		local lang = mw.ustring.match(pagename, "^([^:]*):")
		local cat
		
		if lang then
			lang = require("Module:languages").getByCode(lang) or error('The language code "' .. langCode .. '" is not valid.')
			cat = mw.ustring.match(pagename, ":(.+)$")
		else
			cat = pagename
		end
		
		return frame:expandTemplate{ title = "topic cat", args = { lang and lang:getCode() or nil, cat } }
	end
	
	local lang = getInitialLang(pagename)
	local label = (lang and mw.ustring.sub(pagename, mw.ustring.len(lang:getCanonicalName() .. " ") + 1) or pagename)

	if mw.ustring.len(pagename) <= 3 then

		local result = "These are languages that use the letter "

		for i = 1, mw.ustring.len(pagename) do
			if i ~= 1 then
				result = result .. ", "
			end

			result = result .. "[[" .. mw.ustring.sub(pagename, i, i) .. "]]"
		end

		result = result .. "."

		result = result .. "[[Category:Letters]]"

		return result
	elseif mw.ustring.len(pagename) <= 6 and mw.ustring.find(pagename, ":", 1, true) then

		local colonSplit = mw.text.split (pagename, ": ", true)
		local partOne = colonSplit[1]
		local partTwo = colonSplit[2]

		-- local scriptPartOne = require("Module:scripts").findBestScript(partOne, require("Module:languages").getByCode(lang))
		-- local scriptPartTwo = require("Module:scripts").findBestScript(partOne, require("Module:languages").getByCode(lang))

		local result = "These are languages that use the letter "

		for i=1, mw.ustring.len(partOne) do
			if i ~= 1 then
				result = result .. ", "
			end

			result = result .. "[[" .. mw.ustring.sub(partOne, i, i) .. "]]"
		end

		result = result .. ", equivalent to "

		for i=1, mw.ustring.len(partTwo) do
			if i ~= 1 then
				result = result .. ", "
			end

			result = result .. "[[" .. mw.ustring.sub(partTwo, i, i) .. "]]"
		end

		result = result .. "."

		result = result .. "[[Category:Letters]]"

		return result
	end

	if mw.ustring.find(pagename, "block$") then
		return require("Module:category tree/blocks"):contents(pagename)
	end

	if mw.ustring.find(label, "^[tT]erms derived from the PIE root .") then
		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 frame:expandTemplate{title = "PIE root cat", args = {lang and lang:getCode() or nil, term, id = id}}
	end
	
	

	if mw.ustring.find(label, "^[tT]erms derived from the PIE word %*") then
		local word = mw.ustring.match(label, "^[Tt]erms derived from the PIE word %*(.+)$")
		return frame:expandTemplate{title = "PIE word cat", args = {lang and lang:getCode() or nil, word}}
	end
	
	if lang and lang:getCode() == "ar" and mw.ustring.find(label, "^terms belonging to the root .+") then
		return frame:expandTemplate{title = "ar-root cat"}
	end
	
	if lang and lang:getCode() == "he" and mw.ustring.find(label, "^terms belonging to the root .+") then
		local root = mw.ustring.match(label, "^terms belonging to the root (.+)$")
		local parts = mw.text.split(root, "־", true)
		return frame:expandTemplate{title = "HE root cat", args = parts}
	end
	
	if lang and lang:getCode() == "he" and mw.ustring.find(label, "^terms in the pattern .+") then
		return frame:expandTemplate{title = "he-patterncat"}
	end
	
	if mw.ustring.find(label, "^[Tt]erms derived from .") then
		local sourcename = mw.ustring.match(label, "^[Tt]erms derived 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 frame:expandTemplate{title = "derivcatboiler", args = {lang and lang:getCode() or nil, source:getCode()}}
		end
	end

	if mw.ustring.find(label, "^[Tt]erms inherited from .") then
		local sourcename = mw.ustring.match(label, "^[Tt]erms inherited from (.+)$")
		local source = require("Module:etymology languages").getByCanonicalName(sourcename) or require("Module:languages").getByCanonicalName(sourcename)
		
		if source then
			return frame:expandTemplate{title = "inherited cat", args = {lang and lang:getCode() or nil, source:getCode()}}
		end
	end
	
	if mw.ustring.find(label, "^[Tt]erms borrowed from .") then
		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 frame:expandTemplate{title = "borrowed cat", args = {lang and lang:getCode() or nil, source:getCode()}}
		end
end

	if mw.ustring.find(label, "^[Tt]erms calqued from .") then
		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 frame:expandTemplate{title = "calque cat", args = {lang and lang:getCode() or nil, source:getCode()}}
		end
	end
	
	if mw.ustring.find(label, "^terms transliterated from other languages") then
		return frame:expandTemplate{title = "translitcatboiler", args = {lang and lang:getCode() or nil}}
	end
	
	if mw.ustring.find(label, "^[Tt]ransliterations of") then
		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 frame:expandTemplate{title = "translitcatboiler", args = {lang and lang:getCode(), source:getCode()}}
		end
	end
	
	for _, affixtype in ipairs({"circumfix", "infix", "interfix", "prefix", "suffix"}) do
		if mw.ustring.find(label, "^.+ " .. affixtype .. "ed with .") then
			local pos, after = mw.ustring.match(label, "^(.+) " .. affixtype .. "ed with (.+)$")
			
			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 frame:expandTemplate{title = affixtype .. "cat", args = {lang:getCode(), term, pos = pos, id = id}}
		end
	end
	
	if lang then
		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 frame:expandTemplate{title = "nameboiler", args = {nametype, lang:getCode(), source:getCode()}}
				end
			end
		end
	end
	
	if lang and lang:getCode() == "ja" and mw.ustring.find(label, "^terms spelled with .+ read as .+$") then
		local term, reading = mw.ustring.match(label, "^terms spelled with (.+) read as (.+)$")
		if term == "kanji" then
			return frame:expandTemplate{title = "ja-readascat", args = {reading}}
		else
			return frame:expandTemplate{title = "ja-readingcat", args = {term, reading}}
		end
	end
	
	if mw.ustring.find(label, "^terms spelled with .+") then
		local term = mw.ustring.match(label, "^terms spelled with (.+)$")
		return frame:expandTemplate{title = "charactercat", args = {lang:getCode(), term}}
	end
	
	if pagename == "Phrasebooks by language" then
		return frame:expandTemplate{title = "pbcatboiler", args = {}}
	elseif label == "phrasebook" then
		return frame:expandTemplate{title = "pbcatboiler", args = {lang:getCode()}}
	elseif mw.ustring.find(label, "^phrasebook/.") then
		label = mw.ustring.match(label, "^phrasebook/(.+)$")
		return frame:expandTemplate{title = "pbcatboiler", args = {lang:getCode(), label}}
	end
	
	if lang then
		return frame:expandTemplate{title = "poscatboiler", args = {lang:getCode(), label}}
	end
	
	if mw.ustring.find(label, ". by language$") then
		local label = mw.getContentLanguage():lcfirst(mw.ustring.match(label, "^(.+) by language$"))
		return frame:expandTemplate{title = "poscatboiler", args = {nil, label}}
	end
	
	if mw.ustring.find(label, "^Terms with redundant transliterations") then
		langCode = mw.ustring.match(label, "/(.+)")
		
		if langCode then
			return frame:expandTemplate { title = "trredcat", args = { langCode } }
		end
	end
	
	if mw.ustring.find(label, "^Terms with manual transliterations different from the automated ones") then
		langCode = mw.ustring.match(label, "/(.+)")
		
		if langCode then
			return frame:expandTemplate { title = "trmandiffcat", args = { langCode } }
		end
	end
	
	return frame:expandTemplate{title = "topic cat", args = {nil, label}}
end

return export