Modulo:Bozza

Versione del 18 mag 2021 alle 16:11 di Sakretsu (discussione | contributi) (mostra richieste precedenti)

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('%| *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 history_table

	if #submissions.old > 0 then
		history_table = mw.html.create('table')
			:cssText('border-collapse: collapse; font-size: 95%; width: 100%')
			:tag('tr')
				:tag('th')
					:cssText('padding-right: 2em; text-align: right')
					:wikitext('Richieste precedenti')
					:done()
				:done()

		for _, submission in ipairs(submissions.old) do
			submission = submission:gsub('%}%}$', '|mostra esito=x}}')
			history_table
				:tag('tr')
					:cssText('border-top: thin solid #D8D8D8')
					:wikitext(frame:preprocess(submission))
		end

		history_table = tostring(history_table)
	end

	local ret = frame:expandTemplate{
			title = 'Template:Bozza/avviso',
			args = { state, ts = args.ts, ['cronologia revisioni'] = history_table }
		}

	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