Commits

David Chambers  committed de61a64

Replaced `title` with new `title_text` attribute in settings where HTML is
inappropriate.

  • Participants
  • Parent commits 30e2da5

Comments (0)

Files changed (4)

     for post in Index().descendants():
         if (todo or all) and (tag is None or post.has_tag(tag)):
             required = {
-                'title':       lambda: post.title,
+                'title':       lambda: post.title_text,
                 'link':        lambda: post.permalink(),
                 'description': lambda: post.html,
             }
     from cgi import parse_qsl # Python 2.5
 
 import disqus
+import lxml.html
 import markdown
 import pytz
 
 RE = {
     '\r\n?': re.compile(r'\r\n?'),
     'alias=canon': re.compile(r'^(0*(?P<alias>.*?)(?P<separator>=>?))?(?P<canon>.+)$'),
+    'code_tags': re.compile(r'<code>(.*?)</code>'),
     'control_characters': re.compile(r'[\a\b\s]'),
     'excerpt': re.compile(block % r'\|'),
     'excerpt_pipes': re.compile(match % r'\|'),
                 self.add_document(path)
 
         self.subcategories.sort(key=lambda category: category.name)
-        self.pages.sort(key=lambda page: page.title)
+        self.pages.sort(key=lambda page: page.title_text)
 
     def add_document(self, filepath):
         document = Document.get(filepath)
     def descendants(self, include_pages=False):
         posts, pages = self._descendants(include_pages)
         posts.sort(key=lambda post: post.datetime, reverse=True)
-        pages.sort(key=lambda page: page.title)
+        pages.sort(key=lambda page: page.title_text)
         return posts + pages
 
     def archives(self):
             except ValueError:
                 logger.warning('Date and/or time incorrectly formatted')
 
-        self.title = self.meta.get('title', u'')
+        self.title = self.title_text = self.meta.get('title', u'')
         if not self.title:
             match = re.match(RE['heading'], self.html)
             if match:
+                self.html = match.group('html')
                 self.title = match.group('title')
-                self.html = match.group('html')
+                self.title_text = lxml.html.fromstring(
+                        re.sub(RE['code_tags'], r'`\1`', self.title)
+                        ).text_content()
 
         def video_markup(text):
             def sub(match):
         self.assertEqual(mango_extras.month(12), 'December')
 
 
+class TitleTests(unittest.TestCase):
+
+    def test_backticks(self):
+        title = 'Simulating `nonlocal` in Python 2.x'
+        document = create_document(context={'title': title})
+        self.assertEqual(document.title, 'Simulating <code>nonlocal</code> in Python 2.x')
+        self.assertEqual(document.title_text, title)
+
+    def test_html_chars(self):
+        title = '1 > 0 && 1 < 2'
+        document = create_document(context={'title': title})
+        self.assertEqual(document.title, '1 &gt; 0 &amp;&amp; 1 &lt; 2')
+        self.assertEqual(document.title_text, title)
+
+
 class UpdateTests(unittest.TestCase):
 
     def setUp(self):
                         subscriber_email=author_email, url=document.permalink())
                 subscription.save()
             logger.debug('%s is %s subscribed to comments on %s' % (author_name,
-                    'already' if subscriptions else 'now', document.title))
+                    'already' if subscriptions else 'now', document.title_text))
 
         if not thread: break
 
             'spam':     ('api_key', 'post_id', 'thread_id'),
         }
         author = u'%s <%s>' % (author_name, author_email)
-        subject = u'[%s] Comment: "%s"' % (SITE_TITLE, document.title)
+        subject = u'[%s] Comment: "%s"' % (SITE_TITLE, document.title_text)
         context = {
             'comment': message,
             'commenter': author,