From 18aabe0f80a747816e0a80632d7506665d06a9f5 Mon Sep 17 00:00:00 2001
From: Josh Borrow <joshua.borrow@durham.ac.uk>
Date: Mon, 25 Sep 2017 20:32:33 +0100
Subject: [PATCH] Sorted our items by date

---
 compiler.py          |  4 ++++
 filters.py           | 33 +++++++++++++++++++++++++++++++++
 templates/pubs.html  |  4 ++--
 templates/talks.html |  4 ++--
 4 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 filters.py

diff --git a/compiler.py b/compiler.py
index ffe7cec..ad5d3e5 100644
--- a/compiler.py
+++ b/compiler.py
@@ -10,6 +10,7 @@ from distutils.dir_util import copy_tree
 import jinja2
 import yaml
 from mdcomp import convert_text
+import filters
 
 
 def get_from_filesystem(template_names, template_dir=".", data_dir="../data"):
@@ -20,6 +21,9 @@ def get_from_filesystem(template_names, template_dir=".", data_dir="../data"):
     template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
     template_env = jinja2.Environment(loader=template_loader)
 
+    # We need to register our custom filters.
+    template_env.filters["sort_cards"] = filters.sort_cards
+
     templates = list(map(template_env.get_template, template_names))
     data = get_associated_data(template_names, data_dir)
 
diff --git a/filters.py b/filters.py
new file mode 100644
index 0000000..2088f84
--- /dev/null
+++ b/filters.py
@@ -0,0 +1,33 @@
+"""
+Some simple macros that are used to e.g. sort things within the jinja
+templates. For the relevant documentation please see:
+
+http://jinja.pocoo.org/docs/2.9/api/#custom-filters
+
+Created 26-09-2017 by Josh Borrow (joshua.borrow@durham.ac.uk)
+"""
+
+import dateutil.parser as dp
+
+def sort_cards(cards):
+    """
+    Sort the cards by date. 'cards' should have the following structure:
+    [
+       {
+         ...
+         date: "date"
+       },
+    ]
+    
+    A list will be returned where the cards are sorted by date.
+    """
+
+    dates = [
+        dp.parse(card["date"]) for card in cards
+    ]
+
+    _, cards = zip(*sorted(zip(dates, cards)))
+
+    # We want the newest to appear first.
+    return reversed(cards)
+
diff --git a/templates/pubs.html b/templates/pubs.html
index fe53e91..6b0c637 100644
--- a/templates/pubs.html
+++ b/templates/pubs.html
@@ -9,7 +9,7 @@
     <div class="container">
         {{ helper.navbar(navbar, link) }}
         <div class="cards">
-            {% for card in cards %}
+            {% for card in cards|sort_cards %}
                 <div class="card">
                     <a class="card-content" href="{{ card.link }}">
                         {% if card.img %}
@@ -29,4 +29,4 @@
             {% endfor %}
         </div>
     </div>
-{% endblock %}
\ No newline at end of file
+{% endblock %}
diff --git a/templates/talks.html b/templates/talks.html
index 8d968c4..f23ca7c 100644
--- a/templates/talks.html
+++ b/templates/talks.html
@@ -6,7 +6,7 @@
     <div class="container">
         {{ helper.navbar(navbar, link) }}
         <div class="cards">
-            {% for card in cards %}
+            {% for card in cards|sort_cards %}
                 <div class="card">
                     <div class="card-content">
                         <div>
@@ -40,4 +40,4 @@
     <div class="bottom-padder">
         {# we need this to prevent overlapping #}
     </div>
-{% endblock %}
\ No newline at end of file
+{% endblock %}
-- 
GitLab