ext/rest.py
changeset 4181 c79135c217df
parent 3418 7b49fa7e942d
parent 4118 8a9a00a9405c
child 4252 6c4f109c2b03
--- a/ext/rest.py	Tue Dec 22 09:27:28 2009 +0100
+++ b/ext/rest.py	Tue Dec 22 10:27:49 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)