예제 #1
0
파일: rest.py 프로젝트: CGTIC/Plone_SP
    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)

        initial_header_level = int(self.config.get('initial_header_level', 2))
        report_level = int(self.config.get('report_level', 2))

        settings = {'documentclass': '',
                    'traceback': 1,
                    }

        html = HTML(orig,
                    input_encoding=input_encoding,
                    output_encoding=output_encoding,
                    language_code=language,
                    initial_header_level=initial_header_level,
                    report_level=report_level,
                    warnings=warnings,
                    settings=settings)

        html = html.replace(' class="document"', '', 1)
        data.setData(html)
        return data
예제 #2
0
    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)

        initial_header_level = int(self.config.get('initial_header_level', 2))
        report_level = int(self.config.get('report_level', 2))

        settings = {
            'documentclass': '',
            'traceback': 1,
        }

        html = HTML(orig,
                    input_encoding=input_encoding,
                    output_encoding=output_encoding,
                    language_code=language,
                    initial_header_level=initial_header_level,
                    report_level=report_level,
                    warnings=warnings,
                    settings=settings)

        html = html.replace(' class="document"', '', 1)
        data.setData(html)
        return data
예제 #3
0
    def transform(self, data, options=None):
        if self._validate(data) is None:
            return None

        header_level = self.header_level
        report_level = self.report_level
        language = self.language
        warnings = self.warnings
        if options:
            header_level = options.get('header_level', header_level)
            report_level = options.get('report_level', report_level)
            language = options.get('language', language)
            warnings = options.get('warnings', warnings)

        html = HTML(u''.join(data).encode('utf-8'),
                    input_encoding='utf-8',
                    output_encoding='utf-8',
                    language_code=language,
                    initial_header_level=header_level,
                    report_level=report_level,
                    warnings=warnings,
                    settings=dict(documentclass='', traceback=1))

        html = html.replace(' class="document"', '', 1)
        return TransformResult(StringIter(unicode(html, 'utf-8')))
예제 #4
0
    def readMeHtml(self):
        """Renders README.txt (in rst) inside the page"""

        opd = os.path.dirname
        readme_path = os.path.join(os.path.abspath(opd(opd(__file__))), 'README.txt')
        readme_html = HTML(file(readme_path).read(), report_level=100) # No errors/warnings -> faster
        # F*****g header changes the base href, so we need to tweak all href="#..."
        actual_url = self.request.URL
        return readme_html.replace('href="#', 'href="%s#' % actual_url)
예제 #5
0
    def readMeHtml(self):
        """Renders README.txt (in rst) inside the page"""

        opd = os.path.dirname
        readme_path = os.path.join(os.path.abspath(opd(opd(__file__))),
                                   'README.txt')
        readme_html = HTML(file(readme_path).read(),
                           report_level=100)  # No errors/warnings -> faster
        # F*****g header changes the base href, so we need to tweak all href="#..."
        actual_url = self.request.URL
        return readme_html.replace('href="#', 'href="%s#' % actual_url)
예제 #6
0
    def test_raw_directive_url_option_raises(self):
        source = '.. raw:: html\n  :url: http://www.zope.org'
        result = HTML(source)

        # The raw: directive hasn't been rendered, it remains
        # verbatimly in the rendered output.  Instead a warning
        # message is presented:
        self.assert_(source in result)
        self.assert_(docutils_raw_warning in result)
예제 #7
0
    def test_raw_directive_file_option_raises(self):
        source = '.. raw:: html\n  :file: inclusion.txt'
        result = HTML(source)

        # The raw: directive hasn't been rendered, it remains
        # verbatimly in the rendered output.  Instead a warning
        # message is presented:
        self.assert_(source in result)
        self.assert_(docutils_raw_warning in result)
예제 #8
0
    def test_include_directive_raises(self):
        directive = '.. include:: /etc/passwd'
        source = 'hello world\n %s' % directive
        result = HTML(source)

        # The include: directive hasn't been rendered, it remains
        # verbatimly in the rendered output.  Instead a warning
        # message is presented:
        self.assert_(directive in result)
        self.assert_(docutils_include_warning in result)
예제 #9
0
    def test_raw_directive_disabled(self):
        EXPECTED = '<h1>HELLO WORLD</h1>'
        source = '.. raw:: html\n\n  %s\n' % EXPECTED
        result = HTML(source)  # don't raise, but don't work either

        # The raw: directive hasn't been rendered, it remains
        # verbatimly in the rendered output.  Instead a warning
        # message is presented:
        self.assert_(EXPECTED not in result)
        self.assert_(cgi.escape(EXPECTED) in result)
        self.assert_(docutils_raw_warning in result)
예제 #10
0
    def test_raw_directive_disabled(self):

        EXPECTED = '<h1>HELLO WORLD</h1>'

        source = '.. raw:: html\n\n  %s\n' % EXPECTED
        result = HTML(source)  # don't raise, but don't work either
        self.failIf(EXPECTED in result)

        self.failUnless("&quot;raw&quot; directive disabled" in result)
        from cgi import escape
        self.failUnless(escape(EXPECTED) in result)
예제 #11
0
    def testHeaderLevel(self):

        encoding = 'iso-8859-15'
        for level in range(0, 5):
            html = HTML(txt,
                        input_encoding=encoding,
                        output_encoding=encoding,
                        initial_header_level=level)

            expected = '<h%d><a id="hello-world" name="hello-world">Hello World</a></h%d>' %\
                        (level+1, level+1)
            self.assertEqual(expected in html, True)

            expected = '<h%d><a id="von-v-geln-und-fen" name="von-v-geln-und-fen">Von Vögeln und Öfen</a></h%d>' %\
                        (level+1, level+1)
            self.assertEqual(expected in html, True)
예제 #12
0
def construct_multipart_from_stx(from_addr,
                                 to_addr,
                                 subject,
                                 body,
                                 other_headers=None,
                                 encoding='utf8'):
    """All inputs to this method are expected to be unicode or ascii.

    This converts the input text to html using the stx converter

        >>> m = construct_multipart_from_stx(u'*****@*****.**',
        ...     u'*****@*****.**',
        ...     u'Un Subj\xc3\xa9t',
        ...     u'A simple body with some non ascii t\xc3\xa9xt with "a link":http://www.example.com')
        >>> print m.as_string() #doctest: +ELLIPSIS
        Content-Type: multipart/alternative; boundary="..."
        MIME-Version: 1.0
        From: [email protected]
        To: [email protected]
        Subject: Un =?utf8?b?U3ViasODwql0?=
        <BLANKLINE>
        --...
        Content-Type: text/plain; charset="utf8"
        MIME-Version: 1.0
        Content-Transfer-Encoding: quoted-printable
        Content-Disposition: inline
        <BLANKLINE>
        A simple body with some non ascii t=C3=83=C2=A9xt with "a link":http://www.=
        example.com
        --...
        Content-Type: text/html; charset="utf8"
        MIME-Version: 1.0
        Content-Transfer-Encoding: quoted-printable
        Content-Disposition: inline
        <BLANKLINE>
        <p>A simple body with some non ascii t=C3=83=C2=A9xt with <a href=3D"http:/=
        /www.example.com">a link</a></p>
        <BLANKLINE>
        --...
    """
    if other_headers is None:
        other_headers = {}
    html = HTML(body, level=2, header=0)
    return construct_multipart(from_addr, to_addr, subject, body, html,
                               other_headers, encoding)