Commits

Christian Jann committed 86d3d6c

Emit 'patch-node' signal

Comments (0)

Files changed (4)

tinkerer/ext/aggregator.py

         for post in posts:
             # deepcopy metadata and patch links
             metadata = copy.deepcopy(env.blog_metadata[post])
-            metadata.body = patch.patch_links(
+            metadata.body = patch.patch_links(app,
                     metadata.body, 
                     post[:11], # first 11 characters is path (YYYY/MM/DD/)
                     post[11:], # following characters represent filename

tinkerer/ext/blog.py

     # hook up our html5 translator instead of the Sphinx built-in html 
     # translator
     app.config.html_translator_class = "tinkerer.ext.html5.SmartyPantsHTML5Translator"
-    
+
+    app._events['patch-node'] = 'node, docpath, docname'
+
     # new directives
     app.add_directive("author", author.AuthorDirective)
     app.add_directive("comments", metadata.CommentsDirective)

tinkerer/ext/patch.py

 except:
     import __builtin__
 
-try:
-    from patch_extra import patch_node_extra
-except ImportError:
-    patch_node_extra = None
-
 # check whether unichr builtin exists, otherwise use chr
 if "unichr" not in __builtin__.__dict__:
     unichr = chr
-    
-            
+
+
 
 def build_html_only_codepoints():
     """
 
 
 
-def patch_links(body, docpath, docname=None, link_title=False):
+def patch_links(app, body, docpath, docname=None, link_title=False):
     '''
     Parses the document body and calls patch_node from the document root
     to fix hyperlinks. Also hyperlinks document title. Returns resulting 
     '''
     in_str = convert(body).encode("utf-8")
     doc = xml.dom.minidom.parseString(in_str)
-    patch_node(doc, docpath, docname)
+    patch_node(app, doc, docpath, docname)
 
     body = doc.toxml()
     if docname:
 
 
 
-def patch_node(node, docpath, docname=None):
+def patch_node(app, node, docpath, docname=None):
     '''
     Recursively patches links in nodes.
     '''
             # to revert change on protocol prefix as normpath deduplicates
             # // (http:// becomes http:/)
             ref.value = path.normpath(ref.value).replace(":/", "://")
-    if patch_node_extra:
-        patch_node_extra(node, docpath, docname)
+
+    # This may throw a UnicodeEncodeError in sphinx/application.py self.debug2()
+    # in Python 2.x because node's __repr__() may return unicode characters
+    # https://bitbucket.org/birkenfeld/sphinx/commits/71444a5b62861a113abdd175a83eaf54ee7da3d3
+    # http://sourceforge.net/tracker/?func=detail&aid=3601607&group_id=38414&atid=422030
+    # I have temporary changed the line self.debug2('[app] emitting event: %r%s', event, repr(args)[:100])
+    # to self.debug2('[app] emitting event: %r%s', event) in sphinx/application.py
+    # The error is already patched in Sphinx and Docutils, they will return unicode.__repr__
+    # on Python 2.x, but not in minidom, maybe xml.etree.ElementTree works better.
+    app.emit('patch-node', node, docpath, docname)
 
     # recurse            
     for node in node.childNodes:
-        patch_node(node, docpath, docname)
+        patch_node(app, node, docpath, docname)
 
 
 

tinkerer/ext/rss.py

         context["items"].append({
                     "title": env.titles[post].astext(),
                     "link": link,
-                    "description": patch.strip_xml_declaration(patch.patch_links(
+                    "description": patch.strip_xml_declaration(patch.patch_links(app,
                             env.blog_metadata[post].body, 
                             app.config.website + post[:11], # first 11 characters is path (YYYY/MM/DD/)
                             post[11:])), # following characters represent filename