Commits

David Chambers  committed 49bf9e6

Added a property attribute to `Resource` for managing link definitions.

  • Participants
  • Parent commits 1a79535
  • Branches 0.8

Comments (0)

Files changed (1)

             aliases = updated
         self._aliases = aliases
         self._slug = canon_fragments[-1]
+        self._references = {}
 
     def identifier(self):
         return self.urls['canon']['rel']
         return [StyleSheet(path) for path in self.required_files()
                 if path.endswith('.css')]
 
+    def _setreferences(self, references):
+        self._references = references.copy()
+    references = property(lambda self: self._references.copy(), _setreferences)
+
     _subhandcraftedexcerpt = re.compile(
         r'''^
         (?:                                 # optional metadata
         (?= \#(?!\#) | .+\n={3,} )          # followed by an h1 of either type
         ''', re.VERBOSE).sub
 
-    def _pluckhandcraftedexcerpt(self, references):
+    def _pluckhandcraftedexcerpt(self):
         def repl(match):
             excerpt = match.group('excerpt')
-            self._excerpt = convert(excerpt, references) + '\n'
+            self._excerpt = self._convert(excerpt) + '\n'
             return ''
         return self._subhandcraftedexcerpt(repl, self.source)
 
 _findtime = re.compile(r'\b(?:[1-9]|1[0-2]):\d\d[ap]m\b').search
 _findzone = re.compile(r'\b[a-zA-Z_]+(?:/[a-zA-Z_-]+){1,2}\b').search
 
-def renderupdate(match, references):
+def renderupdate(resource, match):
     metadata = match.group('metadata')
 
     date = _finddate(metadata)
         return
 
     update.headinglevel = len(match.group('level'))
-    update.html = mark_safe(convert(match.group('body'), references))
+    update.html = mark_safe(resource._convert(match.group('body')))
 
     return _renderupdate(Context({'update': update}))
 
-def convert():
-    sub = re.compile(r'^[ ]{,3}(?:\*[ ]?){3,}[ \t]*$', re.MULTILINE).sub
-    def warn(match):
-        logger.warning('Horizontal rules comprised of asterisks delimit '
-                       'excerpts/updates (use hyphens or underscores instead)')
-        return ''
-    def render(text, references):
-        md.references = references.copy()
-        return md.convert(sub(warn, text))
-    return render
-convert = convert()
-
 class Document(Resource):
     @classmethod
     def get(cls, filepath):
 
         self.convert()
 
+    def _convert():
+        sub = re.compile(r'^[ ]{,3}(?:\*[ ]?){3,}[ \t]*$', re.MULTILINE).sub
+        def warn(match):
+            logger.warning('Horizontal rules comprised of '
+                           'asterisks delimit excerpts/updates '
+                           '(use hyphens or underscores instead)')
+            return ''
+        def render(self, text):
+            md.reset()
+            md.references = self.references
+            return md.convert(sub(warn, text))
+        return render
+    _convert = _convert()
+
     def convert(self, source=None):
         if source is not None:
             self.source = source
         md.reset()
         # discern link definitions
         md.convert(self.source)
-        references = md.references.copy()
-
-        # Having invoked `Markdown#convert` we've populated our
-        # `Markdown` instance with unwanted values. The HeaderId
-        # extension, for example, stores the ids it encounters on
-        # the `Markdown` instance. We must reset the instance to
-        # remove this unwanted data.
-        md.reset()
+        self.references = md.references
 
         self.meta = processmetalists(getattr(md, 'Meta', {}))
 
         self.html = self._excerpt = ''
 
-        source = self._pluckhandcraftedexcerpt(references)
+        source = self._pluckhandcraftedexcerpt()
 
         for nonsection, section in itersections(source):
-            self.html += convert(nonsection, references) + '\n'
+            self.html += self._convert(nonsection) + '\n'
 
             match = matchupdate(section)
             if match:
-                update = renderupdate(match, references)
+                update = renderupdate(self, match)
                 if update:
                     self.html += update
                     continue
 
             if section:
-                excerpt = convert(section, references) + '\n'
+                excerpt = self._convert(section) + '\n'
                 self._excerpt += excerpt
                 self.html += excerpt