-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from shazz/other
Add files via upload
- Loading branch information
Showing
5 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .series import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This plugin extends the original series plugin | ||
by FELD Boris <[email protected]> | ||
Copyright (c) Leonardo Giordani <[email protected]> | ||
Joins articles in a series and provides variables to | ||
manage the series in the template. | ||
""" | ||
|
||
from collections import defaultdict | ||
from operator import itemgetter | ||
|
||
from pelican import signals | ||
|
||
|
||
def aggregate_series(generator): | ||
series = defaultdict(list) | ||
|
||
# This cycles through all articles in the given generator | ||
# and collects the 'series' metadata, if present. | ||
# The 'series_index' metadata is also stored, if specified | ||
for article in generator.articles: | ||
if 'series' in article.metadata: | ||
article_entry = ( | ||
article.metadata.get('series_index', None), | ||
article.metadata['date'], | ||
article | ||
) | ||
|
||
series[article.metadata['series']].append(article_entry) | ||
|
||
# This uses items() which on Python2 is not a generator | ||
# but we are dealing with a small amount of data so | ||
# there shouldn't be performance issues =) | ||
for series_name, series_articles in series.items(): | ||
# This is not DRY but very simple to understand | ||
forced_order_articles = [ | ||
art_tup for art_tup in series_articles if art_tup[0] is not None] | ||
|
||
date_order_articles = [ | ||
art_tup for art_tup in series_articles if art_tup[0] is None] | ||
|
||
forced_order_articles.sort(key=itemgetter(0)) | ||
date_order_articles.sort(key=itemgetter(1)) | ||
|
||
all_articles = forced_order_articles + date_order_articles | ||
ordered_articles = [art_tup[2] for art_tup in all_articles] | ||
enumerated_articles = enumerate(ordered_articles) | ||
|
||
for index, article in enumerated_articles: | ||
article.series = dict() | ||
article.series['name'] = series_name | ||
article.series['index'] = index + 1 | ||
article.series['all'] = ordered_articles | ||
article.series['all_previous'] = ordered_articles[0: index] | ||
article.series['all_next'] = ordered_articles[index + 1:] | ||
|
||
if index > 0: | ||
article.series['previous'] = ordered_articles[index - 1] | ||
else: | ||
article.series['previous'] = None | ||
|
||
try: | ||
article.series['next'] = ordered_articles[index + 1] | ||
except IndexError: | ||
article.series['next'] = None | ||
|
||
|
||
def register(): | ||
signals.article_generator_finalized.connect(aggregate_series) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters