Esempio n. 1
0
    def test_newdirective_with_rst2html(self):
        """
        this test also test the extension runpython
        """
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_newdirective_with_rst2html not run on Python 2.7")
            return

        from docutils import nodes

        class runpythonthis_node(nodes.Structural, nodes.Element):
            pass

        class RunPythonThisDirective (RunPythonDirective):
            runpython_class = runpythonthis_node

        def visit_rp_node(self, node):
            self.body.append("<p><b>visit_rp_node</b></p>")

        def depart_rp_node(self, node):
            self.body.append("<p><b>depart_rp_node</b></p>")

        content = """
                    test a directive
                    ================

                    .. runpythonthis::

                        print(u"this code shoud appear" + u"___")
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node,
                  visit_rp_node, depart_rp_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives)

        ta = "<p><b>visit_rp_node</b></p>"
        if ta not in html:
            raise Exception(html)
        tb = "<p><b>depart_rp_node</b></p>"
        if tb not in html:
            raise Exception(html)
        t1 = "this code shoud appear___".split()
        for t in t1:
            if t not in html:
                temp = get_temp_folder(
                    __file__, "temp_newdirective_with_rst2html")
                with open(os.path.join(temp, "bug.html"), "w", encoding="utf8") as f:
                    f.write(html)
                raise Exception(html)
Esempio n. 2
0
    def test_parse_readme_cb(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        fold = os.path.dirname(os.path.abspath(__file__))
        readme = os.path.join(fold, "data", "README.rst")
        fLOG(readme)
        assert os.path.exists(readme)
        with open(readme, "r", encoding="utf8") as f:
            content = f.read()
        r = parse_markdown(content)
        if sys.version_info[0] == 2:
            return
        if "<p>.. _l-README:</p>" not in str(r):
            m = [ord(c) for c in content]
            m = ",".join(str(_) for _ in m[:20])
            raise Exception("IN\n{0}\nOUT:{1}".format(m, str(r)))

        ht = rst2html(content)
        # fLOG(ht)
        assert len(ht) > 0

        spl = content.split("\n")
        r = list(yield_sphinx_only_markup_for_pipy(spl))
        assert len(r) == len(spl)
Esempio n. 3
0
    def test_rst2html_png_bug(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if is_travis_or_appveyor():
            # it requires latex
            return

        if sys.version_info[:2] <= (2, 7):
            # i don't want to fix it for Python 2.7
            return

        temp = get_temp_folder(__file__, "temp_rst2html_png_latex")
        rst = os.path.join(os.path.abspath(
            os.path.dirname(__file__)), "data", "puzzle_girafe.rst")
        with open(rst, "r", encoding="utf-8") as f:
            content = f.read()
        text = rst2html(content, fLOG=fLOG, outdir=temp,
                        imgmath_latex_preamble="""
                    \\newcommand{\\acc}[1]{\\left\\{#1\\right\\}}
                    \\newcommand{\\cro}[1]{\\left[#1\\right]}
                    \\newcommand{\\pa}[1]{\\left(#1\\right)}
                    \\newcommand{\\girafedec}[3]{ \\begin{array}{ccccc} #1 &=& #2 &+& #3 \\\\ a' &=& a &-& o  \\end{array}}
                    \\newcommand{\\vecteur}[2]{\\pa{#1,\\dots,#2}}
                    \\newcommand{\\R}[0]{\\mathbb{R}}
                    \\newcommand{\\N}[0]{\\mathbb{N}}
                    """)
        # fLOG(text)
        ji = os.path.join(temp, "out.html")
        with open(ji, "w", encoding="utf-8") as f:
            f.write(text)
Esempio n. 4
0
    def test_bigger(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_biffer not run on Python 2.7")
            return

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    :bigger:`facebook`

                    after

                    this code shoud appear
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=None)

        t1 = "this code shoud not appear"
        if t1 in html:
            raise Exception(html)

        t1 = "this code shoud appear"
        if t1 not in html:
            raise Exception(html)

        t1 = "facebook"
        if t1 not in html:
            raise Exception(html)

        t1 = "linkedin"
        if t1 in html:
            raise Exception(html)

        t1 = '{1}'
        if t1 in html:
            raise Exception(html)

        t1 = "visit_sharenet_node"
        if t1 in html:
            raise Exception(html)

        temp = get_temp_folder(__file__, "temp_bigger")
        with open(os.path.join(temp, "out_bigger.html"), "w", encoding="utf8") as f:
            f.write(html)
Esempio n. 5
0
    def test_todoext_done(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    .. todoext::
                        :title: first todo
                        :tag: bug
                        :issue: 7
                        :hidden:

                        this code shoud appear___

                    after
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        tives = [("todoext", TodoExt, todoext_node,
                  visit_todoext_node, depart_todoext_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives, extlinks={'issue': ('http://%s', '_issue_')})

        temp = get_temp_folder(__file__, "temp_todoext")
        with open(os.path.join(temp, "out_todoext.html"), "w", encoding="utf8") as f:
            f.write(html)

        t1 = "this code shoud appear"
        if t1 in html:
            raise Exception(html)

        t1 = "after"
        if t1 not in html:
            raise Exception(html)

        t1 = "first todo"
        if t1 in html:
            raise Exception(html)

        t1 = "(bug)"
        if t1 in html:
            raise Exception(html)

        t1 = 'href="http://7"'
        if t1 in html:
            raise Exception(html)
Esempio n. 6
0
    def test_exreflist(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    .. exref::
                        :title: first todo
                        :tag: freg
                        :lid: id3

                        this code shoud appear___

                    middle

                    .. exreflist::
                        :tag: freg
                        :sort: title

                    after
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        tives = [("exref", ExRef, exref_node,
                  visit_exref_node, depart_exref_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives)
        if "admonition-exref exref_node admonition" not in html:
            raise html

        temp = get_temp_folder(__file__, "temp_exreflist")
        with open(os.path.join(temp, "out_exref.html"), "w", encoding="utf8") as f:
            f.write(html)

        t1 = "this code shoud appear"
        if t1 not in html:
            raise Exception(html)

        t1 = "after"
        if t1 not in html:
            raise Exception(html)

        t1 = "first todo"
        if t1 not in html:
            raise Exception(html)
Esempio n. 7
0
    def test_sharenet_inline(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_sharenet not run on Python 2.7")
            return

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    abeforea :sharenet:`facebook-linkedin-twitter-20-body` aaftera
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=None)

        t1 = "abeforea"
        if t1 not in html:
            raise Exception(html)

        t1 = "aftera"
        if t1 not in html:
            raise Exception(html)

        t1 = "function share_icon(divid, text)"
        if t1 not in html:
            raise Exception(html)

        t1 = '<a href="#"'
        if t1 not in html:
            raise Exception(html)

        t1 = '{1}'
        if t1 in html:
            raise Exception(html)

        t1 = "visit_sharenet_node"
        if t1 in html:
            raise Exception(html)

        temp = get_temp_folder(__file__, "temp_sharenet_inline")
        with open(os.path.join(temp, "out_sharenet.html"), "w", encoding="utf8") as f:
            f.write(html)
Esempio n. 8
0
    def test_mathdeflist_contents(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    .. mathdef::
                        :title: first def2
                        :tag: Theoreme

                        this code shoud appear___

                    middle

                    .. mathdeflist::
                        :tag: definition
                        :contents:

                    after
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        tives = [("mathdef", MathDef, mathdef_node,
                  visit_mathdef_node, depart_mathdef_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives)

        temp = get_temp_folder(__file__, "temp_mathdeflist_contents")
        with open(os.path.join(temp, "out_mathdef_contents.html"), "w", encoding="utf8") as f:
            f.write(html)

        t1 = "this code shoud appear"
        if t1 not in html:
            raise Exception(html)

        t1 = "after"
        if t1 not in html:
            raise Exception(html)

        t1 = "first def2"
        if t1 not in html:
            raise Exception(html)
Esempio n. 9
0
    def test_rst2html_svg(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            return
        temp = get_temp_folder(__file__, "temp_rst2html_svg")
        rst = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "hermionne.rst")
        with open(rst, "r", encoding="utf-8") as f:
            content = f.read()
        text = rst2html(content, fLOG=fLOG, outdir=temp, imgmath_image_format="svg")
        # fLOG(text)
        ji = os.path.join(temp, "out.html")
        with open(ji, "w", encoding="utf-8") as f:
            f.write(text)
Esempio n. 10
0
    def test_rst2html_plot(self):
        fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__")

        # disabled because it does not handle inline graph
        # or I did not found
        return

        temp = get_temp_folder(__file__, "temp_rst2html_plot")
        rst = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "rstplot.rst")
        with open(rst, "r", encoding="utf-8") as f:
            content = f.read()
        text = rst2html(content, fLOG=fLOG, outdir=temp)
        # fLOG(text)
        ji = os.path.join(temp, "out.html")
        with open(ji, "w", encoding="utf-8") as f:
            f.write(text)
Esempio n. 11
0
    def test_newdirective_with_rst2html_bug(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        from docutils import nodes

        class runpythonthis_node(nodes.Structural, nodes.Element):
            pass

        class RunPythonThisDirective (RunPythonDirective):
            runpython_class = runpythonthis_node

        def visit_node(self, node):
            self.body.append("<p><b>visit_node</b></p>")

        def depart_node(self, node):
            self.body.append("<p><b>depart_node</b></p>")

        content = """

                        description
                        -----------

                        .. deprecated:: 0.3
                            to add

                        this code shoud appear___

                    """.replace("                        ", "")

        tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node,
                  visit_node, depart_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives)

        t1 = "this code shoud appear___"
        assert t1 in html
        ta = "Deprecated since version 0.3"
        if ta not in html:
            raise Exception(html)
Esempio n. 12
0
    def test_directive_with_rst2html(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        path = os.path.abspath(os.path.split(__file__)[0])
        file = os.path.join(path, "data", "2015-04-04_first_blogpost.rst")
        with open(file, "r", encoding="utf8") as f:
            content = f.read()

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True)

        t1 = "<p>Text before the blog post.</p>"
        t2 = "<p>Text after the blog post.</p>"
        assert t1 in html
        assert t2 in html
        if "it was difficult" not in html:
            p1 = html.find(t1) + len(t1)
            p2 = html.find(t2)
            fLOG("--------------ERRORS\n", html[p1:p2], "------------")
Esempio n. 13
0
    def test_runpython(self):
        """
        this test also test the extension runpython
        """
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_runpython not run on Python 2.7")
            return

        from docutils import nodes

        class runpythonthis_node(nodes.Structural, nodes.Element):
            pass

        class RunPythonThisDirective (RunPythonDirective):
            runpython_class = runpythonthis_node

        def visit_rp_node(self, node):
            self.body.append("<p><b>visit_rp_node</b></p>")

        def depart_rp_node(self, node):
            self.body.append("<p><b>depart_rp_node</b></p>")

        if "enable_disabled_documented_pieces_of_code" in sys.__dict__:
            raise Exception("this case shoud not be")

        content = """
                    test a directive
                    ================

                    .. runpythonthis::
                        :setsysvar:
                        :rst:
                        :showcode:

                        print(u"this code shoud appear" + u"___")
                        import sys
                        print(u"setsysvar: " + str(sys.__dict__.get('enable_disabled_documented_pieces_of_code', None)))
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node,
                  visit_rp_node, depart_rp_node)]

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=tives)

        t1 = "this code shoud appear___"
        if t1 not in html:
            raise Exception(html)
        t2 = "setsysvar: True"
        if t2 not in html:
            raise Exception(html)
        t2 = "<p>In</p>"
        if t2 not in html:
            raise Exception(html)
        t2 = "<p>Out</p>"
        if t2 not in html:
            temp = get_temp_folder(__file__, "temp_runpython")
            with open(os.path.join(temp, "out.html"), "w", encoding="utf8") as f:
                f.write(html)
            raise Exception(html)
        if "enable_disabled_documented_pieces_of_code" in sys.__dict__:
            raise Exception("this case shoud not be")
Esempio n. 14
0
    def test_githublink(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_biffer not run on Python 2.7")
            return

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    :githublink:`j|py`

                    after

                    :githublink:`j`

                    again

                    :githublink:`j|py|84`

                    again

                    :githublink:`j|myfile.ipynb|83`

                    again

                    :githublink:`j|myfile.ipynb|*`

                    again

                    :githublink:`%|myfile.ipynb|*`

                    this code shoud appear
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=None,
                        githublink_options=dict(user="******", project="pyquickhelper", anchor="ANCHOR"))

        t1 = "this code shoud not appear"
        if t1 in html:
            raise Exception(html)

        t1 = "this code shoud appear"
        if t1 not in html:
            raise Exception(html)

        t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string.py#L7"
        if t1 not in html:
            raise Exception(html)

        t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string#L11"
        if t1 not in html:
            raise Exception(html)

        t1 = "https://github.com/sdpython/pyquickhelper/blob/master/src/string.py#L84"
        if t1 not in html:
            raise Exception(html)

        t1 = "https://github.com/sdpython/pyquickhelper/blob/master/myfile.ipynb#L83"
        if t1 not in html:
            raise Exception(html)

        t1 = "https://github.com/sdpython/pyquickhelper/blob/master/myfile.ipynb"
        if t1 not in html:
            raise Exception(html)

        t1 = 'href="https://github.com/sdpython/pyquickhelper/blob/master/myfile.ipynb">ANCHOR</a></p>'
        if t1 not in html:
            raise Exception(html)

        temp = get_temp_folder(__file__, "temp_githublink")
        with open(os.path.join(temp, "out_githublink.html"), "w", encoding="utf8") as f:
            f.write(html)
Esempio n. 15
0
    def test_githublink_function(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        if sys.version_info[0] == 2:
            warnings.warn(
                "test_biffer not run on Python 2.7")
            return

        from docutils import nodes as skip_

        content = """
                    test a directive
                    ================

                    before

                    :githublink:`j|py`

                    after

                    :githublink:`j`

                    again

                    :githublink:`j|py|84`

                    again

                    :githublink:`j|myfile.ipynb|83`

                    again

                    :githublink:`j|myfile.ipynb|*`

                    this code shoud appear
                    """.replace("                    ", "")
        if sys.version_info[0] >= 3:
            content = content.replace('u"', '"')

        def processor(path, lineno):
            return "[{0}:{1}]".format(path, lineno), "my_sources"

        html = rst2html(content, fLOG=fLOG,
                        writer="custom", keep_warnings=True,
                        directives=None,
                        githublink_options=dict(processor=processor))

        t1 = "this code shoud not appear"
        if t1 in html:
            raise Exception(html)

        t1 = "this code shoud appear"
        if t1 not in html:
            raise Exception(html)

        t1 = "[src/string.py:7]"
        if t1 not in html:
            raise Exception(html)

        t1 = "[src/string:11]"
        if t1 not in html:
            raise Exception(html)

        t1 = "[src/string.py:84]"
        if t1 not in html:
            raise Exception(html)

        t1 = "[myfile.ipynb:83]"
        if t1 not in html:
            raise Exception(html)

        t1 = "[myfile.ipynb:None]"
        if t1 not in html:
            raise Exception(html)

        temp = get_temp_folder(__file__, "temp_githublink")
        with open(os.path.join(temp, "out_githublink.html"), "w", encoding="utf8") as f:
            f.write(html)