Jump to content

Module:glossary

From Wiktionary, the free dictionary

The test_existence function tracks instances of {{glossary}} that link to anchors not found in Appendix:Glossary. See Category:Pages linking to anchors not found in Appendix:Glossary. It uses the data module Module:glossary/data to determine if the anchor exists.


local export = {}

local get_ids_module = "Module:get IDs"
local glossary_data_module = "Module:glossary/data"
local parameters_module = "Module:parameters"
local string_nowiki_module = "Module:string/nowiki"

local anchor_encode = mw.uri.anchorEncode
local require = require

--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
	local function normalize_anchor(...)
		normalize_anchor = require(get_ids_module).normalize_anchor
		return normalize_anchor(...)
	end
	
	local function nowiki(...)
		nowiki = require(string_nowiki_module)
		return nowiki(...)
	end
	
	local function process_params(...)
		process_params = require(parameters_module).process
		return process_params(...)
	end

--[==[
Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==]
	local data
	local function get_data()
		data, get_data = mw.loadData(glossary_data_module), nil
		return data
	end

function export.def(frame)
	local required_default_ = {required = true, default = ""}
	local args = process_params(frame:getParent().args, {
		[1] = required_default_,
		[2] = required_default_,
	})
	
	local term = args[1]
	return "; <span id=\"" .. anchor_encode(term) .. "\">" .. term .. "</span>\n: " .. args[2]
end

local function get_link(formatted_anchor, text, anchor)
	return "[[Appendix:Glossary#" .. nowiki(formatted_anchor) .. "|" .. (text or anchor) .. "]]"
end

function export.link(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
		[2] = true,
	})
	
	local anchor, text = args[1], args[2]
	local formatted_anchor = normalize_anchor(anchor)
	if (data or get_data())[formatted_anchor] then
		return get_link(formatted_anchor, text, anchor)
	end
	
	local lower_anchor = anchor:ulower()
	formatted_anchor = normalize_anchor(lower_anchor)
	
	local link = get_link(formatted_anchor, text, anchor)
	
	if data[formatted_anchor] or formatted_anchor == "" then
		return link
	end
	
	mw.log("The anchor " .. lower_anchor
		.. (lower_anchor ~= anchor and " or " .. anchor or "")
		.. " does not exist in Appendix:Glossary.")
	
	return link
		.. "[[Category:Pages linking to anchors not found in Appendix:Glossary|"
		.. lower_anchor .. "]]"
end

return export