Create a Author class which has a url property

This commit is contained in:
Kyle Fuller 2011-12-22 16:22:34 +00:00
commit ff9c786149
6 changed files with 28 additions and 30 deletions

View file

@ -37,9 +37,9 @@ class Page(object):
# default author to the one in settings if not defined # default author to the one in settings if not defined
if not hasattr(self, 'author'): if not hasattr(self, 'author'):
if 'AUTHOR' in settings: if 'AUTHOR' in settings:
self.author = settings['AUTHOR'] self.author = Author(settings['AUTHOR'])
else: else:
self.author = getenv('USER', 'John Doe') self.author = Author(getenv('USER', 'John Doe'))
warning(u"Author of `{0}' unknow, assuming that his name is `{1}'".format(filename or self.title, self.author)) warning(u"Author of `{0}' unknow, assuming that his name is `{1}'".format(filename or self.title, self.author))
# manage languages # manage languages
@ -142,47 +142,44 @@ class Article(Page):
class Quote(Page): class Quote(Page):
base_properties = ('author', 'date') base_properties = ('author', 'date')
class URLWrapper(object):
class Category(object): def __init__(self, name):
def __init__(self, category): self.name = unicode(name)
self.category = unicode(category)
def __hash__(self): def __hash__(self):
return hash(self.category) return hash(self.name)
def __eq__(self, other): def __eq__(self, other):
return self.category == unicode(other) return self.name == unicode(other)
def __str__(self): def __str__(self):
return str(self.category) return str(self.name)
def __unicode__(self): def __unicode__(self):
return self.category return self.name
@property
def url(self):
return '%s.html' % self.name
class Category(URLWrapper):
@property @property
def url(self): def url(self):
return 'category/%s.html' % self return 'category/%s.html' % self
class Tag(object): class Tag(URLWrapper):
def __init__(self, tag): def __init__(self, name):
self.tag = unicode.strip(tag) self.name = unicode.strip(name)
def __hash__(self):
return hash(self.tag)
def __eq__(self, other):
return self.tag == unicode(tag)
def __str__(self):
return str(self.tag)
def __unicode__(self):
return self.tag
@property @property
def url(self): def url(self):
return 'tag/%s.html' % self return 'tag/%s.html' % self
class Author(URLWrapper):
@property
def url(self):
return 'author/%s.html' % self
def is_valid_content(content, f): def is_valid_content(content, f):
try: try:
content.check_properties() content.check_properties()

View file

@ -195,7 +195,7 @@ class ArticlesGenerator(Generator):
author_template = self.get_template('author') author_template = self.get_template('author')
for aut, articles in self.authors: for aut, articles in self.authors:
dates = [article for article in self.dates if article in articles] dates = [article for article in self.dates if article in articles]
write('author/%s.html' % aut, author_template, self.context, write(aut.url, author_template, self.context,
author=aut, articles=articles, dates=dates, author=aut, articles=articles, dates=dates,
paginated={'articles': articles, 'dates': dates}, paginated={'articles': articles, 'dates': dates},
page_name='author/%s' % aut) page_name='author/%s' % aut)

View file

@ -15,7 +15,7 @@ except ImportError:
Markdown = False Markdown = False
import re import re
from pelican.contents import Category, Tag from pelican.contents import Category, Tag, Author
from pelican.utils import get_date, open from pelican.utils import get_date, open
@ -24,6 +24,7 @@ _METADATA_PROCESSORS = {
'date': lambda x: get_date(x), 'date': lambda x: get_date(x),
'status': unicode.strip, 'status': unicode.strip,
'category': Category, 'category': Category,
'author': Author,
} }
def _process_metadata(name, value): def _process_metadata(name, value):

View file

@ -5,7 +5,7 @@
{% if article.author %} {% if article.author %}
<address class="vcard author"> <address class="vcard author">
By <a class="url fn" href="{{ SITEURL }}/author/{{ article.author }}.html">{{ article.author }}</a> By <a class="url fn" href="{{ SITEURL }}/{{ article.author.url }}">{{ article.author }}</a>
</address> </address>
{% endif %} {% endif %}
<p>In <a href="{{ SITEURL }}/{{ article.category.url }}">{{ article.category }}</a>. {% if PDF_PROCESSOR %}<a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">get the pdf</a>{% endif %}</p> <p>In <a href="{{ SITEURL }}/{{ article.category.url }}">{{ article.category }}</a>. {% if PDF_PROCESSOR %}<a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">get the pdf</a>{% endif %}</p>

View file

@ -8,7 +8,7 @@
</abbr> </abbr>
{% if article.author %} {% if article.author %}
<address class="vcard author"> <address class="vcard author">
By <a class="url fn" href="#">{{ article.author }}</a> By <a class="url fn" href="{{ SITEURL }}/{{ article.author.url }}">{{ article.author }}</a>
</address> </address>
{% endif %} {% endif %}
</footer><!-- /.post-info --> </footer><!-- /.post-info -->

View file

@ -11,7 +11,7 @@
<header> <h2 class="entry-title"><a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title}}">{{ article.title }}</a></h2> </header> <header> <h2 class="entry-title"><a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title}}">{{ article.title }}</a></h2> </header>
<footer class="post-info"> <footer class="post-info">
<abbr class="published" title="{{ article.date.isoformat() }}"> {{ article.locale_date }} </abbr> <abbr class="published" title="{{ article.date.isoformat() }}"> {{ article.locale_date }} </abbr>
{% if article.author %}<address class="vcard author">By <a class="url fn" href="{{ SITEURL }}/author/{{ article.author }}.html">{{ article.author }}</a></address>{% endif %} {% if article.author %}<address class="vcard author">By <a class="url fn" href="{{ SITEURL }}/{{ article.author.url }}">{{ article.author }}</a></address>{% endif %}
</footer><!-- /.post-info --> </footer><!-- /.post-info -->
<div class="entry-content"> {{ article.summary }} </div><!-- /.entry-content --> <div class="entry-content"> {{ article.summary }} </div><!-- /.entry-content -->
</article></li> </article></li>