Commits

David Chambers committed 49d776c

Changed the way in which link definitions are stored – utilizing Python-Markdown's caching was a nice idea, but caused inner scopes to inherit link definitions from preceding inner scopes. Added accompanying unit test.

Comments (0)

Files changed (2)

         else:
             self._contents = contents
 
+        md.reset()
+        # populate `md.references` with any link identifiers defined in enclosing scopes
+        parent = self._parent
+        while parent:
+            if hasattr(parent, '_references'):
+                md.references.update((k, v) for k, v in parent._references.items() if k not in md.references)
+            parent = parent._parent
+
         contents = u'%s\n' % re.sub(RE['\r\n?'], '\n', contents)
         if isinstance(self, Update):
             contents = re.sub(RE['update_pipes'], u'', contents)
         # updates
         split = re.split(RE['update'], contents)
         chunks = md.convert(u'\n<mango/>\n'.join(split[0::2])).split('<mango/>')
+        self._references = md.references.copy() # save link identifiers defined in current scope
         self.meta = getattr(md, 'Meta', {})
         updates = [update_template.render(Context({'update': Update(parent=self).convert(item)})) for item in split[1::2]]
         combined = [None] * (len(chunks) + len(updates))
                 if master._filepath:
                     with open(master._filepath, 'w') as f:
                         contents = self._contents
-                        if master._contents.find(contents) == -1: # update within an excerpt
-                            contents = re.sub(RE['update_pipes'], r'| \1', contents)
                         contents = master._contents.replace(contents, re.sub(pattern, repl, contents), 1)
                         f.write(contents.encode('utf-8'))
                         master._contents = contents
             self.html += fragment
         self.body = re.sub(RE['skip'], '', self.body)
         self.html = mark_safe(re.sub(RE['skip'], '', self.html))
-        self.excerpt = (Excerpt(parent=self).convert('\n\n'.join(snippets)) if snippets else self).html
-        self.type = self.meta.get('type', 'post' if self.datetime else 'page')
+        if type(self) is Document:
+            self.excerpt = '\n'.join([Excerpt(parent=self).convert(s).html for s in snippets]) if snippets else self.html
+            self.type = self.meta.get('type', 'post' if self.datetime else 'page')
 
         # attach comments thread
         if FORUM and hasattr(self, 'urls'):
                     cache.set(cache_key, self._thread, 24*60*60)
                     logger.debug('Disqus thread for "%s" cached' % self._thread.title)
 
-        if type(self) is Document:
-            md.reset() # clear cached link definitions
+        md.reset() # clear cached link definitions
 
         return self
 
         return tag in self.meta.get('tags', [])
 
     def master(self):
-        return self._parent or self
+        master = self
+        parent = self._parent
+        while parent:
+            master = parent
+            parent = parent._parent
+        return master
 
     def __str__(self):
         return self.__unicode__().encode('utf-8')
                 '<p><a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> meets <a href="http://www.djangoproject.com/">Django</a>.</p>',
                 '</div></div>\n')))
 
+    def test_inherits_only_from_enclosing_scopes(self):
+        document = create_document(body='''
+            | [Mango][1].
+            | 
+            | [1]: http://mango.io/
+            
+            More text.
+            
+            | [Django][1].
+            
+            [1]: http://www.djangoproject.com/
+        ''')
+        self.assertEqual(document.excerpt, '\n'.join((
+                '<p><a href="http://mango.io/">Mango</a>.</p>',
+                '<p><a href="http://www.djangoproject.com/">Django</a>.</p>')))
+
 
 class FileNameTests(unittest.TestCase):