Module:LinkParser: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:LinkParser
--[[
-- Handles external and wiki link processing with caching
* Name: LinkParser
* Author: Mark W. Datysgeld
* Description: Comprehensive link handling module for both external links and internal wiki links with caching
* Notes: URL normalization (protocol stripping, www removal); wiki link extraction, preservation, and automatic application; caching for both link types; supports autoWikiLink field configuration; preserves wiki links during processing
]]


local p = {}
local p = {}
Line 74: Line 78:
-- @return Processed value or boolean based on mode
-- @return Processed value or boolean based on mode
function p.processWikiLink(value, mode)
function p.processWikiLink(value, mode)
    -- Default mode is "extract"
     mode = mode or "extract"
     mode = mode or "extract"
   
    -- Early return for nil or empty values
     if not value or value == "" then
     if not value or value == "" then
         return mode == "check" and false or value
         return mode == "check" and false or value
     end
     end
   
 
    -- Generate cache key
     local cacheKey = value .. ":" .. mode
     local cacheKey = value .. ":" .. mode
   
    -- Check cache first
     if wikiLinkCache[cacheKey] ~= nil then
     if wikiLinkCache[cacheKey] ~= nil then
         return wikiLinkCache[cacheKey]
         return wikiLinkCache[cacheKey]
     end
     end
   
 
     -- Verify wiki link format
     -- A single regex to capture the page name and optional display text
     local isWikiLink = value:match("^%[%[.-%]%]$") ~= nil
     local pageName, displayText = value:match("^%[%[([^|]+)|?(.*)%]%]$")
   
 
    -- Handle check mode
     if mode == "check" then
     if mode == "check" then
        local isWikiLink = (pageName ~= nil)
         wikiLinkCache[cacheKey] = isWikiLink
         wikiLinkCache[cacheKey] = isWikiLink
         return isWikiLink
         return isWikiLink
     end
     end
   
 
    -- Return original if not a wiki link
     if not pageName then
     if not isWikiLink then
         wikiLinkCache[cacheKey] = value
         wikiLinkCache[cacheKey] = value
         return value
         return value
     end
     end
   
 
     -- Parse wiki link components
     -- If displayText is empty, it means the format was [[PageName]], so use pageName
    local pageName, displayText
     if displayText == "" then
   
         displayText = pageName
    -- Match [[PageName|DisplayText]] pattern
     pageName, displayText = value:match("^%[%[([^%|%]]+)%|([^%]]+)%]%]$")
   
    if not pageName then
        -- Match [[PageName]] pattern
        pageName = value:match("^%[%[([^%|%]]+)%]%]$")
         displayText = pageName -- Display text equals page name
     end
     end
   
 
    -- Process based on mode
     local result
     local result
     if mode == "extract" then
     if mode == "extract" then
         result = pageName or value
         result = pageName
     elseif mode == "strip" then
     elseif mode == "strip" then
         result = displayText or value
         result = displayText
     else
     else
         -- Use extract for unknown modes
         result = pageName -- Default to extract for unknown modes
        result = pageName or value
     end
     end
   
 
    -- Cache result
     wikiLinkCache[cacheKey] = result
     wikiLinkCache[cacheKey] = result
   
     return result
     return result
end
end