Commits

David Chambers committed 7ca258d

Replaced `_pluck_title` function with `Resource#_plucktitle`.

  • Participants
  • Parent commits ea4fcb5
  • Branches 0.8

Comments (0)

Files changed (1)

         return [StyleSheet(path) for path in self.required_files()
                 if path.endswith('.css')]
 
+    def _plucktitle():
+        subcodetags = re.compile(r'<code>(.*?)</code>').sub
+        matchheading = re.compile(r'\s*<(h[1-6])[^>]*>(?P<title>.+?)</\1>$'
+                                  r'(?P<html>[\s\S]*)', re.MULTILINE).match
+        def plucktitle(self):
+            self.title_text = self.meta.get('title', u'')
+            self.title = convert_html_chars(self.title_text)
 
-_code_tags_sub = re.compile(r'<code>(.*?)</code>').sub
-_heading_match = re.compile(r'\s*<(h[1-6])[^>]*>(?P<title>.+?)</\1>$'
-                            r'(?P<html>[\s\S]*)', re.MULTILINE).match
+            if not self.title_text:
+                match = matchheading(self.html)
+                if match:
+                    self.html = match.group('html')
+                    self.title = match.group('title')
+                    self.title_text = subcodetags(r'`\1`', self.title)
+                    self.title_text = lxml.html.fromstring(self.title_text)
+                    # convert `lxml.etree._ElementUnicodeResult` to `unicode`
+                    # for pickling
+                    self.title_text = unicode(self.title_text.text_content())
+            self.title = mark_safe(self.title)
+        return plucktitle
+    _plucktitle = _plucktitle()
 
-def _pluck_title(resource):
-    title_text = resource.meta.get('title', u'')
-    title = convert_html_chars(title_text)
-    html = resource.html
-
-    if not title_text:
-        match = _heading_match(html)
-        if match:
-            title, html = match.group('title'), match.group('html')
-            title_text = _code_tags_sub(r'`\1`', title)
-            title_text = lxml.html.fromstring(title_text).text_content()
-            # convert `lxml.etree._ElementUnicodeResult` to `unicode`
-            # for pickling
-            title_text = unicode(title_text)
-
-    return title_text, mark_safe(title), mark_safe(html)
 
 def processmetalists(meta):
     for key, value in meta.items():
             md.reset()
             self.html = md.convert(self.source)
             self.meta = processmetalists(getattr(md, 'Meta', {}))
-            _, title, self.html = _pluck_title(self)
-            if title:
-                self.name = title
+            self._plucktitle()
+            if self.title:
+                self.name = self.title
 
         for name in validfilenames(os.listdir(dirpath)):
             path = os.path.join(dirpath, name)
                 logger.exception('Document date/time/zone '
                                  'incorrectly formatted')
 
-        self.title_text, self.title, self.html = _pluck_title(self)
+        self._plucktitle()
         self.html = embed(self.html.rstrip())
         self.html = mark_safe(stripskips(replace(self.html.lstrip('\n'))))
         self.type = self.meta.get('type', 'post' if self.datetime else 'page')