Module:Type

local utils = require('Module:Utils') local links = require('Dev:Links')

local primitive_types = utils.array_to_set {'int', 'double', 'float', 'boolean', 'string', 'number', 'userdata', 'function', 'void', 'nil', 'table', 'bool'}

local p = {}

--% This function is used to format a type and link. --% When the category of the type is not known, the  function can be used instead. --@ type_name (string) the name of the type --@ type_category (string) the category of the type, for example "Primitive", "Class", "DataType", "Enum", or "Group" --: (string) a formatted type, with a link if appropriate function p.link_type_with_category(type_name, type_category) local span = mw.html.create("span") :addClass("value-type") if type_category == "Class" then span :addClass("class-link") :wikitext(links.link("Class:" .. type_name, type_name, "local")) elseif type_category == "Enum" then span :addClass("enum-link") :wikitext(links.link(type_name, type_name, "local")) elseif type_category == "DataType" then span :wikitext(links.link(type_name, type_name, "local")) elseif type_category == "Primitive" then span :addClass("primitive-type") :css("color", "darkgreen") :wikitext(type_name) elseif type_category == "Group" then span :addClass("group-type") :css("color", "darkgreen") :wikitext(type_name) else return error("Unknown type category: " .. type_category .. ". Either this is a mistake or the category should be added to Module:Type.") end return tostring(span) end

--% This function is used to format a type and link. --% When the category of the type is known, prefer to use the  function. --@ type_name (string) the name of the type --: (string) a formatted type, with a link if appropriate function p.link(type_name) if mw.ustring.sub(type_name, -2, -1) == "[]" then -- The type is an array. return p.link(mw.ustring.sub(type_name, 1, -3)) .. "[]"	else local span = mw.html.create("span") :addClass("value-type") if primitive_types[type_name] then span :addClass("primitive-type") :css("color", "darkgreen") :wikitext(type_name) else local title = mw.title.new(type_name, 'Class') -- If there exists a class with the same name as the type, link to its page. if title and title.exists then span :addClass("class-link") :wikitext(links.link(tostring(title), type_name, "local")) else title = mw.title.new(type_name) -- If there exists a page with the same name as the type, link to it. if title and title.exists then span :wikitext(links.link(tostring(title), type_name, "local")) else span:wikitext(type_name) end end end return tostring(span) end end

function p._link_type(args) return p.link(args.type) end

p.link_type = utils.make_wrapper_function(p._link_type)

return p