Modulo che implementa le funzionalità del template:Bozza.

Legge i vari template:Richiesta revisione bozza presenti nella pagina e include il template:Bozza/avviso coi parametri adeguati.


require('Modulo:No globals')

local p = {}
local getArgs = require('Module:Arguments').getArgs

local function replace(str, pattern, replace)
	local i, j = str:find(pattern, 1, true)
	str = str:sub(0, i - 1) .. replace .. str:sub(j + 1)
	return str
end

local function getSubmissions(stored_submissions, text)
	local new_text = text

	for tmp in text:gmatch('%{(%b{})%}') do
		local old_tmp = tmp
		stored_submissions, tmp = getSubmissions(stored_submissions, tmp)
		new_text = replace(new_text, old_tmp, tmp)

		if tmp:match('^%{ *[Rr]ichiesta *revisione *bozza *[%|%}]') then
			local request = string.format('{%s}', tmp)
			new_text = replace(new_text, request, '')
			tmp = tmp:gsub('%{%b{}%}', '{{')

			if tmp:match('%| *1? *=? *[^%s%|%}]') or tmp:match('%| *esito *= *[^%s%|%}]') then
				table.insert(stored_submissions.old, request)
			else
				table.insert(stored_submissions.current, request)
			end
		end
	end

	return stored_submissions, new_text
end

function p.main(frame)
	local current_title = mw.title.getCurrentTitle()

	if current_title.namespace == 0 then
		return '[[Categoria:Rimuovere template Bozza]]'
	end

	local text = current_title:getContent()

	if not text then return end

	text
		:gsub('<!%-%-.-%-%->', '')
		:gsub('<nowiki>.-</nowiki>', '')
		:gsub('%f[%{]%{%f[^%{]', '\r')
		:gsub('%{%{ *[Cc]ategorie *bozza *%|.-%}%}', '')

	local args = getArgs(frame, { parentOnly = true })
	local lang = mw.language.getContentLanguage()
	local state = lang:ucfirst(args[1] or '')
	local submissions = getSubmissions({ current = {}, old = {} }, text)

	if state ~= 'S' then
		if #submissions.current > 0 then
			state = 'R'
		elseif #submissions.old > 0 and state ~= 'A' then
			state = 'N'
		end
	end

	local ret = frame:expandTemplate{
			title = 'Template:Bozza',
			args = { state, ts = args.ts }
		}

	if current_title.namespace == 118 then
		local catpattern = "[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Aa]?"

		for colonprefix in text:gmatch('%[%[ *(' .. catpattern .. ') *: *[^%s][^\n]-%]%]') do
			local ns = mw.site.namespaces[colonprefix]

			if ns and ns.canonicalName == 'Category' then
				ret = ret .. '[[Categoria:Bozze con categorie da disabilitare]]'
				break
			end
		end
	end

	return ret
end

return p