Module:WikitextProcessor: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 158: Line 158:
         pattern = '%[%[#([^|%]]+)|([^%]]+)%]%]',
         pattern = '%[%[#([^|%]]+)|([^%]]+)%]%]',
         processor = function(anchor, text, errorContext)
         processor = function(anchor, text, errorContext)
             local success1, encodedAnchor = pcall(function()
            -- Step 1: Encode anchor
                return mw.uri.anchorEncode(anchor)
             local success1, encodedAnchor = pcall(mw.uri.anchorEncode, anchor)
            end)
              
              
             if not success1 then
             if not success1 then
                 if errorContext then
                 return handleProcessingError(errorContext, 'urlFailedPattern3',
                    ErrorHandling.addStatus(errorContext, 'campaignBanner', 'Pattern 3 mw.uri.anchorEncode failed', 'Error: ' .. tostring(encodedAnchor))
                    'Error: ' .. tostring(encodedAnchor),
                end
                    '[[#' .. anchor .. '|' .. text .. ']]')
                return '[[#' .. anchor .. '|' .. text .. ']]'
             end
             end
              
              
             local success2, encodedText = pcall(function()
             -- Step 2: Encode text
                -- Ensure text is a string before encoding
            local textStr = type(text) == "string" and text or tostring(text)
                local textStr = type(text) == "string" and text or tostring(text)
            local success2, encodedText = pcall(mw.text.encode, textStr)
                return mw.text.encode(textStr)
            end)
              
              
             if not success2 then
             if not success2 then
                 if errorContext then
                 return handleProcessingError(errorContext, 'encodeFailedPattern3',
                    ErrorHandling.addStatus(errorContext, 'campaignBanner', 'Pattern 3 mw.text.encode failed', 'Error: ' .. tostring(encodedText))
                    'Error: ' .. tostring(encodedText),
                end
                    '[[#' .. anchor .. '|' .. text .. ']]')
                return '[[#' .. anchor .. '|' .. text .. ']]'
             end
             end
              
              
             local success3, result = pcall(function()
            -- Step 3: Format HTML
                return string.format('<a href="#%s">%s</a>', encodedAnchor, encodedText)
             local success3, result = pcall(string.format, '<a href="#%s">%s</a>', encodedAnchor, encodedText)
            end)
              
              
             if not success3 then
             if not success3 then
                 if errorContext then
                 return handleProcessingError(errorContext, 'formatFailedPattern3',
                    ErrorHandling.addStatus(errorContext, 'campaignBanner', 'Pattern 3 string.format failed', 'Error: ' .. tostring(result))
                    'Error: ' .. tostring(result),
                end
                    '[[#' .. anchor .. '|' .. text .. ']]')
                return '[[#' .. anchor .. '|' .. text .. ']]'
             end
             end