Merge pull request #146 from mviera/master

Year and month in URL. Fixes #145
This commit is contained in:
Alexis Metaireau 2011-07-29 13:34:23 -07:00
commit 89a48ecb1d
4 changed files with 46 additions and 1 deletions

View file

@ -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'.
================================================ =====================================================

16
pelican/generators.py Normal file → Executable file
View file

@ -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)

View file

@ -42,6 +42,7 @@ _DEFAULT_CONFIG = {'PATH': None,
'DEFAULT_METADATA': (),
'FILES_TO_COPY': (),
'DEFAULT_STATUS': 'published',
'ARTICLE_PERMALINK_STRUCTURE': ''
}
def read_settings(filename):

View file

@ -3,7 +3,7 @@
{% block content %}
<section id="content" class="body">
<article>
<header> <h1 class="entry-title"><a href="{{ article.url }}"
<header> <h1 class="entry-title"><a href="{{ pagename }}"
rel="bookmark" title="Permalink to {{ article.title }}">{{ article.title
}}</a></h1> {% include 'twitter.html' %} </header>
<div class="entry-content">