Module:TemplateHelpers: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
* Name: TemplateHelpers | |||
* Author: Mark W. Datysgeld | |||
* Description: Common helper functions for template modules promoting code reuse and consistency across string processing, field handling, normalization, and block rendering | |||
* Notes: String processing functions for manipulating strings and template arguments; field processing functions for handling template fields and values; normalization wrappers for standardizing data formats; block generation helpers for rendering template blocks; category and semantic utilities (DEPRECATED wrappers for SemanticCategoryHelpers); configuration standardization for creating standard config structures; includes caching mechanism and multi-value string processing | |||
]] | |||
local p = {} | local p = {} | ||
-- Dependencies | |||
local linkParser = require('Module:LinkParser') | |||
local CountryData = require('Module:CountryData') | |||
local dateNormalization = require('Module:NormalizationDate') | |||
local CanonicalForms = require('Module:CanonicalForms') | |||
local NormalizationText = require('Module:NormalizationText') | |||
local ListGeneration = require('Module:ListGeneration') | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 94: | Line 97: | ||
end, | end, | ||
autoWikiLink = function(itemContent) | autoWikiLink = function(itemContent) | ||
-- Trim whitespace and check for existing wiki links | |||
local trimmedContent = p.trim(itemContent) | |||
if linkParser.processWikiLink(trimmedContent, "check") then | |||
-- Extract page name and display text | |||
local pageName, displayText = trimmedContent:match("^%[%[([^|]+)|?(.*)%]%]$") | |||
-- Normalize by trimming whitespace | |||
pageName = p.trim(pageName or "") | |||
displayText = p.trim(displayText or "") | |||
-- Reconstruct link, omitting display text if it's same as page name | |||
if displayText == "" or displayText == pageName then | |||
return string.format("[[%s]]", pageName) | |||
else | |||
return string.format("[[%s|%s]]", pageName, displayText) | |||
end | |||
else | |||
-- Not a wiki link, so just wrap it | |||
return string.format("[[%s]]", trimmedContent) | |||
end | |||
end | end | ||
} | } | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 296: | Line 310: | ||
-- Normalization Wrappers | -- Normalization Wrappers | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Wrapper around CountryData for consistent country formatting | -- Wrapper around CountryData for consistent country formatting | ||
| Line 482: | Line 468: | ||
-- Handle declarative lists first | -- Handle declarative lists first | ||
if field.list then | if field.list then | ||
local items = NormalizationText.splitMultiValueString(value) | -- Use a safe, semicolon-only pattern to avoid breaking up valid names. | ||
local semicolonOnlyPattern = {{pattern = ";%s*", replacement = ";"}} | |||
local items = NormalizationText.splitMultiValueString(value, semicolonOnlyPattern) | |||
if #items > 0 then | if #items > 0 then | ||
local options = {} | local options = {} | ||