Fixes some issues with the category handling.

This commit is contained in:
Alexis Metaireau 2010-09-16 01:49:26 +01:00
commit bd007138fa
3 changed files with 20 additions and 11 deletions

View file

@ -23,8 +23,8 @@ _DEFAULT_CONFIG = {'PATH': None,
'OUTPUT_PATH': 'output/', 'OUTPUT_PATH': 'output/',
'MARKUP': 'rst', 'MARKUP': 'rst',
'STATIC_PATHS': ['css', 'images'], 'STATIC_PATHS': ['css', 'images'],
'FEED': 'atom.xml', 'FEED': 'feeds/all.atom.xml',
'CATEGORY_FEED': '%s.xml', 'CATEGORY_FEED': 'feeds/%s.atom.xml',
'BLOGNAME': 'A Pelican Blog', 'BLOGNAME': 'A Pelican Blog',
'BLOGURL': ''} 'BLOGURL': ''}
@ -64,6 +64,12 @@ def generate_output(path=None, theme=None, output_path=None, markup=None,
f = os.path.abspath(f) f = os.path.abspath(f)
content = open(f, encoding='utf-8').read() content = open(f, encoding='utf-8').read()
article = Article(content, markup, context, os.stat(f)) article = Article(content, markup, context, os.stat(f))
if not hasattr(article, 'category'):
# try to get the category from the dirname
category = os.path.dirname(f).replace(os.path.abspath(path)+'/', '')
if category != '':
article.category = unicode(category)
articles.append(article) articles.append(article)
if hasattr(article, 'date'): if hasattr(article, 'date'):
update_dict(dates, article.date.strftime('%Y-%m-%d'), article) update_dict(dates, article.date.strftime('%Y-%m-%d'), article)
@ -91,13 +97,13 @@ def generate_output(path=None, theme=None, output_path=None, markup=None,
generate('tag/%s.html' % tag, templates['tag'], context, tag=tag) generate('tag/%s.html' % tag, templates['tag'], context, tag=tag)
for cat in categories: for cat in categories:
generate('category/%s.html' % cat, templates['category'], context, generate('category/%s.html' % cat, templates['category'], context,
category=cat) category=cat, articles=categories[cat])
for article in articles: for article in articles:
generate('%s' % article.url, generate('%s' % article.url,
templates['article'], context, article=article) templates['article'], context, article=article)
generate_feed(articles, context, output_path, context['FEED']) generate_feed(articles, context, output_path, context['FEED'])
for category, articles in categories.values(): for category, articles in categories.items():
articles.sort(key=attrgetter('date'), reverse=True) articles.sort(key=attrgetter('date'), reverse=True)
generate_feed(articles, context, output_path, generate_feed(articles, context, output_path,
context['CATEGORY_FEED'] % category) context['CATEGORY_FEED'] % category)
@ -136,7 +142,12 @@ def generate_feed(articles, context, output_path=None, filename=None):
pubdate=article.date) pubdate=article.date)
if output_path and filename: if output_path and filename:
fp = open(os.path.join(output_path, context['FEED']), 'w') complete_path = os.path.join(output_path, filename)
try:
os.makedirs(os.path.dirname(complete_path))
except Exception:
pass
fp = open(complete_path, 'w')
feed.write(fp, 'utf-8') feed.write(fp, 'utf-8')
fp.close() fp.close()
return feed return feed
@ -281,6 +292,3 @@ class Article(object):
@property @property
def summary(self): def summary(self):
return self.content return self.content
def __repr__(self):
return '<%s "%s">' % (self.__class__.__name__, self.title)

View file

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
<ul> <ul>
{% for category in categories %} {% for category, articles in categories %}
<li>{{ category }}</li> <li>{{ category }}</li>
{% endfor %} {% endfor %}
</ul> </ul>

View file

@ -1,8 +1,9 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block content %} {% block content %}
Articles in the {{ category }} category.
<ul> <ul>
{% for articles in category %} {% for article in articles %}
<li><a href="{{ article.url }}">{{ article.name }}</a></li> <li><a href="../{{ article.url }}">{{ article.title }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endblock %} {% endblock %}