From 068a3d2f1db914082a00a944c4332796e37d7a75 Mon Sep 17 00:00:00 2001 From: mviera Date: Wed, 27 Jul 2011 01:35:06 +0200 Subject: [PATCH 1/3] Year and month in URL. Issue #145 --- docs/settings.rst | 4 ++++ pelican/generators.py | 12 ++++++++++++ pelican/settings.py | 1 + pelican/themes/notmyidea/templates/article.html | 2 +- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/settings.rst b/docs/settings.rst index 7eb1b12a..99899058 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -61,6 +61,10 @@ 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. +`PERMALINK_STRUCTURE` (``'/%Y/%m/'``) Allows to render URLs for articles sorted by date, + in case you specify a format as specified in the + example. Also, you can specify any other word + that you want. ================================================ ===================================================== diff --git a/pelican/generators.py b/pelican/generators.py index d91283f3..aafff068 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -7,6 +7,7 @@ from collections import defaultdict import os import math import random +import urlparse from jinja2 import Environment, FileSystemLoader, PrefixLoader, ChoiceLoader from jinja2.exceptions import TemplateNotFound @@ -159,6 +160,17 @@ 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 self.settings.has_key('PERMALINK_STRUCTURE'): + permalink_structure = self.settings.get('PERMALINK_STRUCTURE') + permalink_structure = permalink_structure.lstrip('/') + try: + add_to_url = article.date.strftime(permalink_structure) + except: + pass + + 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..dc87728d 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', + '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' %}

From 2609004719d2b8bfec9a4d614e6e3d92ec21091a Mon Sep 17 00:00:00 2001 From: mviera Date: Fri, 29 Jul 2011 01:11:35 +0200 Subject: [PATCH 2/3] I change PERMALINK_STRUCTURE to ARTICLE_PERMALINK_STRUCTURE and updating the settings.rst documentation. Also I have implemented other options to this setting, such as the category, the date, the author, this kind of things. Finally, I have setted the ARTICLE_PERMALINK_STRUCTURE option as null in pelican.conf.py sample file. --- docs/settings.rst | 18 +++++++++++++++--- pelican/generators.py | 13 +++++++++---- pelican/settings.py | 2 +- samples/pelican.conf.py | 3 +++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 99899058..ba7ebd72 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -61,10 +61,22 @@ 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. -`PERMALINK_STRUCTURE` (``'/%Y/%m/'``) Allows to render URLs for articles sorted by date, +`ARTICLE_PERMALINK_STRUCTURE` (``'/%Y/%m/'``) Allows to render URLs for articles sorted by date, in case you specify a format as specified in the - example. Also, you can specify any other word - that you want. + 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]. + + 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' ================================================ ===================================================== diff --git a/pelican/generators.py b/pelican/generators.py index aafff068..bc2bd574 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -13,6 +13,7 @@ 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 * @@ -161,14 +162,18 @@ class ArticlesGenerator(Generator): article_template = self.get_template('article') for article in chain(self.translations, self.articles): add_to_url = u'' - if self.settings.has_key('PERMALINK_STRUCTURE'): - permalink_structure = self.settings.get('PERMALINK_STRUCTURE') - permalink_structure = permalink_structure.lstrip('/') + if self.settings.has_key('ARTICLE_PERMALINK_STRUCTURE'): + article_permalink_structure = self.settings.get('ARTICLE_PERMALINK_STRUCTURE') + article_permalink_structure = article_permalink_structure.lstrip('/') try: - add_to_url = article.date.strftime(permalink_structure) + add_to_url = article.date.strftime(article_permalink_structure) except: pass + 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, diff --git a/pelican/settings.py b/pelican/settings.py index dc87728d..af6b1bd9 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -42,7 +42,7 @@ _DEFAULT_CONFIG = {'PATH': None, 'DEFAULT_METADATA': (), 'FILES_TO_COPY': (), 'DEFAULT_STATUS': 'published', - 'PERMALINK_STRUCTURE': '' + 'ARTICLE_PERMALINK_STRUCTURE': '/%Y/%m/' } def read_settings(filename): diff --git a/samples/pelican.conf.py b/samples/pelican.conf.py index 2796d561..9b074416 100755 --- a/samples/pelican.conf.py +++ b/samples/pelican.conf.py @@ -9,6 +9,9 @@ PDF_GENERATOR = False REVERSE_CATEGORY_ORDER = True LOCALE = "" DEFAULT_PAGINATION = 2 +# Allows to construct an url like /2011/07/sample-post.html +# See documentation for more info. +ARTICLE_PERMALINK_STRUCTURE = '' FEED_RSS = 'feeds/all.rss.xml' CATEGORY_FEED_RSS = 'feeds/%s.rss.xml' From a13ae91f0bba5b0ce94fb58b39c713f26ccf2a1c Mon Sep 17 00:00:00 2001 From: mviera Date: Fri, 29 Jul 2011 22:08:21 +0200 Subject: [PATCH 3/3] Adding more documentation about ARTICLE_PERMALINK_STRUCTURE in settings. Also, i deleted the try except because strftime never raises an exception. Issue #145 I set the ARTICLE_PERMALINK_STRUCTURE option as null in settings.py and remove it from pelican sample config file. --- docs/settings.rst | 18 +++++++++++++++--- pelican/generators.py | 11 +++++------ pelican/settings.py | 2 +- samples/pelican.conf.py | 3 --- 4 files changed, 21 insertions(+), 13 deletions(-) mode change 100644 => 100755 pelican/generators.py diff --git a/docs/settings.rst b/docs/settings.rst index ba7ebd72..2bdc891f 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -61,13 +61,19 @@ 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` (``'/%Y/%m/'``) 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: +`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. @@ -77,6 +83,12 @@ Setting name (default value) what does it do? * 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 bc2bd574..8d6de666 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -162,14 +162,13 @@ class ArticlesGenerator(Generator): article_template = self.get_template('article') for article in chain(self.translations, self.articles): add_to_url = u'' - if self.settings.has_key('ARTICLE_PERMALINK_STRUCTURE'): - article_permalink_structure = self.settings.get('ARTICLE_PERMALINK_STRUCTURE') + if 'ARTICLE_PERMALINK_STRUCTURE' in self.settings: + article_permalink_structure = self.settings['ARTICLE_PERMALINK_STRUCTURE'] article_permalink_structure = article_permalink_structure.lstrip('/') - try: - add_to_url = article.date.strftime(article_permalink_structure) - except: - pass + # 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) diff --git a/pelican/settings.py b/pelican/settings.py index af6b1bd9..db2984e9 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -42,7 +42,7 @@ _DEFAULT_CONFIG = {'PATH': None, 'DEFAULT_METADATA': (), 'FILES_TO_COPY': (), 'DEFAULT_STATUS': 'published', - 'ARTICLE_PERMALINK_STRUCTURE': '/%Y/%m/' + 'ARTICLE_PERMALINK_STRUCTURE': '' } def read_settings(filename): diff --git a/samples/pelican.conf.py b/samples/pelican.conf.py index 9b074416..2796d561 100755 --- a/samples/pelican.conf.py +++ b/samples/pelican.conf.py @@ -9,9 +9,6 @@ PDF_GENERATOR = False REVERSE_CATEGORY_ORDER = True LOCALE = "" DEFAULT_PAGINATION = 2 -# Allows to construct an url like /2011/07/sample-post.html -# See documentation for more info. -ARTICLE_PERMALINK_STRUCTURE = '' FEED_RSS = 'feeds/all.rss.xml' CATEGORY_FEED_RSS = 'feeds/%s.rss.xml'