Módulo:Calendário

Grupo de Usuários Wiki Movimento Brasil
Ir para navegação Ir para pesquisar

Descrição[editar]

Cria um calendário com links em determinados dias, obtidos da leitura de uma página com datas.

Uso[editar]

{{#invoke:calendário|ler|nome da página}}

ou

{{#invoke:calendário|ler|nome da página|número de semanas}}

Formato das datas[editar]

Atualmente são suportados os seguintes formatos de datas:

  • <dia> de <mês> ... [[link]] ...
  • <dia> e <dia> de <mês> ... [[link]] ...
  • <dia> - <dia> de <mês> ... [[link]] ...
  • <dia> a <dia> de <mês> ... [[link]] ...

m = {}

function m.ler(frame)
  local texto = mw.title.new(frame.args[1]):getContent()
  local nsemanas = frame.args[2] or 4
  local links = {}
  local meses = {['janeiro']='01', ['fevereiro']='02', ['março']='03', ['abril']='04', ['maio']='05', ['junho']='06',
    ['julho']='07', ['agosto']='08', ['setembro']='09', ['outubro']='10', ['novembro']='11', ['dezembro']='12'}
  for d1, d2, mes, link in mw.ustring.gmatch(texto, '(%d%d?)º? ?[-ae]? ?(%d?%d?) de (%a+).-(%[%[[^%]\n]+%]%])') do
    if meses[mes] then
      if not links[meses[mes]] then links[meses[mes]] = {} end
      if #d1 == 2 and d1:sub(1, 1) == '0' then d1 = d1:sub(2) end
      if d2 and #d2 == 2 and d2:sub(1, 1) == '0' then d2 = d2:sub(2) end
      links[meses[mes]][string.sub(' ' .. d1, -2)] = link
      if d2 ~= '' then
	    local dif = d2 - d1
	    if dif > 1 then
	      for n = 1,dif do links[meses[mes]][string.sub(' ' .. (d1 + n), -2)] = link end
	    end
      end
    end
  end
  return m.semanas(tonumber(nsemanas), links)
end

function m.semanas(n, links)
  if not links then links = {} end
  d = os.date'*t'
  hoje = os.time{year=d.year, month=d.month, day=d.day} -- dia de hoje em unix timestamp
  dia = hoje - 86400 * os.date'%w' -- dia do começo da semana em unix timestamp
  meses = {'JAN', 'FEV', 'MAR', 'ABR', 'MAI', 'JUN', 'JUL', 'AGO', 'SET', 'OUT', 'NOV', 'DEZ'}
  tabela = [=[{|class="wikitable" style="min-width:50%"
|-
!style="background-color: #F8FCFF; border-top-color: #F8FCFF; border-left-color: #F8FCFF"| || dom || seg || ter || qua || qui || sex || sáb ]=]
  nmes = 1
  for s = 1,n do -- um loop para cada semana
    if n - s >= 0 and s == nmes then
      mes = os.date('%m', dia)
      linhas = 1
      semana = dia + 604800
      -- verifica por quantas semanas continua sendo o mesmo mês
      while os.date('%m', semana) == mes and linhas + s <= n do
        linhas = linhas + 1
        semana = semana + 604800
      end
      tdargs = (linhas > 1 and 'rowspan=' .. linhas or '') .. (os.date('%m', hoje) ~= mes and ' style="border-top: 1px double black"' or '')
      tabela = tabela .. '\n|-\n!' .. (tdargs ~= '' and tdargs .. '|' or '') .. meses[tonumber(mes)] .. '\n|'
      nmes = nmes + linhas
    else
      tabela = tabela .. '\n|-\n|'
    end
    for d = 1,7 do -- um loop por dia
      if dia == hoje then
        tabela = tabela .. 'style="border:2px solid #E44"|'
      else
        style = ''
        if dia < hoje then
          style = 'color: gray;'
        end
        if tonumber(os.date('%d', dia)) < 8 then
          style = style .. 'border-top: 1px double black;'
        end
        if os.date('%d', dia) == '01' and os.date('%w', dia) ~= '0' then
          style = style .. 'border-left: 1px double black;'
        end
        if style ~= '' then
          tabela = tabela .. 'style="' .. style .. '"|'
        end
      end
      tabela = tabela .. os.date('%e', dia)
      if links[os.date('%m', dia)] then
        link = links[os.date('%m', dia)][os.date('%e', dia)]
        if link then tabela = tabela .. '<br/>' .. link end
      end
      if d < 7 then tabela = tabela .. '||' end
      dia = dia + 86400
    end
  end
  tabela = tabela .. '\n|}'
  return tabela
end

return m