Module:Sandbox/Aidan9382/Link once

This is an old revision of this page, as edited by Aidan9382 (talk | contribs) at 17:02, 4 July 2023 (dont empty string that). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require("strict")

local function getWikilinkInfo(wikilink) -- Returns the wikilink's target and everything else
	local trimmed = string.sub(wikilink, 3, -3)
	local firstPipe = string.find(trimmed, "|")
	if firstPipe then
		return string.sub(trimmed, 1, firstPipe-1), string.sub(trimmed, firstPipe+1)
	else
		return trimmed, nil
	end
end

local function linkOnce(text, options) -- Module entry point
	--[=[
		We are going to traverse the text sequentially.
		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 {}
	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=newText}).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 == "[" then --Add a [[ to the pending wikilink queue
			openWikilinks[#openWikilinks+1] = {Position = Position, Text = {"[["}}

		else --Pair up the ]] to any available [[
			if #openWikilinks >= 1 then
				local openingPair = table.remove(openWikilinks) --Pop the latest [[
				local wlStart, wlText = openingPair.Position, table.concat(openingPair.Text, "") .. "]]"
				local wikilink = string.sub(text, wlStart, Position+1)
				local wlTarget, wlPiped = getWikilinkInfo(wikilink)

				local newContainer = (openWikilinks[#openWikilinks] or {Text=newText}).Text
				if wlTarget:find("^[Ii]mage:") or wlTarget:find("^[Ff]ile:") then --Files/Images aren't processed (they aren't really wikilinks)
					newContainer[#newContainer+1] = wlText
				else
					local realTarget
					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
							realTarget = newTarget:sub(1, 1):upper() .. newTarget:sub(2)
						end
					else
						realTarget = wlTarget:sub(1, 1):upper() .. wlTarget:sub(2)
					end

					if existingWikilinks[realTarget] then
						newContainer[#newContainer+1] = wlPiped or wlTarget
					else
						existingWikilinks[realTarget] = true
						newContainer[#newContainer+1] = wlText
					end
				end

			else --Just a random ]] with no matching pair, dont process it
				newText[#newText+1] = "]]"
			end
		end
	end
	mw.logObject(openWikilinks)

	if #openWikilinks > 0 then --Random [[ with no matching pair, dont process it
		for i = #openWikilinks, 2, -1 do
			local nextLink = openWikilinks[i-1]
			nextLink.Text[#nextLink.Text+1] = table.concat(openWikilinks[i].Text, "")
		end
		newText[#newText+1] = table.concat(openWikilinks[1].Text, "")
	end
	return table.concat(newText, "")
end

local function main(frame) -- Template entry point
	local args = frame.args
	return linkOnce(args[1], {
		follow_redirects = args.follow_redirects or false,
		-- Something else idk
	})
end

return {
	main = main,
	linkOnce = linkOnce
}