Module:Strings
Appearance
Documentation for this module may be created at Module:Strings/doc
local p = {}
--[[
Converts a string to all uppercase letters.
Usage on a wiki page: {{#invoke:Strings|to_upper|text to convert}}
]]
function p.to_upper(frame)
local text = frame.args[1] or ''
return mw.ustring.upper(text)
end
--[[
Converts a string to all lowercase letters.
Usage on a wiki page: {{#invoke:Strings|to_lower|text to convert}}
]]
function p.to_lower(frame)
local text = frame.args[1] or ''
return mw.ustring.lower(text)
end
--[[
Returns the first item from a comma-separated list
stored in a MediaWiki: message page.
Usage: {{#invoke:Strings|getFirst|message-name}}
]]
function p.getFirst(frame)
-- Get the message name passed from the template (e.g., 'pf-values-sex')
local messageName = frame.args[1]
if not messageName then
return '' -- Return nothing if no message name is provided
end
-- Use the parser to get the internationalized content of the message
local messageContent = frame:callParserFunction('int', messageName)
-- Split the string by the comma and return the first part
local parts = mw.text.split(messageContent, '%s*,%s*') -- Splits by comma, ignoring whitespace
return parts[1] or ''
end
--[[
This function takes a comma-separated string of wikitext,
sorts the items alphabetically, and returns the sorted string.
]]
function p.tokenSort(frame)
-- Get the string passed as the first argument in the #invoke call.
-- e.g., {{#invoke:LinkSorter|sort|[[C]], [[A]], [[B]]}}
local inputString = frame.args[1] or ''
-- If the input is empty or just whitespace, return nothing.
if inputString:match('^%s*$') then
return ''
end
-- Create a table to hold the individual link strings.
local linksTable = {}
-- Split the input string by commas and trim any whitespace around each link.
-- The pattern captures everything that is not a comma.
for link in mw.ustring.gmatch(inputString, '([^,]+)') do
-- Trim whitespace from the beginning and end of the captured string.
local trimmedLink = mw.ustring.gsub(link, '^%s*(.-)%s*$', '%1')
table.insert(linksTable, trimmedLink)
end
-- If the table is empty after processing, return nothing.
if #linksTable == 0 then
return ''
end
-- Sort the table alphabetically. This is the core step.
table.sort(linksTable)
-- Join the sorted table back into a single string, separated by a comma and a space.
return table.concat(linksTable, ', ')
end
return p