Back to index

plone3  3.1.7
rest.py
Go to the documentation of this file.
00001 from Products.PortalTransforms.interfaces import itransform
00002 from reStructuredText import HTML
00003 import sys
00004 
00005 class rest:
00006     r"""Converts from reST to HTML.
00007 
00008       >>> transform = rest()
00009       >>> class D:
00010       ...     def setData(self, data):
00011       ...         self.value = data
00012 
00013       >>> data = transform.convert('*hello world*', D())
00014       >>> print data.value
00015       <p><em>hello world</em></p>
00016       <BLANKLINE>
00017 
00018     We want the 'raw' and 'include' directives to be disabled by
00019     default:
00020 
00021       >>> try:
00022       ...     transform.convert('.. raw:: html\n  :file: <isonum.txt>', D())
00023       ... except NotImplementedError:
00024       ...     print 'Good'
00025       ... else:
00026       ...     print 'Failure'
00027       Good
00028 
00029       >>> try:
00030       ...     transform.convert('.. include:: <isonum.txt>', D())
00031       ... except NotImplementedError:
00032       ...     print 'Good'
00033       ... else:
00034       ...     print 'Failure'
00035       Good
00036     """
00037     __implements__ = itransform
00038 
00039     __name__ = "rest_to_html"
00040     inputs  = ("text/x-rst", "text/restructured",)
00041     output = "text/html"
00042 
00043     def __init__(self, name=None, **kwargs):
00044         if name:
00045             self.__name__ = name
00046 
00047         self.config = {
00048             'inputs': self.inputs,
00049             'output': self.output,
00050             'report_level': 2,
00051             'initial_header_level': 2,
00052             }
00053 
00054         self.config_metadata = {
00055             'inputs' :
00056             ('list', 'Inputs', 'Input(s) MIME type. Change with care.'),
00057             'initial_header_level' :
00058             ('int', 'Initial Header Level',
00059              'Level of first header tag. Setting it to "2" will make '
00060              'the first header be "<h2>".'),
00061             'report_level' :
00062             ('int', 'Report Level',
00063              'Level of error reporting. Set to "1" will display all '
00064              'messages. Setting it to "5" will display no messages.'),
00065             }
00066 
00067         self.config.update(kwargs)
00068 
00069 
00070     def name(self):
00071         return self.__name__
00072 
00073     def convert(self, orig, data, **kwargs):
00074         # do the format
00075         encoding        = kwargs.get('encoding', 'utf-8')
00076         input_encoding  = kwargs.get('input_encoding', encoding)
00077         output_encoding = kwargs.get('output_encoding', encoding)
00078         language        = kwargs.get('language', 'en')
00079         warnings        = kwargs.get('warnings', None)
00080 
00081         initial_header_level = int(self.config.get('initial_header_level', 2))
00082         report_level = int(self.config.get('report_level', 2))
00083 
00084         settings = {'documentclass': '',
00085                     'traceback': 1,
00086                     }
00087 
00088         html = HTML(orig,
00089                     input_encoding=input_encoding,
00090                     output_encoding=output_encoding,
00091                     language_code=language,
00092                     initial_header_level=initial_header_level,
00093                     report_level=report_level,
00094                     warnings=warnings,
00095                     settings=settings)
00096 
00097         html = html.replace(' class="document"', '', 1)
00098         data.setData(html)
00099         return data
00100 
00101 def register():
00102     return rest()