cubicweb/ext/rest.py
changeset 12567 26744ad37953
parent 12337 04ff0d3ef1d3
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    36 
    36 
    37 import sys
    37 import sys
    38 from itertools import chain
    38 from itertools import chain
    39 from logging import getLogger
    39 from logging import getLogger
    40 from os.path import join
    40 from os.path import join
    41 
    41 from urllib.parse import urlsplit
    42 from six import text_type
       
    43 from six.moves.urllib.parse import urlsplit
       
    44 
    42 
    45 from docutils import statemachine, nodes, utils, io
    43 from docutils import statemachine, nodes, utils, io
    46 from docutils.core import Publisher
    44 from docutils.core import Publisher
    47 from docutils.parsers.rst import Parser, states, directives, Directive
    45 from docutils.parsers.rst import Parser, states, directives, Directive
    48 from docutils.parsers.rst.roles import register_canonical_role, set_classes
    46 from docutils.parsers.rst.roles import register_canonical_role, set_classes
   401     :rtype: unicode
   399     :rtype: unicode
   402     :return:
   400     :return:
   403       the data formatted as HTML or the original data if an error occurred
   401       the data formatted as HTML or the original data if an error occurred
   404     """
   402     """
   405     req = context._cw
   403     req = context._cw
   406     if isinstance(data, text_type):
   404     if isinstance(data, str):
   407         encoding = 'utf-8'
   405         encoding = 'utf-8'
   408         # remove unprintable characters unauthorized in xml
   406         # remove unprintable characters unauthorized in xml
   409         data = data.translate(ESC_UCAR_TABLE)
   407         data = data.translate(ESC_UCAR_TABLE)
   410     else:
   408     else:
   411         encoding = req.encoding
   409         encoding = req.encoding
   446         # necessary for proper garbage collection, else a ref is kept somewhere in docutils...
   444         # necessary for proper garbage collection, else a ref is kept somewhere in docutils...
   447         del pub.settings.context
   445         del pub.settings.context
   448         return res
   446         return res
   449     except BaseException:
   447     except BaseException:
   450         LOGGER.exception('error while publishing ReST text')
   448         LOGGER.exception('error while publishing ReST text')
   451         if not isinstance(data, text_type):
   449         if not isinstance(data, str):
   452             data = text_type(data, encoding, 'replace')
   450             data = data.encode(encoding, 'replace')
   453         return xml_escape(req._('error while publishing ReST text')
   451         return xml_escape(req._('error while publishing ReST text')
   454                            + '\n\n' + data)
   452                            + '\n\n' + data)
   455 
   453 
   456 
   454 
   457 _INITIALIZED = False
   455 _INITIALIZED = False