<!DOCTYPE html>
<html>
  <head>
    <style>
      .yearly-calendar {
        width: 100%;
        border-bottom: 1px solid black;
        border-collapse: collapse;
        table-layout: fixed;
        margin-top: 50px;
      }
      .month-name {
        text-align: center;
        background-color: #dddddd;
        padding: 5px;
        vertical-align: middle;
      }
      th {
        border: 1px solid black;
        padding: 5px;
        text-align: center;
        background-color: #dddddd;
      }
      td {
        border-left: 1px solid black;
        border-right: 1px solid black;
        vertical-align: top;
        padding-left: 5px;
        padding-right: 5px;
        width: calc(100% / 6) !important;
      }

      .holiday {
        color: #ff3333; /* For holidays - Red */
      }
      .saturday {
        color: #77aaff; /* For Saturday - Blue */
      }
      .sunday {
        color: #88ee88; /* For Sunday - Green */
      }
      .eom {
        background-color: #dddddd; /* End of Month */
      }
      .logo {
        position: fixed;
        top: 0px;
        right: 10px;
        height: 65px;
      }
      h2:not(:first-of-type) {
        page-break-before: always;
      }
    </style>
  </head>
  <body>
    {% set splits = (0, 6), (6, 12) %}
    {% for start, end in splits %}
      <h2>Yearly Calendar for {{ year.1.1.1.date_obj.year }}</h2>
      {% if logo %}<img class="logo" src="{{ logo }}" alt="Logo">{% endif %}
      <table class="yearly-calendar">
        <tr>
          {% for month in year %}
            {% if start <= (month[1][1].date_obj.month - 1) and
              (month[1][1].date_obj.month - 1 < end) %}
              <th>
                <div class="month-name">{{ month[1][1].date_obj.strftime("%B") }}</div>
              </th>
            {% endif %}
          {% endfor %}
        </tr>
        {% set last_month = namespace(prev=start) %}
        {% for date in range(1, 32) %}
          <tr>
            {% for month in year %}
              {% if start <= (month[1][1].date_obj.month - 1) and
                (month[1][1].date_obj.month - 1 < end|int) %}
                {% for week in month %}
                  {% for day in week %}
                    {% if day.date_obj.day == date and
                      day.date_obj.month == month[1][1].date_obj.month %}
                      {% if last_month.prev < (month[1][1].date_obj.month - 1) %}<td></td>{% endif %}
                      <td class="{% if day.holiday %}holiday{% elif day.is_weekend and day.day == 'Saturday' %}saturday{% elif day.is_weekend and day.day == 'Sunday' %}sunday{% endif %}">
                        {{ day.date_obj.strftime("%d") }}
                        {% if day.holiday %}{{ day.holiday }}{% endif %}
                      </td>
                      {% set last_month.prev = month[1][1].date_obj.month %}
                    {% endif %}
                  {% endfor %}
                {% endfor %}
              {% endif %}
            {% endfor %}
            {% if last_month.prev < end %}<td></td>{% endif %}
          </tr>
        {% endfor %}
      </table>
    {% endfor %}
  </body>
</html>
