Commits

David Chambers committed ae75bbb

Added `Tag` class. Template authors should use a tag's `slug` attribute rather
than passing its name through Django's `slugify` filter (if the name contains
a full stop, `tag|slugify` != `tag.slug`).

Comments (0)

Files changed (3)

         MARKDOWN_DATE_FORMAT, MARKDOWN_EXTENSIONS, MARKDOWN_TIME_FORMAT,
         POST_CACHE_SECONDS, PROJECT_PATH, SHORT_URL_BASE, TIME_ZONES)
 from mango.templatetags.mango_extras import _convert, convert_html_chars
-from mango.utils import canonicalize, logger, replace
+from mango.utils import canonicalize, logger, replace, slugify
 
 block = r'(?m)^(%s(?=[ \n])[^\n]*(\n|$))+'
 match = r'(?m)^(%s(?=[ \n]) ?)'
     def _tags(self):
         tags = []
         for document in self.pages + self.posts:
-            tags += [tag for tag in document.meta.get('tags', [])]
+            if document.tags():
+                tags += document.tags(alphabetical=False) # no point in sorting
         for subcategory in self.subcategories:
             tags += subcategory._tags()
         return tags
     def tags(self):
         tags = {}
         for tag in self._tags():
-            tags[tag] = tags.get(tag, 0) + 1
-        keys = sorted(tags.keys(), key=lambda key: key.lower())
-        return [(key, tags[key]) for key in keys]
+            try:
+                tag, count = tags[tag.name]
+            except KeyError:
+                tags[tag.name] = (tag, 1)
+            else:
+                tags[tag.name] = (tag, count + 1)
+        keys = tags.keys()
+        keys.sort(key=lambda key: key.lower())
+        return [tags[key] for key in keys]
 
     def uncache(self):
         for document in self.descendants(include_pages=True):
                 self.meta[key] = value[0].strip()
 
         if 'tags' in mango.settings.META_LISTS and 'tags' in self.meta:
-            self._tags = self.meta['tags']
+            self._tags = [Tag(tag) for tag in self.meta['tags']]
 
         self.author = None
         author_name = self.meta.get('author')
         return master
 
     def tags(self, alphabetical=True):
-        return (sorted(self._tags, key=lambda tag: tag.lower())
+        return (sorted(self._tags, key=lambda tag: tag.name.lower())
                 if alphabetical and self._tags else self._tags)
 
     def uncache(self):
 class Update(Document):
     def __init__(self, parent):
         super(Update, self).__init__(parent=parent)
+
+class Tag(object):
+    def __init__(self, name):
+        self.name = name
+        self.slug = slugify(name)
+
+    def __unicode__(self):
+        return self.name

templates/document.dhtml

                         </ul>{% endif %}{% if document.tags %}
                         <h4>This {{ document.type }} has the following tags:</h4>
                         <ol>{% for tag in document.tags %}
-                            <li><a href="{% url mango.views.tagged_as tag|slugify %}">{{ tag }}</a></li>{% endfor %}
+                            <li><a href="{% url mango.views.tagged_as tag.slug %}">{{ tag }}</a></li>{% endfor %}
                         </ol>{% endif %}
                     </footer>{% endif %}{% endwith %}{% endwith %}
 {% block comments %}{% if comments or new_comment %}

templates/tags.dhtml

 {% block content %}
             <h1>Tags</h1>{% if tags %}
             <dl id="tags">{% for tag, count in tags %}
-                <dt><a href="{% url mango.views.tagged_as tag|slugify %}">{{ tag }}</a></dt>
+                <dt><a href="{% url mango.views.tagged_as tag.slug %}">{{ tag }}</a></dt>
                 <dd>{{ count }}</dd>{% endfor %}
             </dl>{% else %}
             <p>There are no tags to display.</p>{% endif %}