Module:T-Campaign: 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:
-- T-Campaign.lua
-- Module:T-Campaign.lua
-- Generic campaign template that dynamically loads campaign data from JSON files
-- Generic campaign template that dynamically loads campaign data from JSON files
-- Usage: {{#invoke:T-Campaign|render|campaign_name=ASP2025}}
-- Usage: {{#invoke:T-Campaign|render|campaign_name=NAME}}


local p = {}
local p = {}
Line 172: Line 172:
         local banner = args._campaign_data.banner
         local banner = args._campaign_data.banner
         local bannerContent = banner.content or ""
         local bannerContent = banner.content or ""
        local titleText = template.config.constants.title or "Campaign"
       
         -- Combine generic notice-box class with specific campaign class
         -- Combine generic notice-box class with specific campaign class
         local cssClass = "notice-box"
         local cssClass = "notice-box"
Line 183: Line 185:
         end
         end
          
          
         -- Step 1: Get the raw title value
         -- Use the centralized NoticeFactory to create the notice
        local rawTitle = args._campaign_data.defaults.title or "Campaign"
         local noticeOptions = {
        local finalTitle = rawTitle
             type = "campaign-js",
       
        -- Step 2: Launder the string to work around suspected Scribunto bug
        local function launderString(inputStr)
            if type(inputStr) ~= 'string' then return tostring(inputStr) end
            local newStr = ""
            for i = 1, #inputStr do
                newStr = newStr .. string.char(string.byte(inputStr, i))
            end
            return newStr
        end
 
        local cleanTitle = launderString(rawTitle)
       
        ErrorHandling.addStatus(context, 'campaignBanner', 'Laundered String', 'Original Type: ' .. type(rawTitle) .. ', Cleaned Value: "' .. cleanTitle .. '"')
 
        -- If it's a table, we need to serialize it to see what's inside.
        if type(rawTitle) == 'table' then
            -- This is a simple serializer for demonstration.
            local serialized = "{"
            for k, v in pairs(rawTitle) do
                serialized = serialized .. ' [' .. tostring(k) .. '] = "' .. tostring(v) .. '",'
            end
            if serialized:sub(-1) == ',' then
                serialized = serialized:sub(1, -2)
            end
            serialized = serialized .. " }"
            ErrorHandling.addStatus(context, 'campaignBanner', 'Raw title is a table', 'Serialized content: ' .. serialized)
           
            -- Attempt to extract a string value. Let's assume the first value is what we want.
            if rawTitle[1] then
                cleanTitle = launderString(tostring(rawTitle[1]))
                ErrorHandling.addStatus(context, 'campaignBanner', 'Extracted value from table', 'Using value at index 1: "' .. cleanTitle .. '"')
            else
                -- Fallback if we can't find a value at index 1
                cleanTitle = launderString(tostring(rawTitle))
            end
        end
 
        -- Step 3: Use the processed title for the placeholder
        local placeholderValues = {
            CAMPAIGN_NAME = cleanTitle
        }
       
        ErrorHandling.addStatus(context, 'campaignBanner', 'Final placeholder value set', 'CAMPAIGN_NAME = "' .. cleanTitle .. '"')
       
        bannerContent = WikitextProcessor.processContentForFrontend(bannerContent, placeholderValues, context)
       
         local noticeData = {
             type = "campaign",
             position = "top",
             position = "top",
             content = bannerContent,
             content = bannerContent,
            title = titleText,
             cssClass = cssClass
             cssClass = cssClass
         }
         }
          
          
         local success, result = pcall(function()
         return WikitextProcessor.createNoticeForJS(noticeOptions) .. ErrorHandling.formatCombinedOutput(context)
            return string.format(
                '<div style="display:none" class="notice-data" data-notice-type="%s" data-notice-position="%s" data-notice-content="%s" data-notice-css="%s"></div>',
                mw.text.encode(noticeData.type),
                mw.text.encode(noticeData.position),
                mw.text.encode(noticeData.content),
                mw.text.encode(noticeData.cssClass)
            )
        end)
       
        if success then
            return result .. ErrorHandling.formatCombinedOutput(context)
        else
            ErrorHandling.addError(context, 'campaignBanner', 'Data attribute creation failed', tostring(result), false)
            return ErrorHandling.formatCombinedOutput(context)
        end
     end
     end
}
}
Line 430: Line 369:
     -- Always show campaign content fields (they'll show placeholder text when empty)
     -- Always show campaign content fields (they'll show placeholder text when empty)
     for _, fieldDef in ipairs(campaignData.field_definitions) do
     for _, fieldDef in ipairs(campaignData.field_definitions) do
         table.insert(fields, {
         -- CRITICAL: Skip 'title' as it is not a content field
            key = fieldDef.key,
        if fieldDef.key ~= "title" then
            label = fieldDef.label,
            table.insert(fields, {
            type = fieldDef.type
                key = fieldDef.key,
        })
                label = fieldDef.label,
                type = fieldDef.type
            })
        end
     end
     end
      
      
Line 454: Line 396:
     end
     end
      
      
     -- Add campaign-specific category
     -- Add campaign-specific category, defaulting to template_id
     template.config.categories.base = {campaignName}
    local category_value = campaignData.category or campaignData.template_id
     template.config.categories.base = {category_value}
      
      
     return args
     return args