UsageEdit

This module implements many parts of the instructions on Template:2nd chance. This module must be substituted. If it is not, the content cannot be edited and will not be fully parsed.

{{subst:#invoke:Sandbox/Danski454|main|EXACT PAGE NAME}}



--implement some tasks in [[Template:2nd chance]]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local displayError = require('Module:Error').error

function p.main( frame )
	local args = getArgs(frame)
	local title = args[1]
	local page = mw.title.new( title, 0 )
	if (not page) or page.namespace ~= 0 or page.isExternal then
		return displayError({'"' .. title .. '" is not a valid article title'})
	elseif not page.exists then--expensive
		return displayError({'"' .. title .. '" does not exist'})
	elseif page.isRedirect then
		return displayError({'"' .. title .. '" is a redirect to "' .. page.redirectTarget.prefixedText .. '", you may be looking for that page instead'})
	end
	local content = page:getContent()
	--remove unwanted content
	--content = string.gsub(content, "\n%s%[%[File:..-%]%]\n", "\n")--files
	--content = string.gsub(content, "{{[iI]nfobox..-}}%s*\n%s*([^|%s])", "%1")--infoboxes
	content = string.gsub(content, "%b[]", function (match)
			if string.find(match, "[[File:", 1, true) == 1 or string.find(match, "[[Image:", 1, true) == 1 then
				return ""
			end
			return match
		end)--files
	content = string.gsub(content, "{{[^{}\n]+%-stub}}", "")--stub templates
	content = string.gsub(content, "%b{}", function (match)
			if string.find(match, "{{[iI]nfobox") == 1 or string.find(match, "{{[mM]ultiple image") == 1 then
				return ""
			end
			return match
		end)--infoboxes and multiple images
	content = string.gsub(content, "%[%[Category%:..-%]%]", "")--categories
	content = string.gsub(content, "<gallery.*<\\gallery>", "")--galleries
	content = string.gsub(content, "==%s*References%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*Notes%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*Endnotes%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*Footnotes%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*Works cited%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*Further reading%s*==.+", "")--references etc section down
	content = string.gsub(content, "==%s*External links%s*==.+", "")--references etc section down
	
	content = string.gsub(content, "\n=([^\n]+)=\n", "\n==%1==\n")--reduce heading levels
	--add header
	content = "== [[" .. title .. "]] ==\n" .. content .. "\n{{tref}}"
	return content
end

return p