Module:unsupported titles

ຈາກ Wiktionary

Documentation for this module may be created at Module:unsupported titles/doc

local export = {}

-- This function returns a link to an unsupported title.
-- text -> The 1st argument given to [[Template:unsupported]], consisting of the unsupported title to format as a link. If the wanted page is "Unsupported titles/Low line", you can use "_", "low line", "underline", etc., provided the page is listed in [[Module:unsupported titles/data]].
-- altText -> The 2nd argument given to [[Template:unsupported]], consisting of the alternative text to be displayed, if applicable.
-- noError -> If true, it returns nil if the entry does not exist, instead of an error. This may be used to test if an entry exists as an unsupported title, because an error would need to be fixed, but a simple test may not need fixing.
-- onlyActualPageTitle -> If true, the module only accepts the actual page title, and not any of the aliases. For example, the module would only accept "_", and not "low line", "underscore" or "underline". This may be used to test if an entry exists as an unsupported title, so for example "underscore" would not be recognized as an unsupported title.
function export.parse(text, altText, noError, onlyActualPageTitle)
	-- allTitles -> a list of all unsupported titles, located at "Module:unsupported titles/data".
	local allTitles = mw.loadData("Module:unsupported titles/data")

	-- englishObject -> the English language object, to be used later for the "ucfirst" function.
	local englishObject = mw.language.new("en")

	-- The code below parses a text like this, in the "allTitles" argument from "Module:unsupported titles/data":
	-- m["full stop"] = {display=".", "period",}
	-- The "display" value is optional, but if it exists, this is the displayed text.
	for k, v in pairs(allTitles) do
		local pagename = text
		if v["pagename"] then pagename = englishObject:ucfirst(v["pagename"]) end

		-- First, check the table keys for page names.
		if text == k then
			return "[[Unsupported titles/" .. pagename .. "|" .. (altText or v["display"] or k) .. "]]"
		end

		-- Second, check the table values for page names. This is ignored if onlyActualPageTitle is true.
		if onlyActualPageTitle ~= true then
			for l, m in pairs(v) do
				if text == m then
					return "[[Unsupported titles/" .. pagename .. "|" .. (altText or v["display"] or k) .. "]]"
				end
			end
		end
	end

	if noError == true then
		return null
	else
		error ("Unable to recognize the first template argument as an unsupported page title.")
	end
end

function export.link(frame)
	local text = frame:getParent().args[1]
	local altText = frame:getParent().args[2]
	local noError = frame:getParent().args["noerror"]

	return export.parse(text, altText, noError)
end
	
return export