Commits

David Chambers committed 331dce9

Updated `Document#convert` to guarantee that each document's `title` attribute
is a `SafeData` instance, obviating the need to apply Django's `safe` filter.
Utilized the `title_text` attribute in the document template's `title` block.

Comments (0)

Files changed (5)

         MARKDOWN_DATE_FORMAT, MARKDOWN_EXTENSIONS, MARKDOWN_TIME_FORMAT,
         POST_CACHE_SECONDS, PROJECT_PATH, SHORT_URL_BASE, TIME_ZONES)
 from mango.decorators import baseurl
-from mango.templatetags.mango_extras import _convert
+from mango.templatetags.mango_extras import _convert, convert_html_chars
 from mango.utils import canonicalize, logger, replace
 
 block = r'(?m)^(%s(?=[ \n])[^\n]*(\n|$))+'
                 logger.warning('Date and/or time incorrectly formatted')
 
         self.title = self.title_text = self.meta.get('title', u'')
-        if not self.title:
+        if self.title:
+            self.title = convert_html_chars(self.title)
+        else:
             match = re.match(RE['heading'], self.html)
             if match:
                 self.html = match.group('html')
-                self.title = match.group('title')
+                self.title = mark_safe(match.group('title'))
                 self.title_text = lxml.html.fromstring(
                         re.sub(RE['code_tags'], r'`\1`', self.title)
                         ).text_content()

templates/document.dhtml

 {% extends 'base.dhtml' %}
 {% load mango_extras %}
-{% block title %}{{ document.title|strip_tags|safe }}{% endblock %}
+{% block title %}{{ document.title_text }}{% endblock %}
 {% block content %}
 				<article>
 					<header>
-						<h1>{{ document.title|safe }}</h1>{% if document.datetime %}
+						<h1>{{ document.title }}</h1>{% if document.datetime %}
 						<time datetime="{{ document.datetime|isoformat }}" pubdate="pubdate">{{ document.datetime|display_date }}</time>{% endif %}
 					</header>
 					{{ document.html }}{% with document.shorturl as short %}{% with document.permalink as canon %}{% if document.author or document.tags or short != canon %}

templates/excerpt.dhtml

 {% load mango_extras %}
 			<article>{% if document.title or document.datetime %}
 				<header>{% if document.title %}
-					<h2><a href="{{ document.permalink }}">{{ document.title|safe }}</a></h2>{% endif %}{% if document.datetime %}
+					<h2><a href="{{ document.permalink }}">{{ document.title }}</a></h2>{% endif %}{% if document.datetime %}
 					<time datetime="{{ document.datetime|isoformat }}" pubdate="pubdate">{{ document.datetime|display_date }}</time>{% endif %}
 				</header>{% endif %}
 				{{ document.excerpt }}

templatetags/mango_extras.py

     else:
         return comment.author.url
 
-@register.filter
-def h1(title):
-    title = title.strip()
-    return u'%s\n%s' % (title, '=' * len(title))
-
 @stringfilter
 def _convert(text):
     md.reset()
 
 @register.filter
 @stringfilter
-def strip_tags(html):
+def strip_tags(html): # deprecated
     return re.sub(r'<\S+?>', u'', html)
 
 @register.filter
 from mango.templatetags import mango_extras
 from mango.utils import primary_author_email
 
-document_template = Template('''{% load mango_extras %}{% if meta == None %}date: 29 September 2010
+document_template = Template(
+'''{% load mango_extras %}{% autoescape off %}{% if meta == None %}date: 29 September 2010
 time: 1:30am
 tags: Markdown, Django, Mango
 {% else %}{% for k, v in meta %}{{ k }}: {{ v }}
 Introducing Mango
 =================
 {% else %}
-{{ title|h1 }}
+{{ title }}
+===========
 {% endif %}
 {% firstof body 'Mango attempts to make blogging fun again.' %}
+{% endautoescape %}
 ''')
 
 def create_document(filename='test.text', context=None, body=None):
 
 class TemplateFilterTests(unittest.TestCase):
 
-    def test_h1(self):
-        self.assertEqual(mango_extras.h1(' Mango '), 'Mango\n=====')
-
     def test_month_zero(self):
         self.assertEqual(mango_extras.month(0), '')
 
         self.assertEqual(document.title, '1 &gt; 0 &amp;&amp; 1 &lt; 2')
         self.assertEqual(document.title_text, title)
 
+    def test_meta_title(self):
+        title = 'Hello, world!'
+        document = create_document(context={'meta': [('title', title)]})
+        self.assertEqual(document.title, title)
+        self.assertEqual(document.title_text, title)
+
+    def test_meta_title_with_backticks(self):
+        title = 'Simulating `nonlocal` in Python 2.x'
+        document = create_document(context={'meta': [('title', title)]})
+        self.assertEqual(document.title, title)
+        self.assertEqual(document.title_text, title)
+
+    def test_meta_title_with_html_chars(self):
+        title = '1 > 0 && 1 < 2'
+        document = create_document(context={'meta': [('title', title)]})
+        self.assertEqual(document.title, '1 &gt; 0 &amp;&amp; 1 &lt; 2')
+        self.assertEqual(document.title_text, title)
+
 
 class UpdateTests(unittest.TestCase):