From b0a5a1339ecddf46dac96e594309eb5fa85ebe11 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 6 Jan 2016 01:34:29 +0900 Subject: [PATCH] Fix #771: latex output doesn't set tocdepth --- CHANGES | 2 +- sphinx/builders/latex.py | 9 +++++++++ sphinx/writers/latex.py | 9 +++++++++ tests/roots/test-tocdepth/index.rst | 1 + tests/test_build_latex.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 7d56602bc..6bd03e17e 100644 --- a/CHANGES +++ b/CHANGES @@ -30,7 +30,7 @@ Bugs fixed * #1894: Unlisted phony targets in quickstart Makefile * #2125: Fix unifies behavior of collapsed fields (``GroupedField`` and ``TypedField``) * #1408: Check latex_logo validity before copying - +* #771: Fix latex output doesn't set tocdepth Release 1.3.3 (released Dec 2, 2015) ==================================== diff --git a/sphinx/builders/latex.py b/sphinx/builders/latex.py index f98d2153e..85b71b578 100644 --- a/sphinx/builders/latex.py +++ b/sphinx/builders/latex.py @@ -95,6 +95,14 @@ class LaTeXBuilder(Builder): destination_path=path.join(self.outdir, targetname), encoding='utf-8') self.info("processing " + targetname + "... ", nonl=1) + toctrees = self.env.get_doctree(docname).traverse(addnodes.toctree) + if toctrees: + if toctrees[0].get('maxdepth'): + tocdepth = int(toctrees[0].get('maxdepth')) + else: + tocdepth = None + else: + tocdepth = None doctree = self.assemble_doctree( docname, toctree_only, appendices=((docclass != 'howto') and self.config.latex_appendices or [])) @@ -106,6 +114,7 @@ class LaTeXBuilder(Builder): doctree.settings.contentsname = self.get_contentsname(docname) doctree.settings.docname = docname doctree.settings.docclass = docclass + doctree.settings.tocdepth = tocdepth docwriter.write(doctree, destination) self.info("done") diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index a4b42e1e4..c381bf823 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -53,6 +53,7 @@ HEADER = r'''%% Generated by Sphinx. \author{%(author)s} \newcommand{\sphinxlogo}{%(logo)s} \renewcommand{\releasename}{%(releasename)s} +%(tocdepth)s %(makeindex)s ''' @@ -273,6 +274,7 @@ class LaTeXTranslator(nodes.NodeVisitor): 'printindex': '\\printindex', 'transition': '\n\n\\bigskip\\hrule{}\\bigskip\n\n', 'figure_align': 'htbp', + 'tocdepth': '', } # sphinx specific document classes @@ -357,6 +359,13 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.elements['extraclassoptions']: self.elements['classoptions'] += ',' + \ self.elements['extraclassoptions'] + if document.settings.tocdepth: + if document.settings.docclass == 'howto': + self.elements['tocdepth'] = ('\\setcounter{tocdepth}{%d}' % + document.settings.tocdepth) + else: + self.elements['tocdepth'] = ('\\setcounter{tocdepth}{%d}' % + (document.settings.tocdepth - 1)) self.highlighter = highlighting.PygmentsBridge( 'latex', diff --git a/tests/roots/test-tocdepth/index.rst b/tests/roots/test-tocdepth/index.rst index 0b651d483..a702cb88b 100644 --- a/tests/roots/test-tocdepth/index.rst +++ b/tests/roots/test-tocdepth/index.rst @@ -3,6 +3,7 @@ test-tocdepth .. toctree:: :numbered: + :maxdepth: 2 foo bar diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 9177ecb94..81f436c3a 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -428,3 +428,31 @@ def test_latex_logo_if_not_found(app, status, warning): assert False # SphinxError not raised except Exception as exc: assert isinstance(exc, SphinxError) + + +@with_app(buildername='latex', testroot='tocdepth', + confoverrides={'latex_documents': [ + ('index', 'SphinxTests.tex', 'Sphinx Tests Documentation', + 'Georg Brandl', 'manual'), + ]}) +def test_toctree_maxdepth_manual(app, status, warning): + app.builder.build_all() + result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') + print(result) + print(status.getvalue()) + print(warning.getvalue()) + assert '\\setcounter{tocdepth}{1}' in result + + +@with_app(buildername='latex', testroot='tocdepth', + confoverrides={'latex_documents': [ + ('index', 'SphinxTests.tex', 'Sphinx Tests Documentation', + 'Georg Brandl', 'howto'), + ]}) +def test_toctree_maxdepth_howto(app, status, warning): + app.builder.build_all() + result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') + print(result) + print(status.getvalue()) + print(warning.getvalue()) + assert '\\setcounter{tocdepth}{2}' in result