Module:Sandbox/Aidan9382/Link once: Difference between revisions

Content deleted Content added
m quick regex fix
Dont process category links
 
(4 intermediate revisions by the same user not shown)
Line 2:
local yesno = require("Module:Yesno")
 
-- Behaviour for these functions determined via [[Help:Pipe trick]]
local function replicatePipeTrick(target)
local newfunction wlPipeTrick(target)
target = target:gsub("^[a-zA-Z0-9 _-]-:(.*)$", "%1") --Remove the namespace
if newtarget:find("%(.+%)$") then --If ending parenthesis
newtarget = newtarget:gsub("^(.-) *%(.+%)$", "%1") --Remove ending parenthesis
else
newtarget = newtarget:gsub("^(.-), .*$", "%1") --Else, remove ending comma
end
return newtarget
end
 
local function getWikilinkInfowlReversePipeTrick(wikilink) -- Returns the wikilink's target and everything else)
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
 
local function getWikilinkInfo(wikilink)
--[=[
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)
Line 20 ⟶ 35:
local displayText = string.sub(trimmed, firstPipe+1)
if target == "" then -- [[|XYZ]]
return trim(wlReversePipeTrick(displayText)), nil --TODO: Reverse pipe tricktrim(displayText)
elseif displayText == "" then -- [[XYZ|]]
return trim(target), trim(replicatePipeTrickwlPipeTrick(target))
else --[[ABC|XYZ]]
return trim(target), trim(displayText)
end
else
returnlocal out = trim(trimmed), nil
if out:find("^/.-/+$") and mw.title.getCurrentTitle().namespace ~= 0 then -- [[/Test/]]
return out, out:gsub("^/(.-)/+$", "%1")
else -- [[Test]]
return out, nil
end
end
end
Line 38 ⟶ 58:
%[%[.-%]%] wouldn't work for the exact same testcase for other reasons
--]=]
local options = options or {follow_redirects=true}
local newText = {}
local scannerPosition = 1
Line 64 ⟶ 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
Line 108 ⟶ 128:
local args = require('Module:Arguments').getArgs(frame)
return linkOnce(args[1] or "", {
follow_redirects = yesno(args.follow_redirects) or falsetrue,
})
end
Line 117 ⟶ 137:
linkOnce = linkOnce,
-- Helper functions
wlPipeTrick = wlPipeTrick,
replicatePipeTrick = replicatePipeTrick,
wlReversePipeTrick = wlReversePipeTrick,
getWikilinkInfo = getWikilinkInfo
}