quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 14 Dec 2009 16:25:25 +0100
branchstable
changeset 4118 8a9a00a9405c
parent 4117 3f8b7ffdda53
child 4119 85bb30fb6d9b
quick and dirty fix trying to avoid rest directive conflicts when using sphinx (which seems to import the code)
cwconfig.py
ext/rest.py
--- a/cwconfig.py	Mon Dec 14 15:35:21 2009 +0100
+++ b/cwconfig.py	Mon Dec 14 16:25:25 2009 +0100
@@ -560,6 +560,15 @@
         self.adjust_sys_path()
         self.load_defaults()
         self.translations = {}
+        # don't register ReStructured Text directives by simple import, avoid pb
+        # with eg sphinx.
+        # XXX should be done properly with a function from cw.uicfg
+        try:
+            from cubicweb.ext.rest import cw_rest_init
+        except ImportError:
+            pass
+        else:
+            cw_rest_init()
 
     def adjust_sys_path(self):
         self.cls_adjust_sys_path()
--- a/ext/rest.py	Mon Dec 14 15:35:21 2009 +0100
+++ b/ext/rest.py	Mon Dec 14 16:25:25 2009 +0100
@@ -80,8 +80,6 @@
     return [nodes.reference(rawtext, utils.unescape(rest), refuri=ref,
                             **options)], []
 
-register_canonical_role('eid', eid_reference_role)
-
 
 def winclude_directive(name, arguments, options, content, lineno,
                        content_offset, block_text, state, state_machine):
@@ -146,14 +144,13 @@
 winclude_directive.arguments = (1, 0, 1)
 winclude_directive.options = {'literal': directives.flag,
                               'encoding': directives.encoding}
-directives.register_directive('winclude', winclude_directive)
 
 try:
     from pygments import highlight
     from pygments.lexers import get_lexer_by_name, LEXERS
     from pygments.formatters import HtmlFormatter
 except ImportError:
-    pass
+    pygments_directive = None
 else:
     _PYGMENTS_FORMATTER = HtmlFormatter()
 
@@ -174,7 +171,6 @@
 
     pygments_directive.arguments = (1, 0, 1)
     pygments_directive.content = 1
-    directives.register_directive('sourcecode', pygments_directive)
 
 
 class CubicWebReSTParser(Parser):
@@ -244,3 +240,15 @@
             data = unicode(data, encoding, 'replace')
         return xml_escape(req._('error while publishing ReST text')
                            + '\n\n' + data)
+
+
+_INITIALIZED = False
+def cw_rest_init():
+    global _INITIALIZED
+    if _INITIALIZED:
+        return
+    _INITIALIZED = True
+    register_canonical_role('eid', eid_reference_role)
+    directives.register_directive('winclude', winclude_directive)
+    if pygments_directive is not None:
+        directives.register_directive('sourcecode', pygments_directive)