Create a Tag class which has a url property

This commit is contained in:
Kyle Fuller 2011-12-22 15:43:44 +00:00
commit f9ed01bb64
4 changed files with 24 additions and 4 deletions

View file

@ -163,6 +163,26 @@ class Category(object):
def url(self):
return 'category/%s.html' % self
class Tag(object):
def __init__(self, tag):
self.tag = unicode.strip(tag)
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
def url(self):
return 'tag/%s.html' % self
def is_valid_content(content, f):
try:
content.check_properties()

View file

@ -179,7 +179,7 @@ class ArticlesGenerator(Generator):
for tag, articles in self.tags.items():
articles.sort(key=attrgetter('date'), reverse=True)
dates = [article for article in self.dates if article in articles]
write('tag/%s.html' % tag, tag_template, self.context, tag=tag,
write(tag.url, tag_template, self.context, tag=tag,
articles=articles, dates=dates,
paginated={'articles': articles, 'dates': dates},
page_name='tag/%s' % tag)

View file

@ -15,12 +15,12 @@ except ImportError:
Markdown = False
import re
from pelican.contents import Category
from pelican.contents import Category, Tag
from pelican.utils import get_date, open
_METADATA_PROCESSORS = {
'tags': lambda x: map(unicode.strip, unicode(x).split(',')),
'tags': lambda x: map(Tag, unicode(x).split(',')),
'date': lambda x: get_date(x),
'status': unicode.strip,
'category': Category,

View file

@ -1,2 +1,2 @@
{% if article.tags %}<p>tags: {% for tag in article.tags %}<a href="{{ SITEURL }}/tag/{{ tag }}.html">{{ tag }}</a>{% endfor %}</p>{% endif %}
{% if article.tags %}<p>tags: {% for tag in article.tags %}<a href="{{ SITEURL }}/{{ tag.url }}">{{ tag }}</a>{% endfor %}</p>{% endif %}
{% if PDF_PROCESSOR %}<p><a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">get the pdf</a></p>{% endif %}