Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AttributeError("'NoneType' object has no attribute 'get'") when including TOC #347

Merged
merged 8 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ def generate_toc_html(kind="html"):

# Add toc-hN + visible classes
def add_header_level_recursive(ul, level):
if ul is None:
return
if level <= (context["theme_show_toc_level"] + 1):
ul["class"] = ul.get("class", []) + ["visible"]
for li in ul("li", recursive=False):
li["class"] = li.get("class", []) + [f"toc-h{level}"]
ul = li.find("ul", recursive=False)
if ul:
add_header_level_recursive(ul, level + 1)
add_header_level_recursive(li.find("ul", recursive=False), level + 1)

add_header_level_recursive(soup.find("ul"), 1)

Expand Down
1 change: 1 addition & 0 deletions tests/sites/included_toc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_build
124 changes: 124 additions & 0 deletions tests/sites/included_toc/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- 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.

from sphinx.util import logging

logger = logging.getLogger(__name__)

# -- Project information -----------------------------------------------------

project = "Test"
copyright = "Test"
author = "Test"

release = version_long = version = "1.0.0"

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = []

html_extra_path = []

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = ".rst"

# The master toctree document.
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# 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 .
# TODO delete joe configuration when moved to nextgen
exclude_patterns = [
"_shared",
"**/_shared",
"_generated",
"**/_generated",
"_build",
"Thumbs.db",
".DS_Store",
"**/*.include.rst",
]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"


# -- 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'
# html_theme = 'sphinx_rtd_theme'
html_theme = "pydata_sphinx_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {
# 'sticky_navigation': True,
# 'navigation_depth': 3,
# 'collapse_navigation': False,
# 'logo_only': False,
# 'display_version': False,
# 'style_external_links': True
# }

html_theme_options = {"show_prev_next": False, "show_toc_level": 3}


# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}


# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = "Test"

# -- Extension configuration -------------------------------------------------

# -- Options for intersphinx extension ---------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"https://docs.python.org/": None}
3 changes: 3 additions & 0 deletions tests/sites/included_toc/included-page.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##########
Test page
##########
8 changes: 8 additions & 0 deletions tests/sites/included_toc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
########################
ABC
########################

Test Content

.. include:: toc-extension.include.rst

5 changes: 5 additions & 0 deletions tests/sites/included_toc/toc-extension.include.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. toctree::
:hidden:
:maxdepth: 2

included-page
16 changes: 16 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,19 @@ def test_sidebars_level2(sphinx_build_factory, file_regression):
# Sidebar structure
sidebar = subindex_html.select("nav#bd-docs-nav")[0]
file_regression.check(sidebar.prettify(), extension=".html")


def test_included_toc(sphinx_build_factory):
tomas-pajurek marked this conversation as resolved.
Show resolved Hide resolved
"""Test that Sphinx project containing TOC (.. toctree::) included
via .. include:: can be successfully built.
"""

sphinx_build = sphinx_build_factory("included_toc").build()

# Regression test for bug resolved in #347.
# Tests mainly makes sure that the sphinx_build.build() does not raise exception.

sphinx_build.build()

included_page_html = sphinx_build.html_tree("included-page.html")
assert included_page_html is not None