Group books on the reviews page by year read
authorAlex Chan <alex@alexwlchan.net>
Sun, 2 Feb 2020 22:36:13 +0000 (22:36 +0000)
committerAlex Chan <alex@alexwlchan.net>
Sun, 2 Feb 2020 22:36:13 +0000 (22:36 +0000)
scripts/add_book.py
scripts/render_html.py
static/style.css
templates/_footer.html
templates/list_reviews.html
templates/review.html

index c31d723..134709b 100755 (executable)
@@ -86,7 +86,9 @@ def get_review_info():
             choices=["★★★★★", "★★★★☆", "★★★☆☆", "★★☆☆☆", "★☆☆☆☆"],
         ),
         inquirer.Text("format", message="What format did you read it in?"),
-        inquirer.List("did_you_finish", message="Did you finish the book?", choices=["yes", "no"])
+        inquirer.List(
+            "did_you_finish", message="Did you finish the book?", choices=["yes", "no"]
+        ),
     ]
 
     answers = inquirer.prompt(other_questions)
@@ -94,7 +96,12 @@ def get_review_info():
     rating = int(answers["rating"].count("★"))
     assert 1 <= rating <= 5
 
-    return {"date_read": date_read, "rating": rating, "format": answers["format"], "did_not_finish": answers["did_you_finish"] == "no"}
+    return {
+        "date_read": date_read,
+        "rating": rating,
+        "format": answers["format"],
+        "did_not_finish": answers["did_you_finish"] == "no",
+    }
 
 
 def save_cover(slug, cover_image_url):
index 4e626ec..d1d05b1 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
 import datetime
+import itertools
 import os
 import pathlib
 import re
@@ -183,7 +184,16 @@ if __name__ == "__main__":
         render_individual_review(env, review_entry=review_entry)
 
     template = env.get_template("list_reviews.html")
-    html = template.render(all_reviews=all_reviews, title="books i’ve read")
+    html = template.render(
+        all_reviews=[
+            (year, list(reviews))
+            for (year, reviews) in itertools.groupby(
+                all_reviews, key=lambda rev: str(rev.review.date_read)[:4]
+            )
+        ],
+        title="books i’ve read",
+        this_year=str(datetime.datetime.now().year),
+    )
 
     out_path = pathlib.Path("_html") / "reviews/index.html"
     out_path.write_text(html)
index 8db2748..f38b4fa 100644 (file)
@@ -182,3 +182,11 @@ footer a:hover {
 .note p:last-child {
   margin-bottom: 0.75em;
 }
+
+.divider {
+  color: #bbb;
+  text-align: center;
+  font-size: 2em;
+  margin-top:    1em;
+  margin-bottom: 1em;
+}
index ac113e2..f3e78c1 100644 (file)
@@ -1,6 +1,6 @@
 <footer>
   <div id="footer_inner">
-    made with 💜 by <a href="https://twitter.com/alexwlchan">lexie</a>, who thinks you look great today
+    made with &#x2764; by <a href="https://twitter.com/alexwlchan">lexie</a>, who thinks you look great today
     •
     code on <a href="https://snekks.club/alexwlchan/books.alexwlchan.net">snekks.club</a>
   </div>
index 1ad3b11..219db19 100644 (file)
@@ -9,38 +9,63 @@
 
   <h2>books i&rsquo;ve read</h2>
 
-  {% for review_entry in all_reviews %}
-  <div class="review_preview">
-    <a href="/{{ review_entry.out_path() }}">
-      <div class="book_thumbnail">
-        {% set book = review_entry.book %}
-        {% include "_book_cover.html" %}
-      </div>
-
-      <div class="book_metadata">
-        <p class="title">{{ review_entry.book.title }}</p>
-        <p>
-          <small>
-            by {{ review_entry.book.author }}
-            {%- if review_entry.book.publication_year %}
-              ({{ review_entry.book.publication_year}})
-            {%- endif -%}
-              <br/>
-            read {{ review_entry.review.date_read | render_date }}
-              <br/>
-            {% for _ in range(review_entry.review.rating) %}
-            ★
-            {% endfor %}
-            {% for _ in range(5 - review_entry.review.rating) %}
-            ☆
-            {% endfor %}
-            {% if review_entry.review.did_not_finish %}
-            (did not finish)
-            {% endif %}
-          </small>
-        </p>
-      </div>
-    </a>
+  {% for year, reviews in all_reviews %}
+  <div class="books_by_year">
+    <h3>the {{ reviews|length }} book{% if reviews|length != 1 %}s{% endif %}
+      {% if year == this_year %}
+        i&rsquo;ve read so far
+      {% else %}
+        i read
+      {% endif %}
+
+      {% if year %}
+        in {{ year }}
+      {% else %}
+        at another time
+      {% endif %}
+    </h3>
+
+    {% for review_entry in reviews %}
+    <div class="review_preview">
+      <a href="/{{ review_entry.out_path() }}">
+        <div class="book_thumbnail">
+          {% set book = review_entry.book %}
+          {% include "_book_cover.html" %}
+        </div>
+
+        <div class="book_metadata">
+          <p class="title">{{ review_entry.book.title }}</p>
+          <p>
+            <small>
+              by {{ review_entry.book.author }}
+              {%- if review_entry.book.publication_year %}
+                ({{ review_entry.book.publication_year}})
+              {%- endif -%}
+                <br/>
+              {% if review_entry.review.date_read %}
+              read {{ review_entry.review.date_read | render_date }}
+                <br/>
+              {% endif %}
+              {% for _ in range(review_entry.review.rating) %}
+              ★
+              {% endfor %}
+              {% for _ in range(5 - review_entry.review.rating) %}
+              ☆
+              {% endfor %}
+              {% if review_entry.review.did_not_finish %}
+              (did not finish)
+              {% endif %}
+            </small>
+          </p>
+        </div>
+      </a>
+    </div>
+    {% endfor %}
   </div>
+
+  {% if not loop.last %}
+  <div class="divider">{{ loop.cycle('&#x2766;', '&#x2767;') | safe }}</div>
+  {% endif %}
+
   {% endfor %}
 {% endblock %}
\ No newline at end of file
index c196752..88f910b 100644 (file)
         {%- endif -%}
       </th>
     </tr>
+    {% if review_entry.review.date_read %}
     <tr>
       <td>date read:</td>
       <th>{{ review_entry.review.date_read | render_date }}</th>
     </tr>
+    {% endif %}
     <tr>
       <td>rating:</td>
       <th>