Jump to content

Module:Sandbox/MSGJ

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tom.Reding (talk | contribs) at 00:04, 20 September 2024 (fix escapement). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('strict')
local p = {}

p.main = function(frame)
	local banner_name = mw.text.trim(frame.args[1]) -- name of template to look for, e.g. WikiProject Finland
	local special_chars = {'%', '(', ')', '.', '+', '-', '*', '?', '[', '^', '$'}
	for _, char in ipairs(special_chars) do
		banner_name = banner_name:gsub('%'..char, '%%'..char) -- escape each special character
	end
	local WPBSredirects = mw.loadData('Module:WikiProject banner/config').banner_shell.redirects -- load the current set of redirects to Template:WikiProjct banner shell
	local page_content = mw.title.getCurrentTitle():getContent() -- get content of current page
	local content_without_shell
	for capture in mw.ustring.gmatch(page_content, '%b{}') do -- look for possible templates on page
		for _, redirect in ipairs(WPBSredirects) do
			if mw.ustring.find(capture, '^{{%s*' .. redirect .. '%s*[|}].*}}$') then -- found a banner shell
				content_without_shell = mw.ustring.gsub(page_content, capture, '') -- remove banner shell content from page content
				break -- ideally want to break out from both for loops now
			end
		end
	end
	local template_outside_shell
	if content_without_shell then -- banner shell was found
		if mw.ustring.find(content_without_shell, '{{%s*' .. banner_name .. '%s*[|}]') then -- found banner template outside of the shell
			template_outside_shell = true
		else -- banner template must be inside the shell
			template_outside_shell = false
		end
	else -- no banner shell on page
		template_outside_shell = true
	end
	return template_outside_shell
end

return p