From 8bf6c2f216abb8873027af70e3d6d27d9f482164 Mon Sep 17 00:00:00 2001 From: AakashGC Date: Thu, 29 Oct 2020 10:46:01 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20BUG:=20numbered=20nested=20toctr?= =?UTF-8?q?ees=20were=20unordered=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * doc correction * README changes * ordering in nested toctree --- .gitignore | 1 + sphinx_multitoc_numbering/__init__.py | 8 ++- tests/roots/test-nested-toctree/Makefile | 20 +++++++ tests/roots/test-nested-toctree/conf.py | 51 +++++++++++++++++ tests/roots/test-nested-toctree/index.rst | 24 ++++++++ tests/roots/test-nested-toctree/make.bat | 35 ++++++++++++ .../test-nested-toctree/part1/chapter1.rst | 5 ++ .../test-nested-toctree/part1/chapter2.rst | 5 ++ .../roots/test-nested-toctree/part1/index.rst | 9 +++ .../test-nested-toctree/part2/chapter3.rst | 5 ++ .../roots/test-nested-toctree/part2/index.rst | 8 +++ .../test-nested-toctree/part3/chapter4.rst | 5 ++ .../roots/test-nested-toctree/part3/index.rst | 8 +++ .../test-nested-toctree/unumbered_chapter.rst | 5 ++ tests/test_build.py | 15 +++++ tests/test_build/test_nested.html | 55 +++++++++++++++++++ 16 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 tests/roots/test-nested-toctree/Makefile create mode 100644 tests/roots/test-nested-toctree/conf.py create mode 100644 tests/roots/test-nested-toctree/index.rst create mode 100644 tests/roots/test-nested-toctree/make.bat create mode 100644 tests/roots/test-nested-toctree/part1/chapter1.rst create mode 100644 tests/roots/test-nested-toctree/part1/chapter2.rst create mode 100644 tests/roots/test-nested-toctree/part1/index.rst create mode 100644 tests/roots/test-nested-toctree/part2/chapter3.rst create mode 100644 tests/roots/test-nested-toctree/part2/index.rst create mode 100644 tests/roots/test-nested-toctree/part3/chapter4.rst create mode 100644 tests/roots/test-nested-toctree/part3/index.rst create mode 100644 tests/roots/test-nested-toctree/unumbered_chapter.rst create mode 100644 tests/test_build/test_nested.html diff --git a/.gitignore b/.gitignore index d2b41c6..13f5e0f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ dist/ _build/ _static _templates +.DS_Store # Unit test / coverage reports diff --git a/sphinx_multitoc_numbering/__init__.py b/sphinx_multitoc_numbering/__init__.py index 55f4f82..67afd04 100644 --- a/sphinx_multitoc_numbering/__init__.py +++ b/sphinx_multitoc_numbering/__init__.py @@ -98,7 +98,13 @@ def _walk_toctree(toctreenode: addnodes.toctree, depth: int) -> None: if secnums != old_secnumbers.get(ref): rewrite_needed.append(ref) - for docname in env.numbered_toctrees: + # rearrange it to respect ordering in toctree directives + rearranged_numbered_toctrees = [] + for toc in env.tocs: + if toc in env.numbered_toctrees: + rearranged_numbered_toctrees.append(toc) + + for docname in rearranged_numbered_toctrees: assigned.add(docname) doctree = env.get_doctree(docname) for toctreenode in doctree.traverse(addnodes.toctree): diff --git a/tests/roots/test-nested-toctree/Makefile b/tests/roots/test-nested-toctree/Makefile new file mode 100644 index 0000000..d4bb2cb --- /dev/null +++ b/tests/roots/test-nested-toctree/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/tests/roots/test-nested-toctree/conf.py b/tests/roots/test-nested-toctree/conf.py new file mode 100644 index 0000000..165703e --- /dev/null +++ b/tests/roots/test-nested-toctree/conf.py @@ -0,0 +1,51 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) + + +# -- Project information ----------------------------------------------------- + +project = "sphinx-multitoc-numbering" +copyright = "2020, Cristian G Guerrero" +author = "Cristian G Guerrero" + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = ["sphinx_multitoc_numbering"] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ["_templates"] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = "alabaster" + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ["_static"] diff --git a/tests/roots/test-nested-toctree/index.rst b/tests/roots/test-nested-toctree/index.rst new file mode 100644 index 0000000..2a9af3a --- /dev/null +++ b/tests/roots/test-nested-toctree/index.rst @@ -0,0 +1,24 @@ +.. sphinx-multitoc-numbering / Issue #14 Reproducer documentation master file, created by + sphinx-quickstart on Wed Oct 21 12:22:53 2020. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to sphinx-multitoc-numbering / Issue #14 Reproducer's documentation! +============================================================================ + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + unumbered_chapter + part1/index + part2/index + part3/index + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/tests/roots/test-nested-toctree/make.bat b/tests/roots/test-nested-toctree/make.bat new file mode 100644 index 0000000..2119f51 --- /dev/null +++ b/tests/roots/test-nested-toctree/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=_build + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/tests/roots/test-nested-toctree/part1/chapter1.rst b/tests/roots/test-nested-toctree/part1/chapter1.rst new file mode 100644 index 0000000..78e3af7 --- /dev/null +++ b/tests/roots/test-nested-toctree/part1/chapter1.rst @@ -0,0 +1,5 @@ +********* +Chapter 1 +********* + +The contents diff --git a/tests/roots/test-nested-toctree/part1/chapter2.rst b/tests/roots/test-nested-toctree/part1/chapter2.rst new file mode 100644 index 0000000..8b0d26e --- /dev/null +++ b/tests/roots/test-nested-toctree/part1/chapter2.rst @@ -0,0 +1,5 @@ +********* +Chapter 2 +********* + +The contents diff --git a/tests/roots/test-nested-toctree/part1/index.rst b/tests/roots/test-nested-toctree/part1/index.rst new file mode 100644 index 0000000..32c7ba2 --- /dev/null +++ b/tests/roots/test-nested-toctree/part1/index.rst @@ -0,0 +1,9 @@ +###### +Part 1 +###### + +.. toctree:: + :numbered: + + chapter1 + chapter2 diff --git a/tests/roots/test-nested-toctree/part2/chapter3.rst b/tests/roots/test-nested-toctree/part2/chapter3.rst new file mode 100644 index 0000000..edc8f4f --- /dev/null +++ b/tests/roots/test-nested-toctree/part2/chapter3.rst @@ -0,0 +1,5 @@ +********* +Chapter 3 +********* + +The contents diff --git a/tests/roots/test-nested-toctree/part2/index.rst b/tests/roots/test-nested-toctree/part2/index.rst new file mode 100644 index 0000000..b47cdeb --- /dev/null +++ b/tests/roots/test-nested-toctree/part2/index.rst @@ -0,0 +1,8 @@ +###### +Part 2 +###### + +.. toctree:: + :numbered: + + chapter3 diff --git a/tests/roots/test-nested-toctree/part3/chapter4.rst b/tests/roots/test-nested-toctree/part3/chapter4.rst new file mode 100644 index 0000000..f298ea1 --- /dev/null +++ b/tests/roots/test-nested-toctree/part3/chapter4.rst @@ -0,0 +1,5 @@ +********* +Chapter 4 +********* + +The contents diff --git a/tests/roots/test-nested-toctree/part3/index.rst b/tests/roots/test-nested-toctree/part3/index.rst new file mode 100644 index 0000000..661e1af --- /dev/null +++ b/tests/roots/test-nested-toctree/part3/index.rst @@ -0,0 +1,8 @@ +###### +Part 3 +###### + +.. toctree:: + :numbered: + + chapter4 diff --git a/tests/roots/test-nested-toctree/unumbered_chapter.rst b/tests/roots/test-nested-toctree/unumbered_chapter.rst new file mode 100644 index 0000000..f480df9 --- /dev/null +++ b/tests/roots/test-nested-toctree/unumbered_chapter.rst @@ -0,0 +1,5 @@ +***************** +Unumbered Chapter +***************** + +The contents diff --git a/tests/test_build.py b/tests/test_build.py index dff9fa6..6a64b22 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -44,3 +44,18 @@ def test_mixed(app, file_regression): assert "2. Chapter 2" in str(toctree_wrapper) assert ">Part 2" in str(toctree_wrapper) assert ">Chapter 3" in str(toctree_wrapper) + + +@pytest.mark.sphinx("html", testroot="nested-toctree") +def test_nested(app, file_regression): + app.build() + outfile = app.outdir / "index.html" + + # get content markup + soup = bs(outfile.read_text(encoding="utf8"), "html.parser") + toctree_wrapper = soup.findAll("div", {"class": "toctree-wrapper"})[0] + file_regression.check(toctree_wrapper.prettify(), extension=".html") + assert "1. Chapter 1" in str(toctree_wrapper) + assert "2. Chapter 2" in str(toctree_wrapper) + assert "3. Chapter 3" in str(toctree_wrapper) + assert "4. Chapter 4" in str(toctree_wrapper) diff --git a/tests/test_build/test_nested.html b/tests/test_build/test_nested.html new file mode 100644 index 0000000..de74616 --- /dev/null +++ b/tests/test_build/test_nested.html @@ -0,0 +1,55 @@ +
+

+ + Contents: + +

+ +