diff --git a/docs/settings.rst b/docs/settings.rst index 7eb1b12a..2bdc891f 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -61,6 +61,34 @@ Setting name (default value) what does it do? on the output path "static". By default, pelican will copy the 'images' folder to the output folder. +`ARTICLE_PERMALINK_STRUCTURE` (``''``) Empty by default. Allows to render URLs for + articles sorted by date, in case you specify a + format as specified in the example. + It follows the python datetime directives: + * %Y: Year with century as a decimal number. + * %m: Month as a decimal number [01,12]. + * %d: Day of the month as a decimal number [01,31]. + + Note: if you specify a datetime directive, it will + be substituted using the date metadata field into + the rest file. if the date is not specified, pelican + will rely on the mtime of your file. + + Check the python datetime documentation + at http://bit.ly/cNcJUC for more information. + + Also, you can use any metadata in the + restructured text files: + * category: '%(category)s' + * author: '%(author)s' + * tags: '%(tags)s' + * date: '%(date)s' + + Example usage: + * '/%Y/%m/' it will be something like + '/2011/07/sample-post.html'. + * '/%Y/%(category)s/' it will be something like + '/2011/life/sample-post.html'. ================================================ ===================================================== diff --git a/pelican/generators.py b/pelican/generators.py old mode 100644 new mode 100755 index d91283f3..8d6de666 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -7,11 +7,13 @@ from collections import defaultdict import os import math import random +import urlparse from jinja2 import Environment, FileSystemLoader, PrefixLoader, ChoiceLoader from jinja2.exceptions import TemplateNotFound from pelican.utils import copy, get_relative_path, process_translations, open +from pelican.utils import slugify from pelican.contents import Article, Page, is_valid_content from pelican.readers import read_file from pelican.log import * @@ -159,6 +161,20 @@ class ArticlesGenerator(Generator): # in writer, articles pass first article_template = self.get_template('article') for article in chain(self.translations, self.articles): + add_to_url = u'' + if 'ARTICLE_PERMALINK_STRUCTURE' in self.settings: + article_permalink_structure = self.settings['ARTICLE_PERMALINK_STRUCTURE'] + article_permalink_structure = article_permalink_structure.lstrip('/') + + # try to substitute any python datetime directive + add_to_url = article.date.strftime(article_permalink_structure) + # try to substitute any article metadata in rest file + add_to_url = add_to_url % article.__dict__ + add_to_url = [slugify(i) for i in add_to_url.split('/')] + add_to_url = os.path.join(*add_to_url) + + article.url = urlparse.urljoin(add_to_url, article.url) + article.save_as = urlparse.urljoin(add_to_url, article.save_as) write(article.save_as, article_template, self.context, article=article, category=article.category) diff --git a/pelican/settings.py b/pelican/settings.py index 125d0bb0..db2984e9 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -42,6 +42,7 @@ _DEFAULT_CONFIG = {'PATH': None, 'DEFAULT_METADATA': (), 'FILES_TO_COPY': (), 'DEFAULT_STATUS': 'published', + 'ARTICLE_PERMALINK_STRUCTURE': '' } def read_settings(filename): diff --git a/pelican/themes/notmyidea/templates/article.html b/pelican/themes/notmyidea/templates/article.html index 14fffe4d..6615b63a 100644 --- a/pelican/themes/notmyidea/templates/article.html +++ b/pelican/themes/notmyidea/templates/article.html @@ -3,7 +3,7 @@ {% block content %}
-

{{ article.title }}

{% include 'twitter.html' %}