Module:Members

local member_icon = require("Module:MemberIcon") local member_tags = require("Module:Member tags") local types = require("Module:Type") local utils = require("Module:Utils")

--% This function is used to compare two members by their names. --@ member1 (table) the first member --@ member2 (table) the second member --: (boolean) true if the first member's name is sorted before the second's, false otherwise local function compare_members(member1, member2) return member1.Name < member2.Name end

local p = {}

--% This function generates a list of a class' members, getting the necessary information from Module:APIDump. --@ class_name (string) the name of the class --@ descriptions (table) a dictionary: keys are member names, values are their wikitext descriptions --: (string) the generated list of members function p.generate_member_list(class_name, descriptions) local class_data = require("Module:API").get_class_data(class_name) if not class_data then return error("No information is known about class " .. class_name .. ".") end if not class_data.Members then return nil -- No members to create a list for end local properties = {} local functions = {} local events = {} local callbacks = {} for _, member in ipairs(class_data.Members) do       if member.MemberType == "Property" then table.insert(properties, member) elseif member.MemberType == "Function" then table.insert(functions, member) elseif member.MemberType == "Event" then table.insert(events, member) elseif member.MemberType == "Callback" then table.insert(callbacks, member) end end local sections = {} table.insert(sections, p.generate_member_list_of_type("Properties", properties, descriptions)) table.insert(sections, p.generate_member_list_of_type("Functions", functions, descriptions)) table.insert(sections, p.generate_member_list_of_type("Events", events, descriptions)) table.insert(sections, p.generate_member_list_of_type("Callbacks", callbacks, descriptions)) return table.concat(sections, "\n") end

--% This function generates a list of members of a certain type. --@ title (string) name for the section --@ members (table) array of members in API dump format --@ descriptions (table) dictionary: keys are member names, values are their wikitext descriptions --: (string) generated list of all members passed function p.generate_member_list_of_type(title, members, descriptions) table.sort(members, compare_members) local sections = {} for _, member in ipairs(members) do       if descriptions and descriptions[member.Name] then table.insert(sections, p.generate_member_section(member, descriptions[member.Name])) else table.insert(sections, p.generate_member_section(member, nil)) end end if #sections > 0 then return "== " .. (title and title or "Members") .. " ==\n\n" .. table.concat(sections, "\n") else return nil end end

--% This function generates a list of tags. --@ tags (table) a list of tags, in the API dump format --: (string) the formatted tags, including newlines before and after function p.generate_tag_list(tags) local list = {} if tags then for tag_index, tag in ipairs(tags) do           table.insert(list, member_tags.make_heading_tag{name=tag}) end end if #list > 0 then return " "..table.concat(list, " ").." " else return "" end end

--% This function generates a string describing a parameter. --% It is meant for use in parameter lists. --@ parameter (table) a table describing the parameter, in API dump format --: (string) the generated string function p.generate_parameter_string(parameter) local parts = {} if parameter.Type then table.insert(parts, types.link_type_with_category(parameter.Type.Name, parameter.Type.Category)) end if parameter.Name then table.insert(parts, mw.text.tag("var", {class="parameter-name"}, parameter.Name)) end if parameter.Default then table.insert(parts, "=") table.insert(parts, parameter.Default) end return table.concat(parts, " ") end

--% This function generates a list of parameters. --@ parameters (table) an array of parameters, in API dump format --: (string) the generated list of parameters, including parentheses function p.generate_parameter_list(parameters) local list = {} if parameters then for _, parameter in ipairs(parameters) do           table.insert(list, p.generate_parameter_string(parameter)) end return "(" .. table.concat(list, ", ") .. ")" end return "" end

--% This function generates a section header for a member. --@ member (table) information about the member, in API dump format --: (string) the generated header function p.generate_member_header(member) -- Start heading. local heading = "" heading = heading .. member_icon.make_member_icon(member) .. " "   -- Add value type. if member.ValueType then if member.ValueType.Category then heading = heading .. types.link_type_with_category(member.ValueType.Name, member.ValueType.Category) .. " "       else heading = heading .. types.link(member.ValueType.Name) .. " "       end end

-- Add return type. if member.ReturnType then if member.ReturnType.Category then heading = heading .. types.link_type_with_category(member.ReturnType.Name, member.ReturnType.Category) .. " "       else heading = heading .. types.link(member.ReturnType.Name) end end

-- Add the member name. heading = heading .. mw.text.tag("b", {       class = "member-name",        id = member.Name -- Add the member's name as an anchor so that it can be linked to conveniently.    }, member.Name)

-- Add the parameter list heading = heading .. p.generate_parameter_list(member.Parameters) -- Add tags heading = heading .. p.generate_tag_list(member.Tags) return heading end

--% This function generates a section for a member. --@ member (table) information about the member, in API dump format --@ description (string) a wikitext description of the callback --: (string) the generated section function p.generate_member_section(member, description) local section = {| class = "member-table" ===
 * class = "member-table-header" |

-- Add header local header = p.generate_member_header(member) section = section .. header

-- End the section heading. section = section .. " ===\n" local bullets = {}

-- Add category. if member.Category then table.insert(bullets, "* Category: " .. member.Category .. ".\n") end

-- Add security. if type(member.Security) == "string" then if member.Security ~= "" and member.Security ~= "None" then table.insert(bullets, "* Security: " .. member.Security .. ".\n") end elseif type(member.Security) == "table" then local read_security = member.Security.Read ~= "" and member.Security.Read ~= "None" and member.Security.Read local write_security = member.Security.Write ~= "" and member.Security.Write ~= "None" and member.Security.Write if read_security then table.insert(bullets, "* Read security: " .. read_security .. ".\n") end if write_security then table.insert(bullets, "* Write security: " .. write_security .. ".\n") end elseif member.Security then return error("Unexpected type for member security: " .. type(member.Security)) end

-- Add serialization. if member.Serialization then local serialization = "* Serialization: " if member.Serialization.CanLoad and member.Serialization.CanSave then serialization = serialization .. "can save and load.\n" elseif member.Serialization.CanLoad then serialization = serialization .. "can load only.\n" elseif member.Serialization.CanSave then serialization = serialization .. "can save only.\n" else serialization = serialization .. "cannot load or save.\n" end table.insert(bullets, serialization) end -- Add thread safety. if member.ThreadSafety then local thread_safety = "* Thread safety: " if member.ThreadSafety == "ReadSafe" then thread_safety = thread_safety .. "read safe" elseif member.ThreadSafety == "Unsafe" then thread_safety = thread_safety .. "unsafe" elseif member.ThreadSafety == "Safe" then thread_safety = thread_safety .. "safe" else thread_safety = "" end table.insert(bullets, thread_safety) end if description or #bullets > 0 then section = section .. [[|-       if description then            section = section .. "\n" .. description .. "\n"        end        if #bullets > 0 then            section = section .. "\n" .. table.concat(bullets, "")        end    end    section = section .. "\n|}"
 * class = "member-table-body" |]]

return section end

function p._member_list(args) return p.generate_member_list(args.name, args) end p.member_list = utils.make_wrapper_function(p._member_list)

return p