Module:category tree/data: Difference between revisions

From Linguifex
Jump to navigation Jump to search
Created page with "local data = { "All language families", "All languages", "All scripts", "Categories only containing subcategories", "Character boxes", "Characters by script", "Entries..."
 
m 1 revision imported
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
local data = {
local labels = {}
"All language families",
local raw_categories = {}
"All languages",
local handlers = {}
"All scripts",
local raw_handlers = {}
"Categories only containing subcategories",
 
"Character boxes",
local subpages = {
"Characters by script",
-- It should not matter much what order we do the handlers in, but topic handling historically
"Entries with audio examples",
-- preceded "poscatboiler" handling (i.e. everything else), so keep it that way for the moment.
"Entries with redirects",
"topic",
"Entry maintenance by language",
"affixes and compounds",
"Figures of speech by language",
"characters",
"Gestures",
"entry maintenance",
"Lemmas by language",
"etymology",
"Letters",
"families",
"Lists",
"figures of speech",
"Non-lemma forms by language",
"grammatical classes",
"Phrasebooks by language",
"lang-specific-raw",
"Protologisms",
"languages",
"Regionalisms",
"lects",
"Rhymes by language",
"lemmas",
"Sentences by language",
"lexical properties",
"All sets",
"miscellaneous",
"Shortenings by language",
"modules",
"Symbols by language",
"names",
"Synchronized entries by language",
"non-lemma forms",
"Terms by etymology by language",
"phrases",
"Terms by lexical property by language",
"pragmatic properties",
"Terms by semantic function by language",
"rhymes",
"Terms by usage by language",
"scripts",
"All topics",
"semantic classes",
"Unicode blocks",
"shortenings",
"Unsupported titles",
"speech acts",
"Wiktionary",
"symbols",
"Wiktionary pages that don't exist",
"templates",
"Wiktionary-namespace discussion pages",
"terms by script",
"transliterations",
"unicode",
"wiktionary maintenance",
"wiktionary users",
"word of the day",
}
}


for i, category in ipairs(data) do
-- Import subpages
data[i] = nil
for _, subpage in ipairs(subpages) do
data["Category:" .. category] = true
local datamodule = "Module:category tree/" .. subpage
local retval = require(datamodule)
if retval["LABELS"] then
for label, data in pairs(retval["LABELS"]) do
if labels[label] and not retval["IGNOREDUP"] then
error("Label " .. label .. " defined in both [["
.. datamodule .. "]] and [[" .. labels[label].module .. "]].")
end
data.module = datamodule
labels[label] = data
end
end
if retval["RAW_CATEGORIES"] then
for category, data in pairs(retval["RAW_CATEGORIES"]) do
if raw_categories[category] and not retval["IGNOREDUP"] then
error("Raw category " .. category .. " defined in both [["
.. datamodule .. "]] and [[" .. raw_categories[category].module .. "]].")
end
data.module = datamodule
raw_categories[category] = data
end
end
if retval["HANDLERS"] then
for _, handler in ipairs(retval["HANDLERS"]) do
table.insert(handlers, { module = datamodule, handler = handler })
end
end
if retval["RAW_HANDLERS"] then
for _, handler in ipairs(retval["RAW_HANDLERS"]) do
table.insert(raw_handlers, { module = datamodule, handler = handler })
end
end
end
end


return data
-- Add child categories to their parents
local function add_children_to_parents(hierarchy, raw)
for key, data in pairs(hierarchy) do
local parents = data.parents
if parents then
if type(parents) ~= "table" then
parents = {parents}
end
if parents.name or parents.module then
parents = {parents}
end
for _, parent in ipairs(parents) do
if type(parent) ~= "table" or not parent.name and not parent.module then
parent = {name = parent}
end
if parent.name and not parent.module and type(parent.name) == "string" and not parent.name:find("^Category:") then
local parent_is_raw
if raw then
parent_is_raw = not parent.is_label
else
parent_is_raw = parent.raw
end
-- Don't do anything if the child is raw and the parent is lang-specific, otherwise e.g.
-- "Lemmas subcategories by language" will be listed as a child of every "LANG lemmas" category.
-- FIXME: We need to rethink this mechanism.
if not raw or parent_is_raw then
local child_hierarchy = parent_is_raw and raw_categories or labels
if child_hierarchy[parent.name] then
local child = {name = key, sort = parent.sort, raw = raw}
if child_hierarchy[parent.name].children then
table.insert(child_hierarchy[parent.name].children, child)
else
child_hierarchy[parent.name].children = {child}
end
end
end
end
end
end
end
end
 
add_children_to_parents(labels)
add_children_to_parents(raw_categories, true)
 
return {
LABELS = labels, RAW_CATEGORIES = raw_categories,
HANDLERS = handlers, RAW_HANDLERS = raw_handlers
}

Latest revision as of 11:22, 21 April 2026

Documentation for this module may be created at Module:category tree/data/doc

local labels = {}
local raw_categories = {}
local handlers = {}
local raw_handlers = {}

local subpages = {
	-- It should not matter much what order we do the handlers in, but topic handling historically
	-- preceded "poscatboiler" handling (i.e. everything else), so keep it that way for the moment.
	"topic",
	"affixes and compounds",
	"characters",
	"entry maintenance",
	"etymology",
	"families",
	"figures of speech",
	"grammatical classes",
	"lang-specific-raw",
	"languages",
	"lects",
	"lemmas",
	"lexical properties",
	"miscellaneous",
	"modules",
	"names",
	"non-lemma forms",
	"phrases",
	"pragmatic properties",
	"rhymes",
	"scripts",
	"semantic classes",
	"shortenings",
	"speech acts",
	"symbols",
	"templates",
	"terms by script",
	"transliterations",
	"unicode",
	"wiktionary maintenance",
	"wiktionary users",
	"word of the day",
}

-- Import subpages
for _, subpage in ipairs(subpages) do
	local datamodule = "Module:category tree/" .. subpage
	local retval = require(datamodule)
	if retval["LABELS"] then
		for label, data in pairs(retval["LABELS"]) do
			if labels[label] and not retval["IGNOREDUP"] then
				error("Label " .. label .. " defined in both [["
					.. datamodule .. "]] and [[" .. labels[label].module .. "]].")
			end
			data.module = datamodule
			labels[label] = data
		end
	end
	if retval["RAW_CATEGORIES"] then
		for category, data in pairs(retval["RAW_CATEGORIES"]) do
			if raw_categories[category] and not retval["IGNOREDUP"] then
				error("Raw category " .. category .. " defined in both [["
					.. datamodule .. "]] and [[" .. raw_categories[category].module .. "]].")
			end
			data.module = datamodule
			raw_categories[category] = data
		end
	end
	if retval["HANDLERS"] then
		for _, handler in ipairs(retval["HANDLERS"]) do
			table.insert(handlers, { module = datamodule, handler = handler })
		end
	end
	if retval["RAW_HANDLERS"] then
		for _, handler in ipairs(retval["RAW_HANDLERS"]) do
			table.insert(raw_handlers, { module = datamodule, handler = handler })
		end
	end
end

-- Add child categories to their parents
local function add_children_to_parents(hierarchy, raw)
	for key, data in pairs(hierarchy) do
		local parents = data.parents
		if parents then
			if type(parents) ~= "table" then
				parents = {parents}
			end
			if parents.name or parents.module then
				parents = {parents}
			end
			for _, parent in ipairs(parents) do
				if type(parent) ~= "table" or not parent.name and not parent.module then
					parent = {name = parent}
				end
				if parent.name and not parent.module and type(parent.name) == "string" and not parent.name:find("^Category:") then
					local parent_is_raw
					if raw then
						parent_is_raw = not parent.is_label
					else
						parent_is_raw = parent.raw
					end
					-- Don't do anything if the child is raw and the parent is lang-specific, otherwise e.g.
					-- "Lemmas subcategories by language" will be listed as a child of every "LANG lemmas" category.
					-- FIXME: We need to rethink this mechanism.
					if not raw or parent_is_raw then
						local child_hierarchy = parent_is_raw and raw_categories or labels
						if child_hierarchy[parent.name] then
							local child = {name = key, sort = parent.sort, raw = raw}
							if child_hierarchy[parent.name].children then
								table.insert(child_hierarchy[parent.name].children, child)
							else
								child_hierarchy[parent.name].children = {child}
							end
						end
					end
				end
			end
		end
	end
end

add_children_to_parents(labels)
add_children_to_parents(raw_categories, true)

return {
	LABELS = labels, RAW_CATEGORIES = raw_categories,
	HANDLERS = handlers, RAW_HANDLERS = raw_handlers
}