common/rest.py
changeset 996 9495a0594b95
parent 0 b97547f5f1fa
equal deleted inserted replaced
995:7163b5258f98 996:9495a0594b95
     1 """rest publishing functions
     1 """rest publishing functions
     2 
     2 
     3 contains some functions and setup of docutils for cubicweb
     3 contains some functions and setup of docutils for cubicweb. Provides the
       
     4 following ReST directives:
       
     5 
       
     6 * `eid`, create link to entity in the repository by their eid
       
     7 
       
     8 * `card`, create link to card entity in the repository by their wikiid
       
     9   (proposing to create it when the refered card doesn't exist yet)
       
    10 
       
    11 * `winclude`, reference to a web documentation file (in wdoc/ directories)
       
    12 
       
    13 * `sourcecode` (if pygments is installed), source code colorization
     4 
    14 
     5 :organization: Logilab
    15 :organization: Logilab
     6 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
    16 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     7 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
    17 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     8 """
    18 """
     9 __docformat__ = "restructuredtext en"
    19 __docformat__ = "restructuredtext en"
    10 
    20 
    11 from cStringIO import StringIO
    21 from cStringIO import StringIO
   154 winclude_directive.arguments = (1, 0, 1)
   164 winclude_directive.arguments = (1, 0, 1)
   155 winclude_directive.options = {'literal': directives.flag,
   165 winclude_directive.options = {'literal': directives.flag,
   156                               'encoding': directives.encoding}
   166                               'encoding': directives.encoding}
   157 directives.register_directive('winclude', winclude_directive)
   167 directives.register_directive('winclude', winclude_directive)
   158 
   168 
       
   169 try:
       
   170     from pygments import highlight
       
   171     from pygments.lexers import get_lexer_by_name, LEXERS
       
   172     from pygments.formatters import HtmlFormatter
       
   173 except ImportError:
       
   174     pass
       
   175 else:
       
   176     _PYGMENTS_FORMATTER = HtmlFormatter()
       
   177 
       
   178     def pygments_directive(name, arguments, options, content, lineno,
       
   179                            content_offset, block_text, state, state_machine):
       
   180         try:
       
   181             lexer = get_lexer_by_name(arguments[0])
       
   182         except ValueError:
       
   183             import traceback
       
   184             traceback.print_exc()
       
   185             print sorted(aliases for module_name, name, aliases, _, _  in LEXERS.itervalues())
       
   186             # no lexer found
       
   187             lexer = get_lexer_by_name('text')
       
   188         print 'LEXER', lexer
       
   189         parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER)
       
   190         context = state.document.settings.context
       
   191         context.req.add_css('pygments.css')
       
   192         return [nodes.raw('', parsed, format='html')]
       
   193      
       
   194     pygments_directive.arguments = (1, 0, 1)
       
   195     pygments_directive.content = 1
       
   196     directives.register_directive('sourcecode', pygments_directive)
       
   197 
       
   198 
   159 class CubicWebReSTParser(Parser):
   199 class CubicWebReSTParser(Parser):
   160     """The (customized) reStructuredText parser."""
   200     """The (customized) reStructuredText parser."""
   161 
   201 
   162     def __init__(self):
   202     def __init__(self):
   163         self.initial_state = 'Body'
   203         self.initial_state = 'Body'