Skip to content
Permalink
Browse files

add extension postcontents

  • Loading branch information...
sdpython committed Jul 19, 2017
1 parent c50b144 commit 141ce900e9a1b45df7f495723f48ae5db3f978bd
@@ -0,0 +1,148 @@
"""
@brief test log(time=4s)
@author Xavier Dupre
"""

import sys
import os
import unittest
import warnings
from docutils.parsers.rst import directives
from docutils import nodes

try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

from src.pyquickhelper.loghelper.flog import fLOG
from src.pyquickhelper.pycode import get_temp_folder
from src.pyquickhelper.helpgen import rst2html
from src.pyquickhelper.sphinxext import RunPythonDirective
from src.pyquickhelper.sphinxext import PostContentsDirective

if sys.version_info[0] == 2:
from codecs import open


class TestPostContentsExtension(unittest.TestCase):

content = """
Contents:
.. contents::
:local:
Title outside
+++++++++++++
.. runpython::
:rst:
print("some text")
print()
print("Title inside")
print("++++++++++++")
print()
print("Title inside2")
print("^^^^^^^^^^^^^")
print()
print("some new text")
""".replace(" ", "")

def test_post_parse(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

directives.register_directive("runpython", RunPythonDirective)
directives.register_directive("postcontents", PostContentsDirective)

def test_contents(self):
"""
this test also test the extension runpython
"""
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

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

content = TestPostContentsExtension.content

rst = rst2html(content, # fLOG=fLOG,
# layout="sphinx",
writer="rst", keep_warnings=True)

if "* :ref:`title-outside`" not in rst:
raise Exception(rst)
if "* :ref:`title-inside`" in rst:
raise Exception("\n" + rst)

def test_postcontents(self):
"""
this test also test the extension runpython
"""
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

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

content = TestPostContentsExtension.content.replace(
"contents", "postcontents")

# 1

rst = rst2html(content, # fLOG=fLOG,
# layout="sphinx",
writer="rst", keep_warnings=True)

if "* :ref:`title-outside`" in rst:
raise Exception("\n" + rst)
if "* :ref:`title-inside`" in rst:
raise Exception("\n" + rst)

# 2

rst = rst2html(content, # fLOG=fLOG,
layout="sphinx",
writer="html", keep_warnings=True)

temp = get_temp_folder(__file__, "temp_postcontents")
with open(os.path.join(temp, "postcontents.html"), "w", encoding="utf-8") as f:
f.write(rst)
if '<li><p><a class="reference internal" href="#title-inside2" title="Title inside2">Title inside2</a></p></li>' not in rst:
raise Exception("\n" + rst)

# 3

rst = rst2html(content, # fLOG=fLOG,
layout="sphinx",
writer="rst", keep_warnings=True)

if "* :ref:`title-outside`" not in rst:
raise Exception(rst)
if "* :ref:`title-inside`" not in rst:
raise Exception(rst)


if __name__ == "__main__":
unittest.main()
@@ -44,6 +44,7 @@
from ..sphinxext.blog_post_list import BlogPostList
from ..sphinxext.sphinx_blog_extension import BlogPostDirective, BlogPostDirectiveAgg
from ..sphinxext.sphinx_runpython_extension import RunPythonDirective
from ..sphinxext.sphinx_postcontents_extension import PostContentsDirective
from ..sphinxext.sphinx_sharenet_extension import ShareNetDirective, sharenet_role
from ..sphinxext.sphinx_template_extension import tpl_role
from ..sphinxext.sphinx_epkg_extension import epkg_role
@@ -319,6 +320,7 @@ def lay_build_override_newconf(t3):
directives.register_directive("faqref", FaqRef)
directives.register_directive("nbref", NbRef)
directives.register_directive("cmdref", CmdRef)
directives.register_directive("postcontents", PostContentsDirective)
roles.register_canonical_role("sharenet", sharenet_role)
roles.register_canonical_role("bigger", bigger_role)
roles.register_canonical_role("githublink", githublink_role)
@@ -49,6 +49,7 @@
from ..sphinxext.sphinx_bigger_extension import depart_bigger_node_html as ext_depart_bigger_node_html
from ..sphinxext.sphinx_blog_extension import depart_blogpostagg_node_html as ext_depart_blogpostagg_node_html
from ..sphinxext.sphinx_sharenet_extension import depart_sharenet_node_html as ext_depart_sharenet_node_html
from ..sphinxext.sphinx_postcontents_extension import depart_postcontents_node as ext_depart_postcontents_node, visit_postcontents_node as ext_visit_postcontents_node

with warnings.catch_warnings():
warnings.simplefilter("ignore")
@@ -293,6 +294,18 @@ def visit_pending_xref(self, node):
self.visit_Text(node)
raise nodes.SkipNode

def depart_postcontents_node(self, node):
"""
@see fn depart_postcontents_node
"""
ext_depart_postcontents_node(self, node)

def visit_postcontents_node(self, node):
"""
@see fn visit_postcontents_node
"""
ext_visit_postcontents_node(self, node)

def unknown_visit(self, node):
raise NotImplementedError("[HTMLTranslatorWithCustomDirectives] Unknown node: '{0}' in '{1}'".format(node.__class__.__name__,
self.__class__.__name__))
@@ -12,19 +12,20 @@
from .blog_post import BlogPost
from .blog_post_list import BlogPostList
from .sphinx_bigger_extension import bigger_node, bigger_role
from .sphinx_githublink_extension import githublink_node, githublink_role
from .sphinx_blocref_extension import BlocRef, BlocRefList
from .sphinx_blog_extension import BlogPostDirective, BlogPostDirectiveAgg
from .sphinx_cmdref_extension import CmdRef, CmdRefList
from .sphinx_epkg_extension import epkg_node
from .sphinx_exref_extension import ExRef, ExRefList
from .sphinx_faqref_extension import FaqRef, FaqRefList
from .sphinx_githublink_extension import githublink_node, githublink_role
from .sphinx_mathdef_extension import MathDef, MathDefList
from .sphinx_nbref_extension import NbRef, NbRefList
from .sphinx_cmdref_extension import CmdRef, CmdRefList
from .sphinx_postcontents_extension import PostContentsDirective, postcontents_node
from .sphinx_runpython_extension import RunPythonDirective, runpython_node
from .sphinx_sharenet_extension import ShareNetDirective, sharenet_node
from .sphinx_todoext_extension import TodoExt, TodoExtList
from .sphinx_template_extension import tpl_node
from .sphinx_epkg_extension import epkg_node
from .sphinx_todoext_extension import TodoExt, TodoExtList
from .documentation_link import python_link_doc

from sphinx.ext.autodoc import setup as setup_autodoc
@@ -36,23 +37,24 @@

from matplotlib.sphinxext.plot_directive import setup as setup_plot
from matplotlib.sphinxext.only_directives import setup as setup_only
from ..sphinxext.sphinx_autosignature import setup as setup_signature
from ..sphinxext.sphinx_bigger_extension import setup as setup_bigger
from ..sphinxext.sphinx_githublink_extension import setup as setup_githublink
from ..sphinxext.sphinx_blocref_extension import setup as setup_blocref
from ..sphinxext.sphinx_blog_extension import setup as setup_blog
from ..sphinxext.sphinx_cmdref_extension import setup as setup_cmdref
from ..sphinxext.sphinx_docassert_extension import setup as setup_docassert
from ..sphinxext.sphinx_epkg_extension import setup as setup_epkg
from ..sphinxext.sphinx_exref_extension import setup as setup_exref
from ..sphinxext.sphinx_faqref_extension import setup as setup_faqref
from ..sphinxext.sphinx_githublink_extension import setup as setup_githublink
from ..sphinxext.sphinx_mathdef_extension import setup as setup_mathdef
from ..sphinxext.sphinx_nbref_extension import setup as setup_nbref
from ..sphinxext.sphinx_postcontents_extension import setup as setup_postcontents
from ..sphinxext.sphinx_rst_builder import setup as setup_rst
from ..sphinxext.sphinx_runpython_extension import setup as setup_runpython
from ..sphinxext.sphinx_sharenet_extension import setup as setup_sharenet
from ..sphinxext.sphinx_todoext_extension import setup as setup_todoext
from ..sphinxext.sphinx_autosignature import setup as setup_signature
from ..sphinxext.sphinx_template_extension import setup as setup_tpl
from ..sphinxext.sphinx_cmdref_extension import setup as setup_cmdref
from ..sphinxext.sphinx_epkg_extension import setup as setup_epkg
from ..sphinxext.sphinx_rst_builder import setup as setup_rst
from ..sphinxext.sphinx_todoext_extension import setup as setup_todoext


def get_default_extensions():
@@ -76,7 +78,7 @@ def get_default_extensions():
setup_runpython, setup_mathdef, setup_blocref,
setup_faqref, setup_exref, setup_nbref,
setup_docassert, setup_signature, setup_tpl,
setup_cmdref, setup_epkg, setup_rst,
setup_cmdref, setup_epkg, setup_rst, setup_postcontents,
# directives from sphinx
setup_graphviz, setup_math, setup_todo,
# the rest of it
@@ -47,3 +47,63 @@ def info_blocref(app, doctree, fromdocname, class_name,
message = " ".join(rows)
app.info(message)
return True


class TinyNode:
"""
Returned by @see fn traverse.
"""

def __init__(self, parent):
"""
Create a note
@param parent parent node
"""
self.parent = parent


class NodeEnter(TinyNode):
"""
Returned by function @see fn traverse.
"""
pass


class NodeLeave(TinyNode):
"""
Returned by function @see fn traverse.
"""
pass


class WrappedNode:
"""
Wraps a docutils node.
"""

def __init__(self, node):
self.node = node


def traverse(node, depth=0):
"""
Enumerate through all children but insert a node whenever
digging or leaving the childrens nodes.
@param node node (from doctree)
@param depth current depth
@return enumerate (depth, node)
@see cl NodeEnter and @see cl NodeLeave are returned whenever entering or leaving nodes.
"""
if isinstance(node, WrappedNode):
node = node.node
ne = NodeEnter(node)
nl = NodeLeave(node)
yield (depth, ne)
yield (depth, node)
for n in node.children:
for r in traverse(n, depth + 1):
yield r
yield (depth, nl)
Oops, something went wrong.

0 comments on commit 141ce90

Please sign in to comment.
You can’t perform that action at this time.