Module:ListGeneration: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:ListGeneration
--[[
-- A centralized and flexible module for generating various types of lists from delimited strings.
* Name: ListGeneration
--
* Author: Mark W. Datysgeld
-- This module is designed to replace fragmented list generation logic across the wiki,
* Description: Centralized and flexible module for generating various types of lists from delimited strings, designed to replace fragmented list generation logic
-- providing a single, powerful function to create multiple list formats.
* Notes: Handles semicolon-delimited strings automatically; supports multiple list modes (bullet, bullet_custom, invisible, comma); allows custom CSS classes on list container; provides itemHook for custom per-item processing and formatting; accepts both string input (auto-split) and pre-split table input
--
]]
-- Features:
--  * Handles semicolon-delimited strings automatically.
--  * Supports multiple list modes:
--    - 'bullet': Standard HTML unordered list (<ul>).
--    - 'bullet_custom': Unordered list with a custom Unicode character as the bullet.
--    - 'invisible': A bulleted list with the 'no-bullets' CSS class for an unstyled list.
--    - 'comma': A simple, comma-separated string of items.
--  * Allows for custom CSS classes on the list container.
--  * Provides an 'itemHook' for custom, per-item processing and formatting.
--
-- Usage:
--  local ListGeneration = require('Module:ListGeneration')
--  local options = {
--    mode = 'bullet',
--    listClass = 'my-custom-list',
--    itemHook = function(item) return '<b>' .. item .. '</b>' end
--  }
--  local htmlList = ListGeneration.createList('item1; item2; item3', options)


local p = {}
local p = {}
Line 52: Line 34:
     local items
     local items
     if type(input) == 'string' then
     if type(input) == 'string' then
         items = NormalizationText.splitMultiValueString(input)
        -- Default to splitting only by semicolon. This is a safer default than also splitting by "and", which can break up valid entity names.
        -- Callers that need more complex splitting logic should pre-split the string and pass a table instead.
        local semicolonOnlyPattern = {{pattern = ";%s*", replacement = ";"}}
         items = NormalizationText.splitMultiValueString(input, semicolonOnlyPattern)
     else
     else
         items = input
         items = input
Line 71: Line 56:
         end
         end
         items = processedItems
         items = processedItems
    end
    -- If there's only one item and no special formatting is needed, return it directly without list formatting
    if #items == 1 then
        local singleItem = items[1]
        local hasCustomBullet = (mode == 'bullet_custom' and bulletChar and bulletChar ~= '')
        local hasSpecialClass = (type(singleItem) == 'table' and singleItem.class and singleItem.class ~= '')
       
        -- Only bypass list formatting if there's no custom bullet AND no special class
        if not hasCustomBullet and not hasSpecialClass then
            if type(singleItem) == 'table' then
                return singleItem.content or ''
            else
                return singleItem
            end
        end
        -- If we have custom bullets or special classes, continue to list formatting below
     end
     end


Line 109: Line 111:
         local finalClass = 'template-list'
         local finalClass = 'template-list'
         if mode == 'invisible' then
         if mode == 'invisible' then
             finalClass = finalClass .. ' no-bullets'
             finalClass = finalClass .. ' list-style-none'
         end
         end
         if listClass and listClass ~= '' then
         if listClass and listClass ~= '' then