rest.py 2.25 KB
Newer Older
1 2
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implements
Yusei Tahara's avatar
Yusei Tahara committed
3 4 5 6
from reStructuredText import HTML
import sys

class rest:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    r"""Converts from reST to HTML.

      >>> transform = rest()
      >>> class D:
      ...     def setData(self, data):
      ...         self.value = data

      >>> data = transform.convert('*hello world*', D())
      >>> print data.value
      <p><em>hello world</em></p>
      <BLANKLINE>

    We want the 'raw' and 'include' directives to be disabled by
    default:

      >>> try:
23
      ...     out = transform.convert('.. raw:: html\n  :file: <isonum.txt>', D())
24 25 26
      ... except NotImplementedError:
      ...     print 'Good'
      ... else:
27 28 29 30
      ...     if "&quot;raw&quot; directive disabled." in out.value:
      ...         print 'Good'
      ...     else:
      ...         print 'Failure'
31 32 33
      Good

      >>> try:
34
      ...     out = transform.convert('.. include:: <isonum.txt>', D())
35 36 37
      ... except NotImplementedError:
      ...     print 'Good'
      ... else:
38 39 40 41
      ...     if "&quot;include&quot; directive disabled." in out.value:
      ...         print 'Good'
      ...     else:
      ...         print 'Failure'
42 43
      Good
    """
44
    implements(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

    __name__ = "rest_to_html"
    inputs  = ("text/x-rst", "text/restructured",)
    output = "text/html"

    def name(self):
        return self.__name__

    def convert(self, orig, data, **kwargs):
        # do the format
        encoding        = kwargs.get('encoding', 'utf-8')
        input_encoding  = kwargs.get('input_encoding', encoding)
        output_encoding = kwargs.get('output_encoding', encoding)
        language        = kwargs.get('language', 'en') 
        warnings        = kwargs.get('warnings', None) 
        settings = {'documentclass': '',
                    'traceback': 1,
               }
        html = HTML(orig, 
64
                    report_level=2,
Yusei Tahara's avatar
Yusei Tahara committed
65 66 67 68 69 70 71 72 73 74 75
                    input_encoding=input_encoding, 
                    output_encoding=output_encoding, 
                    language_code=language, 
                    warnings=warnings, 
                    settings=settings)
        html = html.replace(' class="document"', '', 1)
        data.setData(html)
        return data

def register():
    return rest()