Add some nice social sharing previews
authorAlex Chan <alex@alexwlchan.net>
Sat, 18 Apr 2020 21:27:34 +0000 (22:27 +0100)
committerAlex Chan <alex@alexwlchan.net>
Sat, 18 Apr 2020 21:27:34 +0000 (22:27 +0100)
scripts/render_html.py
templates/base.html
templates/review.html

index ac0e5ad..7853abf 100755 (executable)
@@ -187,6 +187,24 @@ def thumbnail_1x(name):
     return pth.stem + "_1x" + pth.suffix
 
 
+def _create_new_square(src_path, square_path):
+    square_path.parent.mkdir(exist_ok=True, parents=True)
+
+    im = Image.open(src_path)
+    im.thumbnail((240, 240))
+
+    dimension = max(im.size)
+
+    new = Image.new("RGB", size=(dimension, dimension), color=(255, 255, 255))
+
+    if im.height > im.width:
+        new.paste(im, box=((dimension - im.width) // 2, 0))
+    else:
+        new.paste(im, box=(0, (dimension - im.height) // 2))
+
+    new.save(square_path)
+
+
 def create_thumbnails():
     for image_name in os.listdir("src/covers"):
         if image_name == ".DS_Store":
@@ -200,6 +218,13 @@ def create_thumbnails():
         elif src_path.stat().st_mtime > dst_path.stat().st_mtime:
             _create_new_thumbnail(src_path, dst_path)
 
+        square_path = pathlib.Path("_html/squares") / image_name
+
+        if not square_path.exists():
+            _create_new_square(src_path, square_path)
+        elif src_path.stat().st_mtime > square_path.stat().st_mtime:
+            _create_new_square(src_path, square_path)
+
 
 def main():
     env = Environment(
index 07b2586..b10e1eb 100644 (file)
@@ -6,6 +6,8 @@
 
     <link rel="stylesheet" href="/static/style.css">
 
+    {% block head_content %}{% endblock %}
+
     <title>{% if title %}{{ title | smartypants | safe }} {% endif %}📚 lexie&rsquo;s book tracker</title>
   </head>
   <body>
index 91d9f1f..80749be 100644 (file)
@@ -1,5 +1,17 @@
 {% extends "base.html" %}
 
+{% block head_content %}
+<meta name="twitter:card" content="summary">
+<meta name="twitter:site" content="@alexwlchan"/>
+<meta name="twitter:title" content="alexwlchan's review of {{ review_entry.book.title }}"/>
+<meta name="twitter:image" content="https://books.alexwlchan.net/squares/{{ review_entry.book.cover_image }}"/>
+
+<meta property="og:type" content="article" />
+<meta property="og:url" content="https://books.alexwlchan.net/{{ review_entry.out_path() }}">
+<meta property="og:title" content="alexwlchan's review of {{ review_entry.book.title }}">
+<meta property="og:image" content="https://books.alexwlchan.net/squares/{{ review_entry.book.cover_image }}"/>
+{% endblock %}
+
 {% block content %}
   {% if review_entry.book.cover_image %}
     <div class="book-cover">