Module:WikitextProcessor: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
-- Module:WikitextProcessor
--[[
-- Generalized content processor for wikitext formatting and frontend display, regardless of source (JSON, XML, database, user input, etc.)
* Name: WikitextProcessor
-- Features: Error handling; Three wiki link patterns: [[Page]], [[Page|text]], [[#anchor|text]]; Placeholder replacement with $VARIABLE$ syntax; Content normalization and whitespace cleanup
* Author: Mark W. Datysgeld
* Description: Generalized content processor for wikitext formatting and frontend display, regardless of source (JSON, XML, database, user input, etc.)
* Notes: Error handling; three wiki link patterns for page links, page links with custom text, and anchor links; placeholder replacement with $VARIABLE$ syntax; content normalization and whitespace cleanup; JavaScript escape hatch factory for NoticeHandler.js gadget integration to work around Scribunto/Lua environment-specific bugs
]]


local p = {}
local p = {}
Line 22: Line 25:
     end
     end
     return fallbackValue
     return fallbackValue
end
-- Interwiki minimal allowlist (test)
local INTERWIKI = {
    w = 'https://en.wikipedia.org/wiki/$1',
    wikipedia = 'https://en.wikipedia.org/wiki/$1',
}
local function resolveInterwiki(pageName)
    if type(pageName) ~= "string" then return nil end
    local prefix, title = pageName:match('^([^:]+):(.*)$')
    if not prefix or not title or prefix == '' or title == '' then return nil end
    local tpl = INTERWIKI[prefix:lower()]
    if not tpl then return nil end
    local spacesReplaced = (title:gsub(' ', '_'))
    return tpl:gsub('%$1', spacesReplaced)
end
end


Line 46: Line 33:
         pattern = '%[%[([^#|%]]+)%]%]',
         pattern = '%[%[([^#|%]]+)%]%]',
         processor = function(pageName, errorContext)
         processor = function(pageName, errorContext)
            -- Interwiki handling (minimal allowlist)
            local iw = resolveInterwiki(pageName)
            if iw then
                local pageNameStr = type(pageName) == "string" and pageName or tostring(pageName)
                local ok
                ok, pageNameStr = pcall(mw.text.encode, pageNameStr)
                if not ok then
                    return handleError(errorContext, 'encodeFailed', '[[' .. pageName .. ']]')
                end
                return string.format('<a href="%s" class="extiw">%s</a>', iw, pageNameStr)
            end
             local spacesReplaced = (pageName:gsub(' ', '_'))
             local spacesReplaced = (pageName:gsub(' ', '_'))
             local success, pageUrl = pcall(function()
             local success, pageUrl = pcall(function()
Line 85: Line 60:
         pattern = '%[%[([^#|%]]+)|([^%]]+)%]%]',
         pattern = '%[%[([^#|%]]+)|([^%]]+)%]%]',
         processor = function(pageName, text, errorContext)
         processor = function(pageName, text, errorContext)
            -- Interwiki handling (minimal allowlist)
            local iw = resolveInterwiki(pageName)
            if iw then
                local textStr = type(text) == "string" and text or tostring(text)
                local ok
                ok, textStr = pcall(mw.text.encode, textStr)
                if not ok then
                    return handleError(errorContext, 'encodeFailed', '[[' .. pageName .. '|' .. text .. ']]')
                end
                return string.format('<a href="%s" class="extiw">%s</a>', iw, textStr)
            end
             local success, pageUrl = pcall(function()
             local success, pageUrl = pcall(function()
                 return tostring(mw.uri.fullUrl((pageName:gsub(' ', '_'))))
                 return tostring(mw.uri.fullUrl((pageName:gsub(' ', '_'))))