Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Type: Difference between revisions

From FiguraMC
PenguinEncounter (talk | contribs)
add some hardcoded links because we're getting near the limit
PenguinEncounter (talk | contribs)
update to vector2/3/4
 
(One intermediate revision by the same user not shown)
Line 24: Line 24:
["^[Mm]atrices$"] = "Matrix",
["^[Mm]atrices$"] = "Matrix",
["^[Mm]atrix[234]?$"] = "Matrix",
["^[Mm]atrix[234]?$"] = "Matrix",
["^[Vv]ector[234]?$"] = "Vector",
["^[Vv]ector([234]?)$"] = "Vector%1",
}
}
-- Adding links here will reduce the 'expensive parser function count',
-- Adding links here will reduce the 'expensive parser function count',
Line 31: Line 31:
-- return *false*, resulting in links being marked broken even if they aren't
-- return *false*, resulting in links being marked broken even if they aren't
local LINKS = {
local LINKS = {
["^Vector$"] = "Vectors and Matrices",
["^Vector$"] = "Vector",
["^Vector2$"] = "Vector2",
["^Vector3$"] = "Vector3",
["^Vector4$"] = "Vector4",
["^Matrix$"] = "Vectors and Matrices",
["^Matrix$"] = "Vectors and Matrices",
["^Event$"] = "Event (type)",
["^Event$"] = "Event (type)",

Latest revision as of 04:53, 29 October 2024

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

local exports = {}

local builtin_url = "https://www.lua.org/manual/5.2/manual.html#2.1"
local BUILTINS = {
	["nil"] = true,
	["number"] = true,
	["boolean"] = true,
	["string"] = true,
	["true"] = true,
	["false"] = true,
	["function"] = true,
	["table"] = true,
	-- not really a builtin but close enough
	["any"] = true
}
local NOT_PLURAL = {
	["Matrixs"] = true,
}
local ALIASES = {
	["^bool$"] = "boolean",
    ["^[Ii]nteger$"] = "number",
    ["^[Dd]ouble$"] = "number",
    ["^[Ff]loat$"] = "number",
	["^[Mm]atrices$"] = "Matrix",
	["^[Mm]atrix[234]?$"] = "Matrix",
	["^[Vv]ector([234]?)$"] = "Vector%1",
}
-- Adding links here will reduce the 'expensive parser function count',
-- (which increases by 1 or 2 per page that has to be dynamically detected)
-- There's a limit of 100 on expensive functions, after which they will always
-- return *false*, resulting in links being marked broken even if they aren't
local LINKS = {
	["^Vector$"] = "Vector",
	["^Vector2$"] = "Vector2",
	["^Vector3$"] = "Vector3",
	["^Vector4$"] = "Vector4",
	["^Matrix$"] = "Vectors and Matrices",
	["^Event$"] = "Event (type)",
	["^ModelPart$"] = "ModelPart",
	["^ConfigAPI$"] = "ConfigAPI",
}

local function exists(page_name)
	return mw.title.new(page_name).exists
end

local function infer_name_link(frame, type_data)
	local name = type_data.name
	name = name:gsub("%s*$", ""):gsub("^%s*", "")
	if not NOT_PLURAL[name] then name = name:gsub("s$", "") end
	for pat, repl in pairs(ALIASES) do
		local new_name, count = name:gsub(pat, repl)
		if count > 0 then name = new_name break end
	end
	
	type_data.css_name = name
	if type_data.link then
		if type_data.link:find("^https?://") then type_data.link_external = true end
		return
	end
	if BUILTINS[name] then
		type_data.link = builtin_url
		type_data.link_external = true
		return
	end
	local link
	for pat, target in pairs(LINKS) do
		if name:find(pat) then link = target break end
	end
	if not link then
		if exists(name .. " (type)") then
			link = name .. " (type)"
		else
			link = name
			if not exists(name) then
				type_data.extra_attr = (type_data.extra_attr or "") .. " data-missing"
			end
		end
	end
	if link:find("^https?://") then type_data.link_external = true end
	type_data.link = link
end

local function create(frame, options)
	infer_name_link(frame, options)
	
	if options.modifiers and options.modifiers:find("^%s*$") then options.modifiers = nil end
	
	local secondary_text = options.modifiers
	local link = options.link
	local text = options.text or options.name
	local link_text
	if options.link_external then
		link_text = string.format("[%s %s]", options.link, text)
	else
		link_text = string.format("[[%s|%s]]", options.link, text)
	end
	local secondary = string.format([[<span class="lua-type-mod">%s</span>]], secondary_text or "")
	return string.format(
		[=[<span class="lua-type" data-name="%s"%s>%s%s</span>]=],
		options.css_name, options.extra_attr or "", link_text, secondary_text and secondary or ""
	)
end

-- local function resolve_type()

local function expand_types(type_name)
	local name, modifiers = type_name:match("^([^?%.[]-)([[%.%]?]*)$")
	if not name then error("Couldn't parse type name " .. type_name) end
	local result = {}
	if modifiers:find("?$") then
		modifiers = modifiers:sub(1, -2) -- cut off the ?
		table.insert(result, {name="nil"})
	end
	table.insert(result, 1, {name=name, modifiers=modifiers})
	return result
end

function exports.run(frame)
	local sb = ""
	local input_args = frame:getParent().args
	
	if not (input_args[1] or input_args.name) then error "need to specify at least one type name" end
	
	local name = input_args[1] or input_args.name
	local compatibility_mode = {["?"]=true,["[]"]=true, ["[]?"]=true}
	if input_args[2] and compatibility_mode[input_args[2]] then
		name = name .. input_args[2]
	end
	
	local type_structs = expand_types(name)
	if input_args.text then type_structs[1].text = input_args.text end
	if input_args.link then type_structs[1].link = input_args.link end
	local strings = {}
	for _, part in ipairs(type_structs) do table.insert(strings, create(frame, part)) end
	return table.concat(strings, " &vert; ")
end

return exports