Module:TemplateHelpers: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 73: | Line 73: | ||
-- Formats website URLs as either a single link or an HTML unordered list of links | -- Formats website URLs as either a single link or an HTML unordered list of links | ||
-- Uses splitMultiValueString for more flexible delimiter handling | -- Uses splitMultiValueString for more flexible delimiter handling | ||
-- Optimized to avoid unnecessary table creation for single websites | |||
function p.normalizeWebsites(value) | function p.normalizeWebsites(value) | ||
if not value or value == "" then return "" end | if not value or value == "" then return "" end | ||
-- Quick check for single website (no delimiters) | |||
if not value:match(";") and not value:match("%s+and%s+") then | |||
-- Single website case - avoid table creation entirely | |||
return string.format("[%s %s]", value, linkParser.strip(value)) | |||
end | |||
-- Multiple websites case | |||
local websites = p.splitMultiValueString(value) | local websites = p.splitMultiValueString(value) | ||
if #websites > 1 then | if #websites > 1 then | ||
| Line 194: | Line 203: | ||
standardizedInput = standardizedInput:gsub(delimiter.pattern, delimiter.replacement) | standardizedInput = standardizedInput:gsub(delimiter.pattern, delimiter.replacement) | ||
end | end | ||
-- Pre-allocate table based on delimiter count | |||
-- Count semicolons to estimate the number of items | |||
local count = 0 | |||
for _ in standardizedInput:gmatch(";") do | |||
count = count + 1 | |||
end | |||
-- Pre-allocate table with estimated size (count+1 for the last item) | |||
local items = {} | |||
-- Split by semicolons and return the array | -- Split by semicolons and return the array | ||
local | local index = 1 | ||
for item in standardizedInput:gmatch("[^;]+") do | for item in standardizedInput:gmatch("[^;]+") do | ||
local trimmed = item:match("^%s*(.-)%s*$") | local trimmed = item:match("^%s*(.-)%s*$") | ||
if trimmed and trimmed ~= "" then | if trimmed and trimmed ~= "" then | ||
items[index] = trimmed | |||
index = index + 1 | |||
end | end | ||
end | end | ||