{% extends 'base.html' %}

{% set meta = book %}

{% macro poem_html(toc_obj, level) %}
<li class="level{{ level }} {% if toc_obj.children %}parent{% endif %}"
    {% if not toc_obj.children %}data-public-domain="{% if poems[book.slug][toc_obj.slug].text %}true{% else %}false{% endif %}"{% endif %}>
    {% if toc_obj.numeral %}
    <span class="numbered-name">
      <span class="numeral">{{ toc_obj.name }}</span>
      <span class="name">
        {% if poems[book.slug][toc_obj.slug].text -%}<a href="/book/{{ book.slug }}/poem/{{ toc_obj.slug }}/">{% endif %}
          {% if poems[book.slug][toc_obj.slug].titled %}
           {% if poems[book.slug][toc_obj.slug].text -%}<a href="/book/{{ book.slug }}/poem/{{ toc_obj.slug }}/">{% endif %}
            {{ poems[book.slug][toc_obj.slug].title }}
          {% else %}
          {% for line in poems[book.slug][toc_obj.slug].first_line|parsePoem|first %}
            <span class="line">{{ line.text|formatFirstLine }}</span>{% if not loop.last %}<br>{% endif %}
          {% endfor %}
          {% endif %}
        {% if poems[book.slug][toc_obj.slug].text -%}</a>{% endif %}
      </span>
    </span>
    {% else %}
       {% if poems[book.slug][toc_obj.slug].text -%}<a href="/book/{{ book.slug }}/poem/{{ toc_obj.slug }}/">{% endif %}
        {{ toc_obj.name }}
      {% if poems[book.slug][toc_obj.slug].text -%}</a>{% endif %}
    {% endif %}
</li>
{% endmacro %}

{% block content %}
{% include "includes/nav.html" %}
<article data-slug="{{ book.slug }}" data-public-domain="{{ book.public_domain }}">
<header class="book">
  <h1>{{ book.title }}</h1>
</header>
<section class="intro">
  {% for graf in book.description %}
  <p>
    {{ graf }}
  </p>
  {% endfor %}
  {% if book.public_domain == true and (availablePoems|length) > 0 and ((availablePoems|length) < (allPoems.length)) %}
  <p>
    Thus far, {{ availablePoems|length|apnumber }} of the book’s {{ allPoems|length|apnumber }} poems have been digitized.
    <a href="https://github.com/ee-cummings-archive/cummings.ee/blob/master/CONTRIBUTING.md">Here’s how</a> you can get involved in adding more.
  </p>
  {% endif %}
</section>
{% if book.public_domain == true and (availablePoems|length) > 0 %}
<section class="toc">
  <h2>Table of contents</h2>
  <ol class="level1">
  {% for level1 in toc[book.slug] %}
    {{ poem_html(level1, '1') }}
    {% if level1.children|length > 0 %}
    <li>
    <ol class="level2">
    {% for level2 in level1.children %}
      {{ poem_html(level2, '2') }}
      {% if level2.children %}
        <li>
          <ol class="level3">
          {% for level3 in level2.children -%}
            {{ poem_html(level3, '3') }}
          {% endfor %}
          </ol>
        </li>
      {% endif %}
    {% endfor %}
    </ol>
    </li>
    {% endif %}
  {% endfor %}
  </ol>
</section>
{% endif %}
</article>
{% endblock content %}

{% block footer -%}
<footer class="footer">
  <div class="break footer"></div>
  {% if book.source %}
    <p>{{ book.source }}</p>
  {% endif %}
   <p>The table of contents is also available in <a href="{{ book.json_url }}">json</a> format.</p>
  <p>{{ footer.copy }}</p>
</footer>
{% endblock footer -%}
