Skip to content
Snippets Groups Projects
Commit 18aabe0f authored by Josh Borrow's avatar Josh Borrow
Browse files

Sorted our items by date

parent fbef9f76
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
"""
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)
......@@ -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 %}
......@@ -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 %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment