Content deleted Content added
dont empty string that |
Dont process category links |
||
(14 intermediate revisions by the same user not shown) | |||
Line 1:
require("strict")
local yesno = require("Module:Yesno")
-- Behaviour for these functions determined via [[Help:Pipe trick]]
local function getWikilinkInfo(wikilink) -- Returns the wikilink's target and everything else▼
local function wlPipeTrick(target)
target = target:gsub("^[a-zA-Z0-9 _-]-:(.*)$", "%1") --Remove the namespace
if target:find("%(.+%)$") then --If ending parenthesis
target = target:gsub("^(.-) *%(.+%)$", "%1") --Remove ending parenthesis
else
target = target:gsub("^(.-), .*$", "%1") --Else, remove ending comma
end
return target
end
local function wlReversePipeTrick(target)
local current = mw.title.getCurrentTitle().prefixedText
if current:find("%(.+%)$") then --If ending parenthesis
target = target .. current:gsub("^.-( *%(.+%))$", "%1") --Append ending parenthesis
else
target = target .. current:gsub("^.-(, .*)$", "%1") --Else, append ending comma
end
return target
end
--[=[
Returns the wikilink's target and its display text.
Automatically recreates the effect of any [[Help:pipe tricks|]]
--]=]
local trim = mw.text.trim
local trimmed = string.sub(wikilink, 3, -3)
local firstPipe = string.find(trimmed, "|")
if firstPipe then
local displayText = string.sub(trimmed, firstPipe+1) if target == "" then -- [[|XYZ]]
return trim(wlReversePipeTrick(displayText)), trim(displayText)
elseif displayText == "" then -- [[XYZ|]]
return trim(target), trim(wlPipeTrick(target))
else --[[ABC|XYZ]]
return trim(target), trim(displayText)
else
local out = trim(trimmed)
return trimmed, nil▼
if out:find("^/.-/+$") and mw.title.getCurrentTitle().namespace ~= 0 then -- [[/Test/]]
return out, out:gsub("^/(.-)/+$", "%1")
else -- [[Test]]
end
end
Line 13 ⟶ 53:
local function linkOnce(text, options) -- Module entry point
--[=[
We are going to traverse the text
Using %b[] isn't preferable as nested brackets (E.g. the wikilink to t
in [[File:x|Cap[[t]]ion]]) would be missed and doing a check for
%[%[.-%]%] wouldn't work for the exact same testcase for other reasons
--]=]
local options = options or {follow_redirects=true}
local newText = {}
local scannerPosition = 1
Line 44 ⟶ 84:
local newContainer = (openWikilinks[#openWikilinks] or {Text=newText}).Text
if wlTarget:find("^[Ii]mage:") or wlTarget:find("^[Ff]ile:") or wlTarget:find("^[Cc]ategory:") then --Files/Images/Categories aren't processed (they aren't really wikilinks)
newContainer[#newContainer+1] = wlText
else
local realTarget = wlTarget:sub(1, 1):upper() .. wlTarget:sub(2)
if options.follow_redirects then▼
local titleObj = mw.title.new(wlTarget)▼
if not titleObj then --junk▼
realTarget = wlTarget:sub(1, 1):upper() .. wlTarget:sub(2)▼
else▼
local newTarget = titleObj.isRedirect and titleObj.redirectTarget.fullText or titleObj.fullText▼
▲ end
else▼
▲ end
if existingWikilinks[realTarget] then
newContainer[#newContainer+1] = wlPiped or wlTarget
else
local resolvedTarget = realTarget
existingWikilinks[realTarget] = true▼
▲ if options.follow_redirects then
newContainer[#newContainer+1] = wlText▼
▲ local titleObj = mw.title.new(wlTarget)
▲ local newTarget = titleObj.isRedirect and titleObj.redirectTarget.fullText or titleObj.fullText
end
if existingWikilinks[resolvedTarget] then
newContainer[#newContainer+1] = wlPiped or wlTarget
▲ else
▲ existingWikilinks[realTarget] = true
existingWikilinks[resolvedTarget] = true
▲ newContainer[#newContainer+1] = wlText
end
end
end
Line 73 ⟶ 114:
end
end
if #openWikilinks > 0 then --Random [[ with no matching pair, dont process it
Line 86 ⟶ 126:
local function main(frame) -- Template entry point
local args =
return linkOnce(args[1] or "", {
follow_redirects = yesno(args.follow_redirects) or
})
end
return {
-- Main entry points
main = main,
linkOnce = linkOnce,
-- Helper functions
wlPipeTrick = wlPipeTrick,
wlReversePipeTrick = wlReversePipeTrick,
getWikilinkInfo = getWikilinkInfo
}
|