Modulo:Qotd

Da Wikiquote, aforismi e citazioni in libertà.

Modulo Lua per svolgere varie funzioni per la gestione delle citazioni del giorno di Wikiquote:


local p = {}
local lang = mw.language.getContentLanguage()
local months = {'january','february','march','april','may long','june','july','august','september','october','november','december'}
local monthBase = 'Wikiquote:Archivio delle citazioni del giorno/'
local dayBase = 'Template:Qotd/'

function p.archivio(frame)
	local output = {}
	for i,e in pairs(months) do
		table.insert( output, '[['..monthBase..mw.message.new(e):plain()..'|'..frame:preprocess('{{ucfirst:{{int:'..e..'}}}}')..']]' )
	end
	return table.concat( output, ' · ' )
end

function p.testo(frame)
	local args = frame:getParent().args
	local testo = (args.capolettera or args[1] or '')..(args.citazione or args[2] or '')
	local t = string.gsub(testo,'%[%[.-%|.-%]%]','')
	local find = string.find(t,'%|')
	if find~=nil then
		return '\'\''..testo..'\'\''
	end
	return testo
end

function p.citazione(frame)
	local args = frame:getParent().args
	local pre = ''
	local cpl = (args.capolettera or args[1] or '')
	local cit = (args.citazione or args[2] or '')
	if cpl=='' and cit~='' then
		pre = mw.ustring.match(cit,'^(%[%[.-%|)')
		if pre~=nil then
			cit = mw.ustring.sub(cit,mw.ustring.len(pre) + 1)
		else
			pre = ''
		end
		local s1, s2 = mw.ustring.match(cit,'^([^%w ]*%w)(.+)$')
		if s1~=nil and s2~=nil then
			cpl = s1
			cit = s2
		else
			cpl = mw.ustring.sub(cit,0,1)
			cit = mw.ustring.sub(cit,2)
		end
	end
	local res = '<span style="font-size:'..(args.grandezzacapolettera or '130%')..';'
	if args.stilecapolettera~=nil and args.stilecapolettera~='' then
		res = res..'font-family:'..args.stilecapolettera..';'
	end
	res = pre..res..'">'..cpl..'</span>'..cit
	return res
end

function p.attribuzione(frame)
	local args = frame:getParent().args
	local grassetto = frame.args.grassetto or '\'\'\''
	local post = args.post or args.b or ', '
	if post~='' and string.sub(post,-1)~=' ' then
		post = post..' '
	end
	local pre = args.pre or args.a
	if pre==nil then
		pre = ''
	elseif pre=='a' then
		pre = 'attribuita a '
	elseif pre=='da' then
		pre = 'tratta da '
	elseif pre~='' and string.sub(pre,-1)~=' ' then
		pre = pre..' '
	end
--[[
QUANDO CAMBI LE PAROLE O LE DEFINIZIONI DEL PARAMETRO "PRE" CAMBIA ANCHE LE ISTRUZIONI DEL CODICE IN Template:Cdg/man#Elenco diciture per "pre"
]]--
	local autore = args.autore or args[4]
	local opera = args.opera or args[5]
	local attrib = args.attribuita
	if attrib~=nil and attrib~='' then
		attrib = 'attribuita a '..grassetto..'[['..attrib..']]'..grassetto
	else
		attrib = ''
	end
	if opera~=nil and opera~='' then
		opera = '\'\'[['..opera..']]\'\''
	else
		opera = ''
	end
	local res = ''
	if autore~=nil and autore~='' then
		res = grassetto..'[['..autore..']]'..grassetto
		if attrib~='' then
			res = res..post..attrib
		elseif opera~='' then
			res = res..post..opera
		elseif post~='' and post~=', ' then
			res = res..post
		end
	else
		if attrib~='' then
			res = attrib
		elseif opera~='' then
			res = opera
		end
	end
	return mw.text.trim(pre..res)
end

function day(date)
	local page = mw.title.new(dayBase..date)
	if page.exists==false then
		return ''
	end
	local c = page:getContent()
	return string.gsub(c,'(%{%{[Cc]dg)([%|%}])','%1/alt%2') 
end

function month(monthName)
	local output = {}
	for x = 1,31 do
		local d = day(x..monthName)
		local ord = 'º'
		if d~='' then
			if x~=1 then ord='' end
			table.insert(output,'*\'\'\'[['..dayBase..x..monthName..'|'..x..ord..' '..monthName..']]:\'\'\' '..d)
		else
			break
		end
	end
	return output
end

function p.giorno(frame)
	return frame:preprocess(day(frame.args[1] or lang:formatDate('jF')))
end

function p.mese(frame)
	local monthName
	if frame.args[1]~=nil then
		monthName = frame.args[1]
	else
		monthName = mw.title.getCurrentTitle().subpageText
	end
	local output = month(monthName)
	if (#output) == 0 then
		output = month(lang:formatDate('F'))
	end
	return frame:preprocess(table.concat(output,'\n'))
end

return p