"Plan to read" books are now ordered by date added
[books.alexwlchan.net] / scripts / render_html.py
index 14bbd53..0cbb13f 100755 (executable)
@@ -10,9 +10,10 @@ import sys
 
 import attr
 import frontmatter
+from jinja2 import Environment, FileSystemLoader, select_autoescape
 import markdown
 from markdown.extensions.smarty import SmartyExtension
-from jinja2 import Environment, FileSystemLoader, select_autoescape
+import smartypants
 
 
 def rsync(dir1, dir2):
@@ -34,9 +35,9 @@ class Book:
 @attr.s
 class Review:
     date_read = attr.ib()
-    rating = attr.ib()
     text = attr.ib()
     format = attr.ib()
+    rating = attr.ib(default=None)
     did_not_finish = attr.ib(default=False)
 
 
@@ -81,9 +82,17 @@ def get_reading_entry_from_path(path):
     return CurrentlyReadingEntry(path=path, book=book, reading=reading)
 
 
+def _parse_date(value):
+    if isinstance(value, datetime.date):
+        return value
+    else:
+        return datetime.datetime.strptime(value, "%Y-%m-%d").date()
+
+
 @attr.s
 class Plan:
     text = attr.ib()
+    date_added = attr.ib(converter=_parse_date)
 
 
 @attr.s
@@ -97,7 +106,7 @@ def get_plan_entry_from_path(path):
     post = frontmatter.load(path)
 
     book = Book(**post["book"])
-    plan = Plan(text=post.content)
+    plan = Plan(**post["plan"], text=post.content)
 
     return PlanEntry(path=path, book=book, plan=plan)
 
@@ -154,7 +163,7 @@ def render_individual_review(env, *, review_entry):
     out_path.write_text(html)
 
 
-if __name__ == "__main__":
+def main():
     env = Environment(
         loader=FileSystemLoader("templates"),
         autoescape=select_autoescape(["html", "xml"]),
@@ -162,6 +171,7 @@ if __name__ == "__main__":
 
     env.filters["render_markdown"] = render_markdown
     env.filters["render_date"] = render_date
+    env.filters["smartypants"] = smartypants.smartypants
 
     rsync("src/covers/", "_html/covers/")
     rsync("static/", "_html/static/")
@@ -214,6 +224,8 @@ if __name__ == "__main__":
         get_entries(dirpath="src/plans", constructor=get_plan_entry_from_path)
     )
 
+    all_plans = sorted(all_plans, key=lambda plan: plan.plan.date_added)
+
     template = env.get_template("list_plans.html")
     html = template.render(all_plans=all_plans, title="books i want to read")
 
@@ -230,3 +242,7 @@ if __name__ == "__main__":
     index_path.write_text(html)
 
     print("✨ Rendered HTML files to _html ✨")
+
+
+if __name__ == "__main__":
+    main()