Módulo:Frase del día

De Wikiquote, la colección libre de citas y frases célebres.
Ir a la navegación Ir a la búsqueda

La documentación para este módulo puede ser creada en Módulo:Frase del día/doc

local months = {'january','february','march','april','may long','june','july','august','september','october','november','december'}
local weekdays = {'sunday','monday','tuesday','wednesday','thursday','friday','saturday'}
local dayBase = 'Wikiquote:Archivo de la Frase célebre del día/'
local lang = mw.language.getContentLanguage()
local p = {}

-- build a list of translated weekday names
for i,x in pairs(weekdays) do
	weekdays[i] = lang:ucfirst(mw.message.new(x):plain())
end

-- detect the index of a month (1-12) from its localized name
function monthNumber(monthName)
	for i,x in pairs(months) do
		if lang:ucfirst(mw.message.new(x):plain())==lang:ucfirst(monthName) then
			return i
		end
	end
end

-- show a list of "Quotes of the day" for the given month
function p.mes(frame)
	local month = frame.args[1]
	local year = frame.args[2]
	if month==nil or year==nil or month=='' or year=='' then
		local s = mw.title.getCurrentTitle().text
		month, year = string.match(s,'/(%S+)/(%d+)$')
	end
	if month~=nil and year~=nil then
		local mn = monthNumber(month)
		if mn~=nil then
			local output = {}
			local timestamp = year..'-'..mn..'-01'
			-- header for month and year
			table.insert(output,'== '..lang:ucfirst(month)..' de '..year..' ==')
			local weekNum = tonumber(lang:formatDate('W',timestamp))
			local weekDayNum = tonumber(lang:formatDate('w',timestamp))
			-- the weekday of the 1st day of the year is the start of every week of that year
			local weekDayNum1stJanuary = tonumber(lang:formatDate('w',year..'-01-01'))
			if weekDayNum==weekDayNum1stJanuary then
				-- increment week count if 1st day of the month is the same weekday as the 1st of January
				weekNum = weekNum+1
			end
			local totalDays = tonumber(lang:formatDate('t',timestamp))
			for x = 1,totalDays do
				local t = ''
				if x==1 or weekDayNum==weekDayNum1stJanuary then
					t = t..'=== '
					if x==1 and weekDayNum~=weekDayNum1stJanuary then
						-- add leading suspension points if the 1st day of the month is not the start of the week
						t = t..'...'
					end
					t = t..'Semana '..weekNum
					if totalDays-x<6 then
						-- add trailing suspension points if the week does not end at the month's end
						t = t..'...'
					end
					t = t..' ===\n'
				end
				-- day heading
				t = t..';'..weekdays[weekDayNum+1]..' '..x..'\n'
				local tt = string.format('%02d%02d',mn,x)
				-- use pcall to catch errors
				local ok,result = pcall( frame.expandTemplate, frame, { title=tt, args={ ['año'] = year } } )
				if ok then
					-- show the actual quote
					t = t..result
				else
					-- show a link if the template was not found
					t = t..'[['..mw.site.namespaces[10].name..':'..tt..']]'
				end
				if weekDayNum==((weekDayNum1stJanuary+6)%7) then
					-- increment week count at the custom weekend
					weekNum = weekNum+1
				end
				if weekDayNum==6 then
					-- reset weekday count at the regular weekend
					weekDayNum = 0
				else
					-- increment weekday count
					weekDayNum = weekDayNum+1
				end
				table.insert(output,t)
			end
			-- automatic categorization by year and month
			table.insert(output,'\n[['..mw.site.namespaces[14].name..':'..string.sub(dayBase,1,-2)..'|'..string.sub(year,3)..string.format('%02d',mn)..']]')
			return table.concat(output,'\n')
		end
	end
end

return p