Module:CanonicalForms: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
-- Module:CanonicalForms | -- Module:CanonicalForms | ||
-- | -- Provides normalization functions by stripping wiki-link markup and mapping input to canonical values. | ||
-- Templates supply mapping tables, each containing: | |||
-- Templates supply | -- • canonical: The standard display value. | ||
-- • canonical: The | -- • synonyms: Case-insensitive alternatives mapping to the canonical value. | ||
-- • synonyms: | -- • [optional] css: An associated CSS class. | ||
-- • [optional] css: An | |||
-- • [optional] category: A category string for auto-assignment. | -- • [optional] category: A category string for auto-assignment. | ||
-- | -- | ||
-- The normalize function | -- The normalize function removes wiki markup, converts input to lowercase, | ||
-- | -- and matches it against provided synonyms, returning the canonical value, css, and category if applicable. | ||
-- | -- | ||
-- | -- Example usage: | ||
-- local mapping = { | -- local mapping = { | ||
-- { canonical = "gTLD", synonyms = {"generic", "gtld", "tld"} }, | -- { canonical = "gTLD", synonyms = {"generic", "gtld", "tld"} }, | ||
| Line 18: | Line 16: | ||
-- } | -- } | ||
-- local canonical, css, category = require('Module:CanonicalForms').normalize(inputString, mapping) | -- local canonical, css, category = require('Module:CanonicalForms').normalize(inputString, mapping) | ||
local CanonicalForms = {} | local CanonicalForms = {} | ||
--- | --- Normalizes an input string based on a mapping table. | ||
-- Strips wiki link markup, converts | -- Strips wiki-link markup, converts to lowercase, and returns the canonical value. | ||
-- @param input The input string to normalize. | -- @param input The input string to normalize. | ||
-- @param mappingTable An array of mapping groups. | -- @param mappingTable An array of mapping groups. | ||
-- @return canonical The canonical | -- @return canonical The matched canonical value, or the cleaned input if no match is found. | ||
-- @return css (optional) | -- @return css (optional) An associated CSS class if available. | ||
-- @return category (optional) | -- @return category (optional) An associated category string if available. | ||
function CanonicalForms.normalize(input, mappingTable) | function CanonicalForms.normalize(input, mappingTable) | ||
if not input or input == "" then | if not input or input == "" then | ||
| Line 34: | Line 31: | ||
end | end | ||
-- Remove wiki-link markup: | -- Remove wiki-link markup: [[Brand TLD]] → "Brand TLD" | ||
local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1") | local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1"):lower() | ||
for _, group in ipairs(mappingTable) do | for _, group in ipairs(mappingTable) do | ||
for _, syn in ipairs(group.synonyms or {}) do | |||
if cleanInput == syn:lower() then | |||
return group.canonical, group.css, group.category | |||
end | end | ||
end | end | ||
end | end | ||
return cleanInput, nil, nil | return cleanInput, nil, nil | ||
end | end | ||
return CanonicalForms | return CanonicalForms | ||