Modulo:Bozza: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
passo arg e arg2
riorganizzo codice
Riga 3:
local p = {}
local getArgs = require('Module:Arguments').getArgs
 
local function checkForCategories(str)
local catpattern = "[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Aa]?"
 
for colonprefix in str:gmatch('%[%[ *(' .. catpattern .. ') *: *[^%s][^\n]-%]%]') do
local ns = mw.site.namespaces[colonprefix]
 
if ns and ns.canonicalName == 'Category' then
return '[[Categoria:Bozze con categorie da disabilitare]]'
end
end
end
 
local function createRow(text1, text2)
return mw.html.create('div')
:addClass('bozza-history-row')
:tag('div')
:wikitext(text1)
:done()
:tag('div')
:wikitext(text2)
:done()
:done()
end
 
local function pairsByTimestamp(t)
local a = {}
local i = 0
local comp = function (ts1, ts2)
return ts1 > ts2
end
 
for ts in pairs(t) do
table.insert(a, ts)
end
 
table.sort(a, comp)
 
local iter = function ()
i = i + 1
if not a[i] then
return
else
return a[i], t[a[i]]
end
end
 
return iter
end
 
local function removeFalsePositives(str)
return str
:gsub('<!%-%-.-%-%->', '')
:gsub('<nowiki>.-</nowiki>', '')
:gsub('%f[%{]%{%f[^%{]', '\r')
:gsub('%{%{ *[Cc]ategorie *bozza *%|.-%}%}', '')
end
 
local function replace(str, pattern, replace)
Line 14 ⟶ 71:
 
for tmp in text:gmatch('%{(%b{})%}') do
local old_tmppattern = tmp
stored_submissions, tmp = getSubmissions(stored_submissions, tmp)
new_text = replace(new_text, old_tmppattern, tmp)
 
if tmp:find('^%{ *[Rr]ichiesta *revisione *bozza *[%|%}]') then
Line 22 ⟶ 79:
new_text = replace(new_text, submission, '')
tmp = tmp:gsub('%{%b{}%}', '{{')
submission = submission:gsub('\r', '{')
 
local ts = tmp:match('%| *ts *= *(%d%d%d%d%d%d%d%d%d%d%d%d%d%d) *[%|%}]')
Line 35 ⟶ 93:
 
return stored_submissions, new_text
end
 
local function pairsByTimestamp(t)
local a = {}
local i = 0
local function comp(ts1, ts2)
return ts1 > ts2
end
 
for ts in pairs(t) do
table.insert(a, ts)
end
 
table.sort(a, comp)
 
return function ()
i = i + 1
if not a[i] then
return
else
return a[i], t[a[i]]
end
end
end
 
function p.main(frame)
local current_title = mw.title.getCurrentTitle()
local text = current_title:getContent()
 
if current_title.namespace == 0 then
return '[[Categoria:Rimuovere template Bozza]]'
elseif not text then
return
else
text = removeFalsePositives(text)
end
 
local text = current_title:getContent()
 
if not text then return end
 
text = text
:gsub('<!%-%-.-%-%->', '')
:gsub('<nowiki>.-</nowiki>', '')
:gsub('%f[%{]%{%f[^%{]', '\r')
:gsub('%{%{ *[Cc]ategorie *bozza *%|.-%}%}', '')
 
local args = getArgs(frame, { parentOnly = true })
Line 93 ⟶ 123:
 
if next(submissions.old) then
local i = 0
local styles = frame:extensionTag{
name = 'templatestyles',
args = { src = 'Modulo:Bozza/styles.css' }
}
local header = createRow('Data richiesta', 'Esito revisione')
:addClass('nomobile bozza-history-header')
local collapsed_submissions = mw.html.create('div')
:addClass('nomobile mw-collapsible mw-collapsed')
:attr('data-expandtext', 'mostra richieste precedenti')
:attr('data-collapsetext', 'nascondi richieste precedenti')
 
history_table = mw.html.create('div')
:addClass('bozza-history')
:tagnode('div'header)
:addClass('nomobile bozza-history-row bozza-history-header')
:tag('div')
:wikitext('Data richiesta')
:done()
:tag('div')
:wikitext('Esito revisione')
:done()
:done()
 
local i = 0
local root = history_table
 
for ts, a in pairsByTimestamp(submissions.old) do
Line 120 ⟶ 146:
 
if i == 2 then
history_table:node(collapsed_submissions)
root = mw.html.create('div')
:addClass('nomobile mw-collapsible mw-collapsed')
:attr('data-expandtext', 'mostra richieste precedenti')
:attr('data-collapsetext', 'nascondi richieste precedenti')
 
history_table:node(root)
end
 
for _, submission in ipairs(a) do
submission = submission:gsub('%}%}$', '|mostra esito=x}}')
local root = i > 1 and collapsed_submissions or history_table
 
local row = createRow(jFY, frame:preprocess(submission))
root
root:tagnode('div'row)
:addClass('bozza-history-row')
:tag('div')
:wikitext(jFY)
:done()
:tag('div')
:wikitext(frame:preprocess(submission))
:done()
:done()
end
end
Line 148 ⟶ 161:
end
 
return frame:expandTemplate{ title = 'Template:Bozza/avviso', args = {
local ret = frame:expandTemplate{
state,
title = 'Template:Bozza/avviso',
arg = args.arg or args.arg1,
args = {
arg2 = args.arg2,
state,
argts = args.arg or args.arg1ts,
['cronologia revisioni'] = history_table
arg2 = args.arg2,
} } .. (current_title.namespace == 118 and checkForCategories(text) or '')
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