Module:LinkParser: Difference between revisions
Appearance
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 1: | Line 1: | ||
-- Module:LinkParser | -- Module:LinkParser | ||
-- Handles | -- Handles external and wiki link processing with caching | ||
local p = {} | local p = {} | ||
| Line 13: | Line 8: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Cache for | -- Cache for processed URLs and wiki links (non-persistent) | ||
local stripCache = {} | local stripCache = {} | ||
local wikiLinkCache = {} | local wikiLinkCache = {} | ||
| Line 21: | Line 16: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Strip URL protocol, www prefix, and trailing slash | ||
function p.strip(link) | function p.strip(link) | ||
-- Input validation | -- Input validation | ||
| Line 33: | Line 28: | ||
end | end | ||
-- | -- Chain operations to minimize string allocations | ||
local result = link:gsub("^%a+://", ""):gsub("^www%.", ""):gsub("/$", "") | local result = link:gsub("^%a+://", ""):gsub("^www%.", ""):gsub("/$", "") | ||
| Line 42: | Line 37: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Creates MediaWiki link: [ originalURL strippedDisplayText ] | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function p.render(frame) | function p.render(frame) | ||
| Line 59: | Line 53: | ||
local rawLink = args["link"] or "" | local rawLink = args["link"] or "" | ||
-- | -- Skip empty links | ||
if not rawLink or rawLink == "" then | if not rawLink or rawLink == "" then | ||
return "" | return "" | ||
end | end | ||
-- | -- Clean URL for display | ||
local displayText = p.strip(rawLink) | local displayText = p.strip(rawLink) | ||
-- | -- Format as [ rawLink displayText ] | ||
return string.format("[%s %s]", rawLink, displayText) | return string.format("[%s %s]", rawLink, displayText) | ||
end | end | ||
| Line 76: | Line 69: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Process wiki links | -- Process wiki links in different modes (extract, strip, check) | ||
-- @param value | -- @param value Wiki link to process | ||
-- @param mode | -- @param mode Processing mode: "extract", "strip", or "check" | ||
-- @return Processed value or boolean | -- @return Processed value or boolean based on mode | ||
function p.processWikiLink(value, mode) | function p.processWikiLink(value, mode) | ||
-- Default mode is "extract" | -- Default mode is "extract" | ||
| Line 89: | Line 82: | ||
end | end | ||
-- | -- Generate cache key | ||
local cacheKey = value .. ":" .. mode | local cacheKey = value .. ":" .. mode | ||
| Line 97: | Line 90: | ||
end | end | ||
-- | -- Verify wiki link format | ||
local isWikiLink = value:match("^%[%[.-%]%]$") ~= nil | local isWikiLink = value:match("^%[%[.-%]%]$") ~= nil | ||
-- | -- Handle check mode | ||
if mode == "check" then | if mode == "check" then | ||
wikiLinkCache[cacheKey] = isWikiLink | wikiLinkCache[cacheKey] = isWikiLink | ||
| Line 106: | Line 99: | ||
end | end | ||
-- | -- Return original if not a wiki link | ||
if not isWikiLink then | if not isWikiLink then | ||
wikiLinkCache[cacheKey] = value | wikiLinkCache[cacheKey] = value | ||
| Line 112: | Line 105: | ||
end | end | ||
-- | -- Parse wiki link components | ||
local pageName, displayText | local pageName, displayText | ||
-- | -- Match [[PageName|DisplayText]] pattern | ||
pageName, displayText = value:match("^%[%[([^%|%]]+)%|([^%]]+)%]%]$") | pageName, displayText = value:match("^%[%[([^%|%]]+)%|([^%]]+)%]%]$") | ||
if not pageName then | if not pageName then | ||
-- | -- Match [[PageName]] pattern | ||
pageName = value:match("^%[%[([^%|%]]+)%]%]$") | pageName = value:match("^%[%[([^%|%]]+)%]%]$") | ||
displayText = pageName -- | 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 | ||
| Line 131: | Line 124: | ||
result = displayText or value | result = displayText or value | ||
else | else | ||
-- | -- Use extract for unknown modes | ||
result = pageName or value | result = pageName or value | ||
end | end | ||
-- | -- Cache result | ||
wikiLinkCache[cacheKey] = result | wikiLinkCache[cacheKey] = result | ||
| Line 141: | Line 134: | ||
end | end | ||
-- Extract page name from wiki link | -- Extract page name from wiki link | ||
-- @param value | -- @param value Wiki link to process | ||
-- @return | -- @return Extracted page name | ||
function p.extractFromWikiLink(value) | function p.extractFromWikiLink(value) | ||
return p.processWikiLink(value, "extract") | return p.processWikiLink(value, "extract") | ||
end | end | ||
-- | -- Add wiki links based on field config | ||
-- @param value | -- @param value Value to process | ||
-- @param field | -- @param field Field configuration | ||
-- @return | -- @return Value with wiki links if needed | ||
function p.applyWikiLinkHandling(value, field) | function p.applyWikiLinkHandling(value, field) | ||
-- Skip | -- Skip invalid values | ||
if not value or type(value) ~= "string" or value == "" then | if not value or type(value) ~= "string" or value == "" then | ||
return value | return value | ||
end | end | ||
-- Add wiki links | -- Add wiki links if autoWikiLink is true | ||
-- | -- Prevents stripping by later processing | ||
if field.autoWikiLink and not value:match("%[%[.-%]%]") then | if field.autoWikiLink and not value:match("%[%[.-%]%]") then | ||
return "[[" .. value .. "]]" | return "[[" .. value .. "]]" | ||
| Line 167: | Line 160: | ||
end | end | ||
-- | -- Preserve wiki links in processed values | ||
-- | -- Keeps original if links were lost during processing | ||
-- @param originalValue Value before processing | |||
-- @param originalValue | -- @param processedValue Value after processing | ||
-- @param processedValue | -- @param preserveWikiLinks Whether to keep wiki links | ||
-- @param preserveWikiLinks Whether to | -- @return Value with links preserved if needed | ||
-- @return | |||
function p.preserveWikiLinks(originalValue, processedValue, preserveWikiLinks) | function p.preserveWikiLinks(originalValue, processedValue, preserveWikiLinks) | ||
-- Skip | -- Skip non-string values | ||
if type(originalValue) ~= "string" or type(processedValue) ~= "string" then | if type(originalValue) ~= "string" or type(processedValue) ~= "string" then | ||
return processedValue | return processedValue | ||
end | end | ||
-- | -- Restore original if links were lost | ||
if preserveWikiLinks and | if preserveWikiLinks and | ||
originalValue:match("%[%[.-%]%]") and | originalValue:match("%[%[.-%]%]") and | ||
Revision as of 01:54, 25 April 2025
Documentation for this module may be created at Module:LinkParser/doc
-- Module:LinkParser
-- Handles external and wiki link processing with caching
local p = {}
--------------------------------------------------------------------------------
-- Caching and Utilities
--------------------------------------------------------------------------------
-- Cache for processed URLs and wiki links (non-persistent)
local stripCache = {}
local wikiLinkCache = {}
--------------------------------------------------------------------------------
-- External Link Processing
--------------------------------------------------------------------------------
-- Strip URL protocol, www prefix, and trailing slash
function p.strip(link)
-- Input validation
if not link or link == "" then
return link
end
-- Check cache first for previously processed URLs
if stripCache[link] then
return stripCache[link]
end
-- Chain operations to minimize string allocations
local result = link:gsub("^%a+://", ""):gsub("^www%.", ""):gsub("/$", "")
-- Cache the result before returning
stripCache[link] = result
return result
end
--------------------------------------------------------------------------------
-- Creates MediaWiki link: [ originalURL strippedDisplayText ]
--------------------------------------------------------------------------------
function p.render(frame)
-- Input validation
if not frame or not frame.getParent then
return ""
end
local parent = frame:getParent()
if not parent or not parent.args then
return ""
end
local args = parent.args
local rawLink = args["link"] or ""
-- Skip empty links
if not rawLink or rawLink == "" then
return ""
end
-- Clean URL for display
local displayText = p.strip(rawLink)
-- Format as [ rawLink displayText ]
return string.format("[%s %s]", rawLink, displayText)
end
--------------------------------------------------------------------------------
-- Wiki Link Processing
--------------------------------------------------------------------------------
-- Process wiki links in different modes (extract, strip, check)
-- @param value Wiki link to process
-- @param mode Processing mode: "extract", "strip", or "check"
-- @return Processed value or boolean based on mode
function p.processWikiLink(value, mode)
-- Default mode is "extract"
mode = mode or "extract"
-- Early return for nil or empty values
if not value or value == "" then
return mode == "check" and false or value
end
-- Generate cache key
local cacheKey = value .. ":" .. mode
-- Check cache first
if wikiLinkCache[cacheKey] ~= nil then
return wikiLinkCache[cacheKey]
end
-- Verify wiki link format
local isWikiLink = value:match("^%[%[.-%]%]$") ~= nil
-- Handle check mode
if mode == "check" then
wikiLinkCache[cacheKey] = isWikiLink
return isWikiLink
end
-- Return original if not a wiki link
if not isWikiLink then
wikiLinkCache[cacheKey] = value
return value
end
-- Parse wiki link components
local pageName, displayText
-- 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
-- Process based on mode
local result
if mode == "extract" then
result = pageName or value
elseif mode == "strip" then
result = displayText or value
else
-- Use extract for unknown modes
result = pageName or value
end
-- Cache result
wikiLinkCache[cacheKey] = result
return result
end
-- Extract page name from wiki link
-- @param value Wiki link to process
-- @return Extracted page name
function p.extractFromWikiLink(value)
return p.processWikiLink(value, "extract")
end
-- Add wiki links based on field config
-- @param value Value to process
-- @param field Field configuration
-- @return Value with wiki links if needed
function p.applyWikiLinkHandling(value, field)
-- Skip invalid values
if not value or type(value) ~= "string" or value == "" then
return value
end
-- Add wiki links if autoWikiLink is true
-- Prevents stripping by later processing
if field.autoWikiLink and not value:match("%[%[.-%]%]") then
return "[[" .. value .. "]]"
end
return value
end
-- Preserve wiki links in processed values
-- Keeps original if links were lost during processing
-- @param originalValue Value before processing
-- @param processedValue Value after processing
-- @param preserveWikiLinks Whether to keep wiki links
-- @return Value with links preserved if needed
function p.preserveWikiLinks(originalValue, processedValue, preserveWikiLinks)
-- Skip non-string values
if type(originalValue) ~= "string" or type(processedValue) ~= "string" then
return processedValue
end
-- Restore original if links were lost
if preserveWikiLinks and
originalValue:match("%[%[.-%]%]") and
not processedValue:match("%[%[.-%]%]") then
return originalValue
end
return processedValue
end
return p