Módulo:Diario de viaje

De Wikiviajes, la guía libre de viajes

La documentación para este módulo puede ser creada en Módulo:Diario de viaje/doc

local p = {}
local lang = mw.language.getContentLanguage()
local listPage = mw.title.new('Diario de viaje/Lista',4)

function list()
	local t = {}
	local pattern = '%-%-%-%-.-%-%-%-%-'
	for item in string.gmatch('----\n'..string.gsub(listPage:getContent(),'(\n%-%-%-%-)\n','%1%1\n')..'\n----',pattern) do
		local m1,m2,m3 = string.match(item,'^%-%-%-%-\n(.-)\n(.-)\n(.+)%-%-%-%-$')
		if m1~=nil and m2~=nil and m3~=nil then
			table.insert(t,{m1,m2,m3})
		else
			local n1,n2 = string.match(item,'^%-%-%-%-\n(.-)\n(.-)\n%-%-%-%-$')
			if n1~=nil and n2~=nil then
				table.insert(t,{n1,'',n2})
			end
		end
	end
	return t
end

function p.diario(frame)
	local args = frame:getParent().args
	local iconsize = (args.iconsize or '30px')
	local showIcons = true
	local useGroups = false
	for _,val in pairs(args) do
		if val=='noicons' then
			showIcons = false
		elseif val=='grouped' then
			useGroups = true
		end
	end
	local sectionTitles = {
		misc= 'Generiche',
		stats='Statistiche',
		talk= 'Discussioni',
		tech= 'Notizie tecniche'
	}
	local defaultIcons = {
		stats='QtiPlot logo.png',
		talk= 'Speech balloon.svg',
		tech= 'Crystal Clear action run.svg'
	}
	local l = list()
	local o = {}
	local latest = nil
	for i,x in pairs(l) do
		local icon = mw.text.trim(x[2])
		local sect = nil
		if icon~='' then
			local par1,par2 = string.match(icon,'^(.-)%|((.-)%.(.+))$')
			if par1~=nil and par1~='' and par2~=nil and par2~='' then
				sect = par1
				icon = par2
			end
			if (par1==nil or par1=='' or par2==nil or par2=='') and string.find(icon,'%.')==nil then
				sect = icon
				icon = ''
			end
		end
		if sect==nil then
			sect = 'misc'
		end
		if showIcons==true then
			if icon=='' and defaultIcons[sect]~=nil then
				icon = defaultIcons[sect]
			end
			if icon=='' then
				icon = '<span style="display:inline-block;height:'..iconsize..';width:'..iconsize..'">&nbsp;</span> '
			else
				icon = '[[File:'..icon..'|'..iconsize..']] '
			end
		else
			icon = ''
		end
		local dt = tonumber(lang:formatDate('U',mw.text.trim(x[1])))
		if latest==nil or dt>latest then
			latest = dt
		end
		local txt = icon..'\'\'\''..lang:formatDate('d xg Y',mw.text.trim(x[1]))..'\'\'\': '..frame:preprocess(mw.text.trim(x[3]))
		if sectionTitles[sect]~=nil then
			sect = sectionTitles[sect]
		end
		if o[sect]~=nil then
			table.insert(o[sect],txt)
		else
			o[sect] = {txt}
		end
	end
	local res = {}
	for k,x in pairs(o) do
		table.insert(res,'==='..k..'===\n'..table.concat(x,'\n\n'))
	end
	return #l..' notizie (la più recente risale a '..lang:formatDuration(os.difftime(os.time(),latest),{'years','months','weeks','days'})..' fa)\n\n['..listPage:fullUrl{action = 'edit'}..' aggiungine una]\n\n'..table.concat(res,'\n\n')
end

return p