Module:CanonicalForms: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 1: | Line 1: | ||
-- Module:CanonicalForms | -- Module:CanonicalForms | ||
-- Normalizes strings by removing wiki markup and mapping | -- Normalizes strings by removing wiki markup and mapping to canonical values. | ||
-- Mapping tables | -- Mapping tables: | ||
-- * canonical: | -- * canonical: Display value | ||
-- * synonyms: | -- * synonyms: Case-insensitive terms mapped to canonical | ||
-- * [optional] css: | -- * [optional] css: CSS class | ||
-- * [optional] category: Auto-assignment category | -- * [optional] category: Auto-assignment category | ||
-- | -- | ||
-- Example: | -- Example: | ||
| Line 17: | Line 17: | ||
--- Normalize an input string. | --- Normalize an input string. | ||
-- Removes wiki markup | -- Removes wiki markup, converts to lowercase, and maps to canonical form. | ||
-- @param input String to normalize. | -- @param input String to normalize. | ||
-- @param mappingTable | -- @param mappingTable Mapping groups with 'canonical', 'synonyms', and optional 'css'/'category'. | ||
-- @return canonical Matched value | -- @return canonical Matched value or cleaned input if no match. | ||
-- @return css Optional CSS class. | -- @return css Optional CSS class. | ||
-- @return category Optional category string. | -- @return category Optional category string. | ||
| Line 29: | Line 28: | ||
end | end | ||
-- Remove wiki-link markup | -- Remove wiki-link markup (e.g., "[[Brand TLD]]" → "Brand TLD") | ||
local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1"):lower() | local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1"):lower() | ||
-- Create | -- Create lookup table for faster matching (first call only) | ||
if not mappingTable._lookupCache then | if not mappingTable._lookupCache then | ||
local lookupCache = {} | local lookupCache = {} | ||
| Line 44: | Line 42: | ||
end | end | ||
-- Direct lookup | -- Direct lookup via cache | ||
local match = mappingTable._lookupCache[cleanInput] | local match = mappingTable._lookupCache[cleanInput] | ||
if match then | if match then | ||