(Jump to code)Module Documentation[view] [edit] [latest diff] [hist] [purge]
Page Tools: Create Sandbox View subpages View links
Module Links: Create talk Latest Diff Page Logs
Page Protected from Editing
This page is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, it is subject to Page Protection.
This page is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, it is subject to Page Protection.
Page Actions: Unprotect • Change Protection • Protection log
This module is used to ease the loading of other modules. It does this by utilizing the global variable table, which allows for more flexibility in variables. It will also automatically load several modules.
Loading the module
To load this module and make it available for use, Add this line of code to the start of your module:
local loadLib = require('Module:LoadLib')
Auto-loaded Modules
This module automatically loads a preset list of commonly used modules. These names will be accessible through the Global Table of your code environment (_G.name or name). To better identify names for code tracing, see #Autoloads for a list of names.
Syntax
loadLib(<_G>(table), <t>(table), <options>(any))
<_G>- The<_G>parameter is the global table. This must be the specific variable _G or the function will fail.<t>- The collection of modules to load. Each key name in this table represents the variable to save the loaded module under. each key may be a table, with the table containing options for the loader for that specific module. See the section below for more details.
Use
This module provides a singe function to load other modules.
This function has options that emulate normal lua code. Note that when ever using this function, you must use the following code format or this function or it will fail.
_G = loadLib(_G, {
-- Any modules to load go here
})
Note that in these examples, sometimes the named table index is not omitted, which prevents the entire module from being loaded. If the table index is omitted, the loader will load the module under the index name.
Variable setting
You can also add the field setVars to true to make the loader set each method in the module as a variable in the loaded module.
Example
_G = loadLib(_G, {
moduleName={ 'Module:ModuleName', setVars=true },
-- Any other modules to load go here
})
Once this code is added to the module, any methods described in Module:ModuleName will be available as variables in the loaded modules with the variable name being the respective method in Module:ModuleName.
Paths
This module also provides basic relative paths and substitution variables. Any relative paths are substituted to absolute paths. These basic relative paths are the following:
- Note in these examples that the current page name is Module:String/Data.
- A / at the start of the module name denotes a subpage of the parent (not the root) module.
- Example: /Test → Module:String/Test
- Example: /Foo/Test → Module:String/Foo/Test
- A ./ at the start of the module name denotes a subpage of the root module.
- Example: ./Test → Module:String/Test
- Example: ./Foo/Test → Module:String/Foo/Test
- A ../ at the start of the module name denotes a subpage of the current module.
- Example: ../Test → Module:String/Data/Test
- Example: ../Foo/Test → Module:String/Data/Foo/Test
Autoloads
LoadLib automatically loads a preset list of commonly used modules. These modules may be found in Autoloads.
These may be disabled entirely using options.doAutoLoads or some modules may be filtered using the options.skip parameter.
| List of Automatically Loaded Names | |
| Name | Value Equivalent |
|---|---|
| string | require('Module:String')
|
| makeLink | require('Module:String').makeLink
|
| externalUrl | require('Module:String').externalUrl
|
| fullUrl | require('Module:String').fullUrl
|
| wrapTag | require('Module:String').wrapTag
|
| wrapHtml | require('Module:String').wrapHtml
|
| wrapLink | require('Module:String').wrapLink
|
| makeTitle | require('Module:String').makeTitle
|
| table | require('Module:Table')
|
| constructor | require('Module:Constructor')
|
| arguments | require('Module:Arguments')
|
| getArgs | require('Module:Arguments').getArgs
|
| yesno | require('Module:Yesno')
|
| color | require('Module:Color')
|
| makeColor | require('Module:Color')._colorTemplates
|
| libraryUtil | require('Module:LibraryUtil')
|
| currency | require('Module:Currency')
|
| coins | require('Module:Currency').coins
|
| gems | require('Module:Currency').gems
|
| bits | require('Module:Currency').bits
|
| rarityTier | require('Module:RarityTier')
|
| makeRarity | require('Module:RarityTier')._link
|
| rarityColor | require('Module:RarityTier')._colorText
|
| makeStat | require('Module:Statname')._getStatName
|
| item | require('Module:Item')
|
| itemDisplay | require('Module:Item')._itemDisplay
|
| link | require('Module:Link')
|
| list | require('Module:List')
|
| multiRequire | require('Module:Multirequire')
|
Variables
You can also use variables (denoted by ${<name>}) in the paths. These variables are substituted with the contents they repersent. The list of built-in variables is as follows:
- ${root} denotes the root page name.
- ${page} denotes the current page name.
- ${subpage} denotes the current subpage name.
- ${base} and ${parent} denotes the base (not root) page name.
- ${fullpage} denotes the current page name (with the namespace prefix).
Custom variables can be created by adding a table in the subst or substVars field in the <options> parameter.
The table index in the table for variables represents the variable name, and the table value its contents.
A custom variable may be called with the following syntax: ${var:<name>}
Example
The example variable here is set to Foo and it's name is foo. The page name in the example is Module:Test
- Module:Test/${page}/${var:foo} → Module:Test/Test/Foo
- Module:${var:foo}/${page} → Module:Foo/Test
Code Example
The current page name in this example is Module:String.
Note that the current path "Module:Test/${page}/${var:foo}" will get subsituted to "Module:Test/String/Foo".
loadLib(_G, {
test="Module:Test/${page}/${var:foo}",
}, {
substVars={ foo="Foo" },
})
Note the module will throw an error if a custom variable does not exist or there is no substitution table to draw from. All methods from Module:Test/String/Foo will now be availible under the test object in the module the function was used.
Options
The following consists of different options for the loader to load the module. Note that you can group each of these options together in one loadLib() call.
Option: Simple Module Loading
This option replicates a standard require() call. See the section below for the Replicated code.
_G = loadLib(_G, {
moduleName='Module:ModuleName'
-- Any other modules to load go here
})
Once this line of code is added to your module, the module and its methods are available under the table index name. In this case, all methods of Module:ModuleName are available under the variable moduleName.
Replicated code
local moduleName = require('Module:ModuleName')
Option: Using Field
This option only loads a field from a library to the global table.
1
This option replicates a require() call and getting a single field from the module and setting that field name as the variable to be used. See the section below for the emulated code.
_G = loadLib(_G, {
{ 'Module:ModuleName', field='foo' }
-- Any other modules to load go here
})
Once this code is added, the method foo() from Module:ModuleName is available under the foo variable.
Replicated code
local foo = require('Module:ModuleName').foo
2
This option replicates a require() call and getting a single field from the module and setting that field name as a different variable from the method name. See the section below for the emulated code.
_G = loadLib(_G, {
bar = { 'Module:ModuleName', field='foo' }
-- Any other modules to load go here
})
Once this code is added, the method foo() from Module:ModuleName is available under the bar variable.
Replicated code
local bar = require('Module:ModuleName').foo
Option: Using Values
1
This option replicates a require() call and getting multiple fields from the module and setting that field name as the variable to be used. See the section below for the emulated code.
_G = loadLib(_G, {
{ 'Module:ModuleName', values={ 'foo', 'bar', 'baz' }
-- Any other modules to load go here
})
Once this code is added, the methods foo(), bar(), and baz() from Module:ModuleName is available under the foo, bar, and baz variables respectively.
Replicated code
local foo = require('Module:ModuleName').foo
local bar = require('Module:ModuleName').bar
local baz = require('Module:ModuleName').baz
2
This option replicates a require() call and getting multiple fields from the module and setting that field name as a different variable from the respective method name. See the section below for the emulated code.
_G = loadLib(_G, {
{ 'Module:ModuleName', values={ qux='foo', lorem='bar', ipisum='baz' }
-- Any other modules to load go here
})
Once this code is added, the methods foo(), bar(), and baz() from Module:ModuleName is available under the qux, lorem, and ipisum variables respectively.
Replicated code
local qux = require('Module:ModuleName').foo
local lorem = require('Module:ModuleName').bar
local ipisum = require('Module:ModuleName').baz
3
This option uses the value field in the two methods above, but adds a key. This will load the library into the global table besides the values. See the section below for the emulated code.
_G = loadLib(_G, {
moduleName = { 'Module:ModuleName', values={ qux='foo', lorem='bar', ipisum='baz' }
-- Any other modules to load go here
})
Once this code is added, the methods foo(), bar(), and baz() from Module:ModuleName is available under the qux, lorem, and ipisum variables respectively. Also, all methods of Module:ModuleName are available under the variable moduleName.
Replicated code
local moduleName = require('Module:ModuleName')
local qux = moduleName.foo
local lorem = moduleName.bar
local ipisum = moduleName.baz
Submodules
See Also
| Hypixel SkyBlock Wiki Standard Lua Libraries (hsw/stdll) v · d · e | |
| Type | Libraries |
|---|---|
| Module Loading Utilities |
|
| General Utilities |
|
| Meta Modules | |
| Object-oriented |
|
| Caching | |
Module Code
require('Module:LibraryUtil')
local autoloadsConfig = require('Module:LoadLib/Autoloads')
local titleObj = mw.title.getCurrentTitle()
local pagename = titleObj.fullText
local implicitLoads = {}
local autoloads = autoloadsConfig.callList
local moduleList = autoloadsConfig.moduleList
local function toCamelCase(s)
return s
:gsub(' (.)(.*)', function(a, b)
return a:upper() .. b
end)
:gsub('[^%a]+', '')
:gsub('^(.)(.*)', function(a, b)
return a:lower() .. b
end)
end
for _, v in ipairs(moduleList) do
implicitLoads[v] = 1
implicitLoads['Module:' .. v] = 1
end
moduleList = nil
local function isNaN(v)
return type(v) == 'number' and tostring(v) == '-nan'
end
local function fallback(...)
local val
local values = { ... }
local lim = select('#', ...)
local i = 0
local def = ({...})[lim]
local f = {
[0] = false,
['f'] = false,
['false'] = false,
['0'] = false,
['-0'] = false,
['+0'] = false,
['off'] = false,
['of'] = false,
['n'] = false,
['no'] = false,
[false] = false,
}
while lim > i do
i = i+1
val = values[i]
if val ~= nil then
local v = f[val]
if v == nil and not isNaN(val) then
return true
else
return false
end
end
end
return def
end
return function(...)
local globalTable, lib, options = checkArgs({ 'table', { 'table', nilOk = true }, { 'table', nilOk = true } }, ...)
local lib = lib or {}
local options = options or {}
local setVars, doAutoLoads, substVars =
fallback(options.setVars, options.set, options[1], false),
fallback(options.doAutoLoads, options.doLoads, options.auto, options[2], true),
fallback(options.substVars, options.subst, options[3], false)
local skip = {}
options.skip = options.skip or {}
if #options.skip > 0 then
for _, v in ipairs(options.skip) do
options.skip[v] = 1
end
end
if doAutoLoads then
for key, value in pairs(lib) do
local mName = type(value) == "string" and value or value[1] or value.m or value.module
if implicitLoads[mName] or options.skip[mName] then
skip[mName] = true
end
end
for key, value in pairs(autoloads) do
local mName = type(value) == "string" and value or value[1] or value.m or value.module or ''
if not skip[mName] then
if type(key) == 'number' then
table.insert(lib, value)
else
lib[key] = value
end
end
end
end
-- Iterate over supplied module names/variables/fields to get
for key, value in pairs(lib) do
local tpKey = type(key)
local varToSet, fieldToGet, setVarsTmp, tmp
-- If key is a table, set some values
if type(value) == "table" then
varToSet, fieldToGet, setVarsTmp, tmp = unpack{
value.varToSet or value.value or value.values or value[3],
value.fieldToGet or value.field or value[2],
value.loadVars or value.setVars or value[4],
value.module or value.m or value[1],
}
end
-- Set module name
local value = tmp and tmp or value
local oldVal = value
-- require() enhancements
if value:match('^%/') then
value = titleObj.fullText .. value
elseif value:match('^%.%/') then
value = titleObj.baseText .. value:gsub('%.', '')
end
if value:match('%$%{.-%}') then
local tpSubstVars = type(substVars)
if value:match('%${parent}') or value:match('^%${base}$') then
value = value:gsub('%${parent}', titleObj.baseText):gsub('%${base}', titleObj.baseText)
end
if value:match('%${root}') then
value = value:gsub('%${root}', titleObj.rootText)
end
if value:match('%${fullpage}') then
value = value:gsub('%${fullpage}', titleObj.fullText)
end
if value:match('%${page}') then
value = value:gsub('%${page}', titleObj.text)
end
if value:match('%${subpage}') then
value = value:gsub('%${subpage}', titleObj.subpageText)
end
if tpSubstVars == "table" then
for k, v in pairs(substVars) do
substVars[k:lower()] = v
end
for var in value:gmatch('%${var:(.-)}') do
v = substVars[tostring(var):lower()]
if not v then error('Unknown variable "' .. var .. '" in require path "' .. value .. '"', 2) end
value = value:gsub('%${var:' .. var .. '}', v)
end
end
if tpSubstVars ~= "table" and value:match('%${var:(.-)}') then
error('Require path variables must have subsituion table to draw from (missing option `substVars`)', 2)
end
end
local mName = 'Module:' .. value:gsub('Module:', '')
-- Load actual module
local res = globalTable.require(mName)
local lib = res
-- Key the module was saved in
local savedKey = tpKey == "number" and toCamelCase(mName:gsub('Module:', '')) or key
local function err(msg, ...)
error(string.format('Invalid loader settings for module %q for index %q: ' .. msg, mName, key, ...), 3)
end
local tpFieldToGet = type(fieldToGet)
local tpVarToSet = type(varToSet)
-- Check types on variable to be set
if tpFieldToGet ~= "string" and tpFieldToGet ~= "nil" then
err('function name to load must be a string or nil')
end
-- If the function supplied a singular field to get, set the global table index as the field
if (varToSet or fieldToGet) and tpVarToSet ~= "table" and tpKey == "number" then
globalTable[varToSet or fieldToGet] = lib[fieldToGet] or lib[varToSet]
-- Extra case if the user did not specify a field to get
elseif tpKey ~= "number" and fieldToGet then
globalTable[key] = lib[fieldToGet]
end
-- Extra case if user set the values to get as a table
if tpVarToSet == "table" and not fieldToGet then
for k, v in pairs(varToSet) do
local tpK = type(k)
-- Check types on variables to be set
if tpK ~= "number" and tpK ~= "string" then
error(string.format(
'Invalid table index type in loader settings for module %q for index %q: index must be a string', mName, key
), 2)
-- Also check types on the value to search for
elseif type(v) ~= "string" then
error(string.format(
'Invalid table index value type in loader settings for module %q for index %q: index must be a string', mName, key
), 2)
-- If the function is not found, error
elseif not lib[v] then
error(string.format(
'Invalid loader configuration settings for module %q at index %q: specified function %q not found on %q', mName, key, v, mName
), 2)
end
if tpK == "number" then
globalTable[v] = lib[v]
else
globalTable[k] = lib[v]
end
end
end
-- Else, set the loaded variable name as the module
if tpKey ~= "number" and not fieldToGet then
globalTable[savedKey] = lib
end
-- If the option `setVars` is true, set global variables as required module methods
if not fieldToGet and (setVarsTmp or setVars) and type(lib) == "table" then
for k, v in pairs(lib) do
globalTable[k] = v
end
end
end
return globalTable
end