ext/rest.py
branchtls-sprint
changeset 1447 a1ca676294f0
parent 1323 5350c5aa0d31
child 1643 59c5cec05bbd
equal deleted inserted replaced
1446:e951a860eeaf 1447:a1ca676294f0
   187             lexer = get_lexer_by_name('text')
   187             lexer = get_lexer_by_name('text')
   188         parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER)
   188         parsed = highlight(u'\n'.join(content), lexer, _PYGMENTS_FORMATTER)
   189         context = state.document.settings.context
   189         context = state.document.settings.context
   190         context.req.add_css('pygments.css')
   190         context.req.add_css('pygments.css')
   191         return [nodes.raw('', parsed, format='html')]
   191         return [nodes.raw('', parsed, format='html')]
   192      
   192 
   193     pygments_directive.arguments = (1, 0, 1)
   193     pygments_directive.arguments = (1, 0, 1)
   194     pygments_directive.content = 1
   194     pygments_directive.content = 1
   195     directives.register_directive('sourcecode', pygments_directive)
   195     directives.register_directive('sourcecode', pygments_directive)
   196 
   196 
   197 
   197 
   214                                                convert_whitespace=1)
   214                                                convert_whitespace=1)
   215         self.statemachine.run(inputlines, document, inliner=self.inliner)
   215         self.statemachine.run(inputlines, document, inliner=self.inliner)
   216         self.finish_parse()
   216         self.finish_parse()
   217 
   217 
   218 
   218 
   219 _REST_PARSER = CubicWebReSTParser()
       
   220 
       
   221 def rest_publish(context, data):
   219 def rest_publish(context, data):
   222     """publish a string formatted as ReStructured Text to HTML
   220     """publish a string formatted as ReStructured Text to HTML
   223     
   221 
   224     :type context: a cubicweb application object
   222     :type context: a cubicweb application object
   225 
   223 
   226     :type data: str
   224     :type data: str
   227     :param data: some ReST text
   225     :param data: some ReST text
   228 
   226 
   237         encoding = req.encoding
   235         encoding = req.encoding
   238     settings = {'input_encoding': encoding, 'output_encoding': 'unicode',
   236     settings = {'input_encoding': encoding, 'output_encoding': 'unicode',
   239                 'warning_stream': StringIO(), 'context': context,
   237                 'warning_stream': StringIO(), 'context': context,
   240                 # dunno what's the max, severe is 4, and we never want a crash
   238                 # dunno what's the max, severe is 4, and we never want a crash
   241                 # (though try/except may be a better option...)
   239                 # (though try/except may be a better option...)
   242                 'halt_level': 10, 
   240                 'halt_level': 10,
   243                 }
   241                 }
   244     if context:
   242     if context:
   245         if hasattr(req, 'url'):
   243         if hasattr(req, 'url'):
   246             base_url = req.url()
   244             base_url = req.url()
   247         elif hasattr(context, 'absolute_url'):
   245         elif hasattr(context, 'absolute_url'):
   250             base_url = req.base_url()
   248             base_url = req.base_url()
   251     else:
   249     else:
   252         base_url = None
   250         base_url = None
   253     try:
   251     try:
   254         return publish_string(writer=Writer(base_url=base_url),
   252         return publish_string(writer=Writer(base_url=base_url),
   255                               parser=_REST_PARSER, source=data,
   253                               parser=CubicWebReSTParser(), source=data,
   256                               settings_overrides=settings)
   254                               settings_overrides=settings)
   257     except Exception:
   255     except Exception:
   258         LOGGER.exception('error while publishing ReST text')
   256         LOGGER.exception('error while publishing ReST text')
   259         if not isinstance(data, unicode):
   257         if not isinstance(data, unicode):
   260             data = unicode(data, encoding, 'replace')
   258             data = unicode(data, encoding, 'replace')