Module:User:Benwing2/category tree/poscatboiler/data/characters

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Benwing2, for their own experimentation. Items in this module may be added and removed at Benwing2's discretion; do not rely on this module's stability.


local handlers = {}

local ulen = mw.ustring.len
local uupper = mw.ustring.upper
local ulower = mw.ustring.lower
local rfind = mw.ustring.find


-----------------------------------------------------------------------------
--                                                                         --
--                                 HANDLERS                                --
--                                                                         --
-----------------------------------------------------------------------------



-- 	If char is a combining character, returns a version with a dotted circle before it.
function add_dotted_circle(char, combining)
	return combining and "◌" .. char or char
end


table.insert(handlers, function(data)
	--[[
		"desc" is in reference to categories like "Hebrew terms spelled with gershayim",
		in which the title does not contain the character itself.
		Perhaps there is a better word for this.
	]]
	local desc = data.label:match("^terms spelled with (.+)$")
	if not desc then
		return nil
	end
	local ja_ryu = data.lang:getCode() == "ja" or data.lang:getCode() == "ryu"
	-- If Japanese or Okinawan, only fire on a single kanji character.
	if ja_ryu and (desc:find("[A-Za-z]") or ulen(desc) > 1) then
		return nil
	end
	local params = {
		["char"] = {},
		["sort"] = {},
		-- Not sure what used to be done with the following parameters.
		["context"] = {},
		["context2"] = {},
	}
	local args = require("Module:parameters").process(data.args, params)
	local char = args.char or desc
	if not args.char then
		desc = nil
	else
		require("Module:debug").track("charactercat/manual char")
	end

	local combining = ulen(char) == 1 and require("Module:Unicode_data").is_combining(mw.ustring.codepoint(char))
	
	local upper = uupper(char)
	if char ~= upper
			and ulen(char) == 1
			and not rfind(upper, "[" .. data.lang:getStandardCharacters() .. "]") then
		error("Category titles should use uppercase characters: '" .. data.label .. "'")
	end
	
	-- Compute description.

	-- If the letter has a lowercase form, show it.
	local character = require("Module:links").full_link(
		{
			term = char,
			alt = combining and add_dotted_circle(char, true) or nil,
			lang = data.lang,
			tr = combining and "-" or nil,
		},
		"term"
	)
	if ulower(char) ~= char then
		character = "upper case " .. character .. " or lower case " ..
			require("Module:links").full_link(
				{
					term = ulower(char),
					lang = data.lang
				},
				"term"
			)
	end
	
	if desc then
		character = character .. " (" .. desc .. ")"
	end

	local description = "{{{langname}}} terms spelled with " .. character .. "."

	-- Set displaytitle.

	local displaytitle
	local labelRegex = require("Module:string").pattern_escape(char)
	if rfind(data.label, labelRegex .. "$") then
		displaytitle = function(title, lang)
			return mw.ustring.gsub(
				title,
				labelRegex .. "$",
				require("Module:script utilities").tag_text(char, lang, nil, "term")
			)
		end
	end
	
	-- Compute sort key.

	local sortkey =
		args.sort or
		ja_ryu and ulen(char) == 1 and require("Module:zh-sortkey").makeSortKey(char, data.lang:getCode()) or
		data.lang:makeSortKey(char)
	if sortkey == "" then
		sortkey = char
	end

	return {
		description = description,
		displaytitle = displaytitle,
		parents = {{name = "terms by their individual characters", sort = sortkey }},
		breadcrumb = desc or require("Module:script utilities").tag_text(add_dotted_circle(char, combining), data.lang, nil, "term"),
		umbrella = false,
	}, true
end)


return {HANDLERS = handlers}