Commit a358609d authored by Marien Fressinaud's avatar Marien Fressinaud

add: Provide more variables in templates

parents 9b59566f db113d6b
......@@ -389,9 +389,15 @@ feed obviously contains only the articles from the serie. You can also access
the serie object from the article template with `ARTICLE_SERIE`. The two most
interesting functions to use on this object are `title()` and `url()`.
Please note for the moment, you cannot access the list of serie's articles in
the `page` template nor from the `ARTICLE_SERIE` object so you'll have to
create the links to the articles manually.
Series are accessible through the `blog` template with the `SERIES` variable. A
serie is just a basic page.
To finish, you can create a template specific to series. For that, you'll have
to set a `template: serie` variable in your `serie.html` files. The series have
access to two additional variables: `ARTICLES` (such in the `blog` template but
they are sorted from the older to the newer) and `FEED` which represent the
Atom feed associated to your serie. You can call `FEED.url()` to create a link
to your feed.
## Template syntax
......
......@@ -38,10 +38,11 @@ def main(environment):
configuration_path = os.path.join(os.curdir, "configuration.yml")
configuration = config.load(configuration_path, environment, output_path)
# STEP 1: we load all the Articles and Pages
# STEP 1: we load all the Articles, Pages and Feeds
pages = []
series = []
articles = []
articles_by_series = {}
feeds = []
# Check that index file exists
index_filepath = os.path.join(os.curdir, "index.html")
......@@ -66,47 +67,62 @@ def main(environment):
# Check that articles directory exists and load Articles from it
articles_path = os.path.join(os.curdir, "articles")
if os.path.isdir(articles_path):
# We iterate on the directories of articles dir. If they contain
# "serie.html" file, it means they contain a serie. Otherwise, we list
# articles as usual.
dirs_with_articles = list(utils.list_dirs(articles_path)) + ["."]
for dirname in dirs_with_articles:
dirpath = os.path.realpath(os.path.join(articles_path, dirname))
serie_filepath = os.path.join(dirpath, "serie.html")
serie = None
# If the directory contains a `serie.html` file, it means it
# contains a serie so we load a page from it.
if dirname != "." and os.path.exists(serie_filepath):
# We have a serie Page! Each article of the directory must be
# considered part of the serie.
slug = f"serie/{dirname}"
serie = utils.build_page_from_filepath(
serie_filepath, configuration, slug=slug
)
pages.append(serie)
articles_by_series[serie] = []
series.append(serie)
# Let's collect all the articles from the directory
serie_articles = []
for filename in utils.list_files(dirpath, only=["md"]):
article_filepath = os.path.join(dirpath, filename)
article = utils.build_article_from_filepath(
article_filepath, configuration, serie
)
articles.append(article)
if serie:
articles_by_series[serie].append(article)
serie_articles.append(article)
# If the directory contains a serie and there is at least one
# article, we load a feed for the serie and we let know the serie
# about its articles and feed.
if serie and serie_articles:
feed_slug = f"feeds/{dirname}"
feed_title = f"{serie.title()} - {configuration['SITE_TITLE']}"
serie_feed = Feed(
feed_slug,
serie_articles,
{"title": feed_title, "site_link": serie.url()},
configuration=configuration,
)
feeds.append(serie_feed)
# We want to sort the articles by publication date first
articles.sort(key=lambda article: article.date(), reverse=True)
for serie_articles in articles_by_series.values():
serie_articles.sort(key=lambda article: article.date(), reverse=True)
serie.set_articles(serie_articles)
serie.set_feed(serie_feed)
# Load a "blog" page which has access to the list of articles
blog_template_filepath = os.path.join("templates", "blog.html")
if os.path.exists(blog_template_filepath):
blog_page = utils.build_blog_page(articles, configuration)
blog_page = utils.build_blog_page(articles, series, configuration)
pages.append(blog_page)
# STEP 2: we write all the pages and articles
for page in pages:
# And load the main feed
if len(articles) > 0:
feed = Feed("feeds/all", articles, configuration=configuration)
feeds.append(feed)
# STEP 2: we write all the pages, articles and feeds
for page in pages + series:
utils.write_content(page, output_path)
print(f"Written page: {page.url()}")
......@@ -114,37 +130,11 @@ def main(environment):
utils.write_content(article, output_path)
print(f"Written article: {article.url()}")
# STEP 3: we write also the Atom feeds (the main one + the series').
# We start by collecting the different feeds
feeds = []
if len(articles) > 0:
# The main one...
feed = Feed("feeds/all", articles, configuration=configuration)
feeds.append(feed)
for serie, serie_articles in articles_by_series.items():
# And the series' ones
if len(serie_articles) == 0:
continue
slug = f"feeds/{os.path.basename(serie.slug())}"
feed_title = f"{serie.title()} - {configuration['SITE_TITLE']}"
feed = Feed(
slug,
serie_articles,
{"title": feed_title, "site_link": serie.url()},
configuration=configuration,
)
feeds.append(feed)
if len(feeds) > 0:
# If we have collected feeds (i.e. we have at least one article), we
# write them.
for feed in feeds:
utils.write_feed(feed, output_path)
print(f"Written feed: {feed.url()}")
for feed in feeds:
utils.write_feed(feed, output_path)
print(f"Written feed: {feed.url()}")
# STEP 4: and we finish by copying the static folder.
# STEP 3: and we finish by copying the static folder.
static_dirpath = os.path.join(os.curdir, "static")
if os.path.isdir(static_dirpath):
distutils.dir_util.copy_tree(static_dirpath, output_path)
......
......@@ -36,6 +36,13 @@ class Page:
def template(self):
return self.meta["PAGE_TEMPLATE"]
def set_articles(self, articles):
articles.sort(key=lambda article: article.date(), reverse=False)
self.meta["ARTICLES"] = articles
def set_feed(self, feed):
self.meta["FEED"] = feed
YAML_FRONT_MATTER_REGEX = re.compile("(---\n.*\n)(---\n)(.*)", re.M | re.S)
......
......@@ -47,13 +47,17 @@ def build_page_from_filepath(filepath, configuration, basename=None, slug=None):
return Page(final_slug, page_file.read(), configuration=configuration)
def build_blog_page(articles, configuration):
def build_blog_page(articles, series, configuration):
articles.sort(key=lambda article: article.date(), reverse=True)
series.sort(key=lambda serie: serie.title(), reverse=False)
blog_page = Page("blog", "", configuration=configuration)
blog_page.meta["PAGE_TEMPLATE"] = "blog"
blog_page.meta["ARTICLES"] = articles
blog_page.meta["ARTICLES_BY_YEAR"] = itertools.groupby(
articles, key=lambda article: article.date().year
)
blog_page.meta["SERIES"] = series
return blog_page
......@@ -90,6 +94,9 @@ def write_feed(feed, output_path):
feed_filepath = os.path.join(output_path, f"{feed.slug()}.atom.xml")
articles = feed.articles()
# We want to sort the articles by publication date
articles.sort(key=lambda article: article.date(), reverse=True)
atom = Atom()
atom.add_header(
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment