Module:Wiki Utility: Difference between revisions
From Against the Storm Official Wiki
m added standardized sizes for small, medium, large, and huge |
m added optional element IDs to link in resource links |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- Defines some utility functions for working with, among other things, mw library functions. | -- Defines some utility functions for working with, among other things, mw library functions. | ||
local Wiki_Utility = {} | local Wiki_Utility = {} | ||
-- The non-breaking space character. | -- The non-breaking space character. | ||
| Line 6: | Line 7: | ||
Wiki_Utility.StandardizedSizes = { | Wiki_Utility.StandardizedSizes = { | ||
["small"] = " | ["small"] = "x18px", | ||
["medium"] = "x30px", | ["medium"] = "x30px", | ||
["large"] = " | ["large"] = "x54px", | ||
["huge"] = " | ["huge"] = "x72px" | ||
} | |||
Wiki_Utility.GradeStars = { | |||
[0] = "☆", | |||
[1] = "★", | |||
[2] = "★★", | |||
[3] = "★★★" | |||
} | } | ||
| Line 21: | Line 29: | ||
-- Surrounds the given wikitext with the given classes. | -- Surrounds the given wikitext with the given classes. | ||
---@param wikitext Wikitext wikitext to surround | ---@param wikitext Wikitext wikitext to surround | ||
---@ | ---@vararg string classes to surround the wikitext with | ||
---@return Wikitext wikitext same wikitext, surrounded with the given classes | ---@return Wikitext wikitext same wikitext, surrounded with the given classes | ||
function Wiki_Utility.surroundWithClasses(wikitext, ...) | function Wiki_Utility.surroundWithClasses(wikitext, ...) | ||
return "<span class=\"" .. table.concat({...}, " ") .. "\">" .. wikitext .. "</span>" | return "<span class=\"" .. table.concat({...}, " ") .. "\">" .. wikitext .. "</span>" | ||
end | |||
-- Surrounds the given wikitext with a div with the given classes. | |||
---@param wikitext Wikitext wikitext to surround | |||
---@vararg string classes to surround the wikitext with | |||
---@return Wikitext wikitext same wikitext, surrounded with a new div | |||
function Wiki_Utility.surroundWithDiv(wikitext, ...) | |||
return "<div class=\"" .. table.concat({...}, " ") .. "\">" .. wikitext .. "</div>" | |||
end | end | ||
| Line 50: | Line 66: | ||
---@param pageName string name of the page | ---@param pageName string name of the page | ||
---@param text string text to display for the link | ---@param text string text to display for the link | ||
---@param targetElementID string|nil #name or #id of the DOM element to link directly to, if any | |||
---@return Wikitext wikitext wikitext markup for the link | ---@return Wikitext wikitext wikitext markup for the link | ||
function Wiki_Utility.renderWikiLink(pageName, text) | function Wiki_Utility.renderWikiLink(pageName, text, targetElementID) | ||
return "[[" .. pageName .. "|" .. text .. "]]" | return "[[" .. pageName .. (targetElementID and ("" .. targetElementID) or "") .. "|" .. text .. "]]" | ||
end | end | ||
-- Checks if the given icon size is valid, both as a number and within usable bounds as an icon. | |||
---@param iconSize string size of the icon including units, e.g., "20em" or "x16px" | |||
---@return boolean # `true` if the size string is a valid size | |||
---@return number|nil sizeN the number part of the specified size | |||
function Wiki_Utility.isValidIconSize(iconSize) | |||
local sizeN = tonumber(iconSize:match("^x?(%d+)")) | |||
return sizeN and sizeN >= 0 and sizeN <= 300 or false, sizeN | |||
end | |||
return Wiki_Utility | return Wiki_Utility | ||
Latest revision as of 03:36, 9 November 2025
Documentation for this module may be created at Module:Wiki Utility/doc
-- Defines some utility functions for working with, among other things, mw library functions.
local Wiki_Utility = {}
-- The non-breaking space character.
Wiki_Utility.NBSP = " "
Wiki_Utility.StandardizedSizes = {
["small"] = "x18px",
["medium"] = "x30px",
["large"] = "x54px",
["huge"] = "x72px"
}
Wiki_Utility.GradeStars = {
[0] = "☆",
[1] = "★",
[2] = "★★",
[3] = "★★★"
}
-- Surrounds the given wikitext with no-wrap tags.
---@param wikitext Wikitext wikitext to surround
---@return Wikitext wikitext same wikitext, surrounded with no-wrap tags
function Wiki_Utility.surroundWithNoWrap(wikitext)
return "<span style=\"white-space: nowrap;\">" .. wikitext .. "</span>"
end
-- Surrounds the given wikitext with the given classes.
---@param wikitext Wikitext wikitext to surround
---@vararg string classes to surround the wikitext with
---@return Wikitext wikitext same wikitext, surrounded with the given classes
function Wiki_Utility.surroundWithClasses(wikitext, ...)
return "<span class=\"" .. table.concat({...}, " ") .. "\">" .. wikitext .. "</span>"
end
-- Surrounds the given wikitext with a div with the given classes.
---@param wikitext Wikitext wikitext to surround
---@vararg string classes to surround the wikitext with
---@return Wikitext wikitext same wikitext, surrounded with a new div
function Wiki_Utility.surroundWithDiv(wikitext, ...)
return "<div class=\"" .. table.concat({...}, " ") .. "\">" .. wikitext .. "</div>"
end
-- Renders an icon with the given filename, size, link, and alt text.
---@param filename string filename of the icon
---@param size string size of the icon including units, e.g., "x16px"
---@param link string link to the icon
---@param alt string alt text for the icon
---@return Wikitext wikitext wikitext markup for the icon
function Wiki_Utility.renderIcon(filename, size, link, alt)
local wikitext = "[[File:" .. filename
if size then
wikitext = wikitext .. "|" .. size
end
if link then
wikitext = wikitext .. "|link=" .. link
end
if alt then
wikitext = wikitext .. "|" .. alt
end
return wikitext .. "]]"
end
-- Renders a link to the given page name with the given text.
---@param pageName string name of the page
---@param text string text to display for the link
---@param targetElementID string|nil #name or #id of the DOM element to link directly to, if any
---@return Wikitext wikitext wikitext markup for the link
function Wiki_Utility.renderWikiLink(pageName, text, targetElementID)
return "[[" .. pageName .. (targetElementID and ("" .. targetElementID) or "") .. "|" .. text .. "]]"
end
-- Checks if the given icon size is valid, both as a number and within usable bounds as an icon.
---@param iconSize string size of the icon including units, e.g., "20em" or "x16px"
---@return boolean # `true` if the size string is a valid size
---@return number|nil sizeN the number part of the specified size
function Wiki_Utility.isValidIconSize(iconSize)
local sizeN = tonumber(iconSize:match("^x?(%d+)"))
return sizeN and sizeN >= 0 and sizeN <= 300 or false, sizeN
end
return Wiki_Utility


