Module

WorldLink

From Dogcraft Wiki

Documentation for this module may be created at Module:WorldLink/doc

-- EXPERIMENTAL
-- takes all detected world names in a string and wraps them in [[]] to make them link

local getArgs = require('Module:Arguments').getArgs
local p = {}
local worldTools = require('Module:World')
local lang = mw.language.getContentLanguage()


function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end


function p._main(args)
	input = args[1]
	detectedWorlds = worldTools.extractAllWorlds(input)

	for key,value in ipairs(detectedWorlds) do
		-- this pattern works for most cases
		-- - works: survival 4, [[survival 4]] (matches and world names, both if on their own and if already linked, handles it as it should)
		-- - doesn'T work: [[Grand Central Station (Survival 4)]] produces [[Grand Central Station ([[Survival 4]])]] - if the search term is inside a bigger link already,
		-- it will double link it
		-- - this is also case sensitive, but that can probably be solved
		pattern = "[%[]*" .. value .. "[%]]*"
	    input = mw.ustring.gsub(input, pattern, "[[" .. lang:ucfirst(value) .. "]]")
	end
	
	return input
end

return p