Commits

David Chambers  committed a3ac9e7

Made it possible to (once again) include hand-crafted excerpts.

  • Participants
  • Parent commits d766195
  • Branches 0.8

Comments (0)

Files changed (2)

         return [StyleSheet(path) for path in self.required_files()
                 if path.endswith('.css')]
 
+    _subhandcraftedexcerpt = re.compile(
+        r'''^
+        (?:                                 # optional metadata
+          [ ]{,3}[-\w]+:.*\n                # "key: value" pair
+          (?:[ ]{4,}.*\n)*                  # value may wrap
+        )*
+        (?P<excerpt>                        # arbitrary excerpt
+          (?:
+            (?![ ]{,3}(?:\*[ ]?){3,}\s*\n)  # section delimiters not allowed
+            [^\n]*\n
+          )+?
+        )
+        (?= \# | .+\n={3,} )                # followed by an h1 of either type
+        ''', re.VERBOSE).sub
+
+    def _pluckhandcraftedexcerpt(self, references):
+        def repl(match):
+            excerpt = match.group('excerpt')
+            self._excerpt = convert(excerpt, references) + '\n'
+            return ''
+        return self._subhandcraftedexcerpt(repl, self.source)
+
     def _plucktitle():
         subcodetags = re.compile(r'<code>(.*?)</code>').sub
         matchheading = re.compile(r'\s*<(h[1-6])[^>]*>(?P<title>.+?)</\1>$'
 
         self.html = self._excerpt = ''
 
-        for nonsection, section in itersections(self.source):
+        source = self._pluckhandcraftedexcerpt(references)
+
+        for nonsection, section in itersections(source):
             self.html += convert(nonsection, references) + '\n'
 
             match = matchupdate(section)

File tests/__init__.py

     def test_replacements(self, document):
         self.assertEqual(document.excerpt(), u'<p>Text — more text.</p>')
 
+    @md('''
+        date: 27 August 2011
+        time: 3:00pm
+        zone: America/Los_Angeles
+        
+        This is an excerpt.
+        
+        # Title
+        
+        Paragraph.
+        ''')
+    def test_handcrafted_excerpt(self, document):
+        self.assertEqual(document.excerpt(), '<p>This is an excerpt.</p>')
+
+    @md('''
+        This is the first part of the excerpt.
+        
+        # Title
+        
+        Paragraph.
+        
+        * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+        
+        This is the rest of the excerpt.
+        
+        * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+        ''')
+    def test_composite_excerpt(self, document):
+        self.assertEqual(document.excerpt(),
+                         '<p>This is the first part of the excerpt.</p>\n'
+                         '<p>This is the rest of the excerpt.</p>')
+
 
 class FileNameTests(unittest.TestCase):