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( | -- Step 1: Encode anchor | ||
local success1, encodedAnchor = pcall(mw.uri.anchorEncode, anchor) | |||
if not success1 then | if not success1 then | ||
return handleProcessingError(errorContext, 'urlFailedPattern3', | |||
'Error: ' .. tostring(encodedAnchor), | |||
'[[#' .. anchor .. '|' .. text .. ']]') | |||
end | end | ||
-- Step 2: Encode text | |||
local textStr = type(text) == "string" and text or tostring(text) | |||
local success2, encodedText = pcall(mw.text.encode, textStr) | |||
if not success2 then | if not success2 then | ||
return handleProcessingError(errorContext, 'encodeFailedPattern3', | |||
'Error: ' .. tostring(encodedText), | |||
'[[#' .. anchor .. '|' .. text .. ']]') | |||
end | end | ||
local success3, result = pcall( | -- Step 3: Format HTML | ||
local success3, result = pcall(string.format, '<a href="#%s">%s</a>', encodedAnchor, encodedText) | |||
if not success3 then | if not success3 then | ||
return handleProcessingError(errorContext, 'formatFailedPattern3', | |||
'Error: ' .. tostring(result), | |||
'[[#' .. anchor .. '|' .. text .. ']]') | |||
end | end | ||