diff --git a/sphinx_proof/directive.py b/sphinx_proof/directive.py index f6dd012..e1eab78 100644 --- a/sphinx_proof/directive.py +++ b/sphinx_proof/directive.py @@ -75,8 +75,11 @@ def run(self) -> List[Node]: node_id = f"{label}" else: self.options["noindex"] = True - label = f"{realtyp}-{serial_no}" - node_id = f"{realtyp}-{serial_no}" + # new_serialno() is unique per document only; include docname + # so unlabeled directives do not collide across files. + doc_id = env.docname.replace("/", "-") + label = f"{doc_id}-{realtyp}-{serial_no}" + node_id = label ids = [node_id] # Duplicate label warning diff --git a/tests/books/test-unlabeled-multidoc/bar.rst b/tests/books/test-unlabeled-multidoc/bar.rst new file mode 100644 index 0000000..53f93f1 --- /dev/null +++ b/tests/books/test-unlabeled-multidoc/bar.rst @@ -0,0 +1,6 @@ +bar +=== + +.. prf:example:: + + An unlabeled example in bar. diff --git a/tests/books/test-unlabeled-multidoc/conf.py b/tests/books/test-unlabeled-multidoc/conf.py new file mode 100644 index 0000000..7edac4d --- /dev/null +++ b/tests/books/test-unlabeled-multidoc/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-proof Test" +copyright = "2020, QuantEcon Developers" +author = "QuantEcon Developers" + + +# -- 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_proof", "myst_parser"] + +# 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 = [] + + +# -- 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/books/test-unlabeled-multidoc/foo.rst b/tests/books/test-unlabeled-multidoc/foo.rst new file mode 100644 index 0000000..cd53cdb --- /dev/null +++ b/tests/books/test-unlabeled-multidoc/foo.rst @@ -0,0 +1,6 @@ +foo +=== + +.. prf:example:: + + An unlabeled example in foo. diff --git a/tests/books/test-unlabeled-multidoc/index.rst b/tests/books/test-unlabeled-multidoc/index.rst new file mode 100644 index 0000000..574885d --- /dev/null +++ b/tests/books/test-unlabeled-multidoc/index.rst @@ -0,0 +1,8 @@ +index +===== + +.. toctree:: + :maxdepth: 1 + + foo + bar diff --git a/tests/test_html.py b/tests/test_html.py index 01a5521..07f76a4 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -53,6 +53,20 @@ def test_duplicate_label(app, warnings): assert "WARNING: duplicate algorithm label" in warnings(app) +@pytest.mark.sphinx("html", testroot="unlabeled-multidoc") +def test_unlabeled_examples_across_documents(app, warnings): + """Unlabeled examples in different files must not share a label.""" + app.build() + examples = { + label: info + for label, info in app.env.proof_list.items() + if info["realtype"] == "example" + } + assert len(examples) == 2 + assert {info["docname"] for info in examples.values()} == {"foo", "bar"} + assert "duplicate example label" not in warnings(app) + + # Tests for Proofs @pytest.mark.sphinx("html", testroot="mybook") @pytest.mark.parametrize( diff --git a/tests/test_html/algorithm/_algo_nonumber.html b/tests/test_html/algorithm/_algo_nonumber.html index 7fa6d0f..6977434 100644 --- a/tests/test_html/algorithm/_algo_nonumber.html +++ b/tests/test_html/algorithm/_algo_nonumber.html @@ -1,5 +1,5 @@ -
-

Algorithm (Test algorithm directive)

+
+

Algorithm (Test algorithm directive)

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

diff --git a/tests/test_latex/test_latex_build.tex b/tests/test_latex/test_latex_build.tex index 19f308a..b6fce2a 100644 --- a/tests/test_latex/test_latex_build.tex +++ b/tests/test_latex/test_latex_build.tex @@ -17,7 +17,7 @@ \chapter{content 2} -\label{\detokenize{algorithm/_algo_nonumber:content-2}}\label{\detokenize{algorithm/_algo_nonumber::doc}}\label{algorithm/_algo_nonumber:algorithm-0} +\label{\detokenize{algorithm/_algo_nonumber:content-2}}\label{\detokenize{algorithm/_algo_nonumber::doc}}\label{algorithm/_algo_nonumber:algorithm-_algo_nonumber-algorithm-0} \begin{sphinxadmonition}{note}{Algorithm (Test algorithm directive)} @@ -134,8 +134,8 @@ \chapter{content 5} \renewcommand{\indexname}{Proof Index} \begin{sphinxtheindex} \let\bigletter\sphinxstyleindexlettergroup -\bigletter{algorithm\sphinxhyphen{}0} -\item\relax\sphinxstyleindexentry{algorithm\sphinxhyphen{}0}\sphinxstyleindexextra{algorithm/\_algo\_nonumber}\sphinxstyleindexpageref{algorithm/_algo_nonumber:\detokenize{algorithm-0}} +\bigletter{algorithm\sphinxhyphen{}\_algo\_nonumber\sphinxhyphen{}algorithm\sphinxhyphen{}0} +\item\relax\sphinxstyleindexentry{algorithm\sphinxhyphen{}\_algo\_nonumber\sphinxhyphen{}algorithm\sphinxhyphen{}0}\sphinxstyleindexextra{algorithm/\_algo\_nonumber}\sphinxstyleindexpageref{algorithm/_algo_nonumber:\detokenize{algorithm-_algo_nonumber-algorithm-0}} \indexspace \bigletter{test\sphinxhyphen{}algo\sphinxhyphen{}label} \item\relax\sphinxstyleindexentry{test\sphinxhyphen{}algo\sphinxhyphen{}label}\sphinxstyleindexextra{algorithm/\_algo\_labeled\_titled\_with\_classname}\sphinxstyleindexpageref{algorithm/_algo_labeled_titled_with_classname:\detokenize{test-algo-label}}