require("strict")local yesno = require("Module:Yesno")
-- Behaviour for these functions determined via 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 targetend
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 targetend
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) local firstPipe = string.find(trimmed, "|") if firstPipe then local target = string.sub(trimmed, 1, firstPipe-1) local displayText = string.sub(trimmed, firstPipe+1) if target
"" then -- return trim(target), trim(wlPipeTrick(target)) else --XYZ return trim(target), trim(displayText) end else local out = trim(trimmed) if out:find("^/.-/+$") and mw.title.getCurrentTitle.namespace ~= 0 then -- /Test/ return out, out:gsub("^/(.-)/+$", "%1") else -- Test return out, nil end endend
local function linkOnce(text, options) -- Module entry point --[=[ We are going to traverse the text linearly ourselves. Using %b[] isn't preferable as nested brackets (E.g. the wikilink to t in) would be missed and doing a check for %[%[.-%]%] wouldn't work for the exact same testcase for other reasons --]=] local options = options or local newText = local scannerPosition = 1 local existingWikilinks = local openWikilinks = while true do local Position, _, Character = string.find(text, "([%[%]])%1", scannerPosition) local container = (openWikilinks[#openWikilinks] or).Text if not Position then --Done container[#container+1] = string.sub(text, scannerPosition) break end container[#container+1] = string.sub(text, scannerPosition, Position-1)
scannerPosition = Position+2 --+2 to pass the / if Character