Module:Role: Difference between revisions
From Space Station 14 Wiki
Created blank page |
improved err message |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | |||
local getArgs = require('Module:Arguments').getArgs | |||
-- ======================= | |||
local roles_json = mw.loadJsonData("Module:Role/roles.json") | |||
local default_icon = "File:JobIconUnknown.png" | |||
local default_icon_size = "24px" | |||
-- ======================= | |||
local function assert_not_nil(value, error_message) | |||
if value == nil then | |||
if error_message == nil then | |||
error("value is nil") | |||
else | |||
error(error_message) | |||
end | |||
end | |||
end | |||
-- ========================= | |||
-- Lookups role from the json. If a role is defined, returns the object defining it, or `nil` otherwise. | |||
local function lookup_role(role) | |||
local result = roles_json[string.lower(role)] | |||
if type(result) == "string" then | |||
-- abbreviation. lookup actual definition. | |||
return roles_json[result] | |||
else | |||
return result | |||
end | |||
end | |||
-- Generates a role element. | |||
function p.main(frame) | |||
local args = getArgs(frame) | |||
local role = args[1] | |||
assert_not_nil(role, "failed to generate role label: role not provided") | |||
local icon_size = args[2] or default_icon_size | |||
local role_obj = lookup_role(role) | |||
assert_not_nil(role_obj, "failed to generate role label: role '" .. role .."' not found. Make sure the role is defined in the roles JSON in Module:Role") | |||
local role_display = role_obj["display"] | |||
assert_not_nil(role_display, "failed to generate role label: role '" .. role .."' does not have a 'display' property defined") | |||
local role_icon = role_obj["icon"] or default_icon | |||
local role_link = role_obj["link"] | |||
local label_wikitext = role_link and "[[" .. role_link .. "|" .. role_display .. "]]" or role_display | |||
local icon_wikitext = "[[" .. role_icon .. "|" .. icon_size .. "]]" | |||
return mw.html.create('span') | |||
:addClass('nowrap') | |||
:wikitext(icon_wikitext, " ", label_wikitext) | |||
:allDone() | |||
end | |||
-- Generates a list of roles. | |||
-- | |||
-- @param `columns` - amount of columns to generate for all roles. Default is `4`. | |||
function p.list_all_roles(frame) | |||
local columns = frame.columns or 4 | |||
local role_els = {} | |||
for role, _ in pairs(roles_json) do | |||
frame.args[1] = role | |||
local wrapper_el = mw.html.create('div') | |||
:node(p.main(frame)) | |||
table.insert(role_els, tostring(wrapper_el)) | |||
end | |||
return frame:expandTemplate{ | |||
title = 'Cols', | |||
args = { | |||
columns, | |||
table.concat(role_els, '') | |||
} | |||
} | |||
end | |||
return p |
Latest revision as of 03:31, 15 March 2025
Module documentation
|
---|
View or edit this documentation • (about module documentation) |
Uses JSON data
This module uses JSON data pages:
Implements {{Role}}.
How to add/edit/remove a role
Go to the roles JSON file (linked at the top).
Each key is a role name that can be used in the template. These must all be lowercase.
Each value is an object with properties:
{
"display": "[required] display name aka the label that would be displayed, for example: Atmospheric Technician",
"icon": "[optional] icon file name, for example: File:JobIconStationEngineer.png",
"link": "[optional] page name to link to, for example: Station Engineer"
}
Value can also be a string. This is used to make abbreviations. Note that abbreviations are only used to lookup roles, they cannot be displayed.
To make an abbreviation, put the key of another role as a value. For example, to add a an atmosian
abbreviation for an Atmospheric Technician
:
{
"atmosian": "atmospheric technician",
"atmospheric technician": {
"display": "Atmospheric Technician",
"icon": "File:JobIconAtmosphericTechnician.png",
"link": "Atmospheric Technician"
}
}
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- =======================
local roles_json = mw.loadJsonData("Module:Role/roles.json")
local default_icon = "File:JobIconUnknown.png"
local default_icon_size = "24px"
-- =======================
local function assert_not_nil(value, error_message)
if value == nil then
if error_message == nil then
error("value is nil")
else
error(error_message)
end
end
end
-- =========================
-- Lookups role from the json. If a role is defined, returns the object defining it, or `nil` otherwise.
local function lookup_role(role)
local result = roles_json[string.lower(role)]
if type(result) == "string" then
-- abbreviation. lookup actual definition.
return roles_json[result]
else
return result
end
end
-- Generates a role element.
function p.main(frame)
local args = getArgs(frame)
local role = args[1]
assert_not_nil(role, "failed to generate role label: role not provided")
local icon_size = args[2] or default_icon_size
local role_obj = lookup_role(role)
assert_not_nil(role_obj, "failed to generate role label: role '" .. role .."' not found. Make sure the role is defined in the roles JSON in Module:Role")
local role_display = role_obj["display"]
assert_not_nil(role_display, "failed to generate role label: role '" .. role .."' does not have a 'display' property defined")
local role_icon = role_obj["icon"] or default_icon
local role_link = role_obj["link"]
local label_wikitext = role_link and "[[" .. role_link .. "|" .. role_display .. "]]" or role_display
local icon_wikitext = "[[" .. role_icon .. "|" .. icon_size .. "]]"
return mw.html.create('span')
:addClass('nowrap')
:wikitext(icon_wikitext, " ", label_wikitext)
:allDone()
end
-- Generates a list of roles.
--
-- @param `columns` - amount of columns to generate for all roles. Default is `4`.
function p.list_all_roles(frame)
local columns = frame.columns or 4
local role_els = {}
for role, _ in pairs(roles_json) do
frame.args[1] = role
local wrapper_el = mw.html.create('div')
:node(p.main(frame))
table.insert(role_els, tostring(wrapper_el))
end
return frame:expandTemplate{
title = 'Cols',
args = {
columns,
table.concat(role_els, '')
}
}
end
return p