1+ # Configuration file for the Sphinx documentation builder.
2+ #
3+ # For the full list of built-in configuration values, see the documentation:
4+ # https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+ # -- Project information -----------------------------------------------------
7+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
9+ import os
10+ import sys
11+ import urllib3
12+ import shutil
13+ import numpy as np
14+ sys.path.insert(0, os.path.abspath('..'))
15+
16+ import {{ project_slug }} # noqa
17+
18+ # -- General configuration ---------------------------------------------------
19+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
20+
21+ extensions = [
22+ 'sphinx.ext.autodoc',
23+ 'sphinx.ext.viewcode',
24+ 'sphinx.ext.napoleon',
25+ 'sphinx.ext.autosummary',
26+ 'matplotlib.sphinxext.plot_directive',
27+ 'sphinx.ext.mathjax',
28+ 'sphinx.ext.intersphinx',
29+ 'autodocsumm',
30+ 'sphinx_design',
31+ 'sphinx_favicon',
32+ 'sphinx_reredirects',
33+ 'sphinx_mdinclude',
34+ ]
35+
36+ # show tocs for classes and functions of modules using the autodocsumm
37+ # package
38+ autodoc_default_options = {'autosummary': True}
39+
40+ # show the code of plots that follows the command .. plot:: based on the
41+ # package matplotlib.sphinxext.plot_directive
42+ plot_include_source = True
43+
44+ # Add any paths that contain templates here, relative to this directory.
45+ templates_path = ['_templates']
46+
47+ # The suffix(es) of source filenames.
48+ # You can specify multiple suffix as a list of string:
49+ source_suffix = {
50+ '.rst': 'restructuredtext',
51+ '.md': 'markdown',
52+ }
53+
54+ # The master toctree document.
55+ master_doc = 'index'
56+
57+ # General information about the project.
58+ project = '{{ project_slug }}'
59+ copyright = "{{ copyright_statement }}"
60+ author = "{{ author }}"
61+
62+ # The version info for the project you're documenting, acts as replacement
63+ # for |version| and |release|, also used in various other places throughout
64+ # the built documents.
65+ #
66+ # The short X.Y version.
67+ version = {{ project_slug }}.__version__
68+ # The full version, including alpha/beta/rc tags.
69+ release = {{ project_slug }}.__version__
70+
71+ # This is also used if you do content translation via gettext catalogs.
72+ # Usually you set "language" from the command line for these cases.
73+ language = 'en'
74+
75+ # List of patterns, relative to source directory, that match files and
76+ # directories to ignore when looking for source files.
77+ # This patterns also effect to html_static_path and html_extra_path
78+ exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
79+
80+ # The name of the Pygments (syntax highlighting) style to use (Not defining
81+ # uses the default style of the html_theme).
82+ # pygments_style = 'sphinx'
83+
84+ # If true, '()' will be appended to :func: etc. cross-reference text.
85+ add_function_parentheses = False
86+
87+ # If true, `todo` and `todoList` produce output, else they produce nothing.
88+ todo_include_todos = False
89+
90+ # default language for highlighting in source code
91+ highlight_language = "python3"
92+
93+ # intersphinx mapping
94+ intersphinx_mapping = {
95+ {% if 'numpy' in dependencies -%}
96+ 'numpy': ('https://numpy.org/doc/stable/', None),{% endif %}
97+ {% if 'scipy' in dependencies -%}
98+ 'scipy': ('https://docs.scipy.org/doc/scipy/', None),{% endif %}
99+ {% if 'matplotlib' in dependencies -%}
100+ 'matplotlib': ('https://matplotlib.org/stable/', None),{% endif %}
101+ {% if 'pyfar' in dependencies -%}
102+ 'pyfar': ('https://pyfar.readthedocs.io/en/stable/', None),{% endif %}
103+ }
104+
105+ # -- Options for HTML output -------------------------------------------------
106+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
107+
108+ html_theme = 'pydata_sphinx_theme'
109+ html_static_path = ['_static']
110+ html_css_files = ['css/custom.css']
111+ html_js_files = ['js/custom.js']
112+ html_logo = '{{ logo_path_gallery }}'
113+ html_title = "{{ project_slug }}"
114+ html_favicon = '_static/favicon.ico'
115+
116+ # -- HTML theme options
117+ # https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/layout.html
118+ html_sidebars = {
119+ "{{ project_slug }}": []
120+ }
121+
122+ html_theme_options = {
123+ "navbar_start": ["navbar-logo"],
124+ "navbar_end": ["navbar-icon-links", "theme-switcher"],
125+ "navbar_align": "content",
126+ "header_links_before_dropdown": None, # will be automatically set later based on headers.rst
127+ "header_dropdown_text": "Packages", # Change dropdown name from "More" to "Packages"
128+ "icon_links": [
129+ {
130+ "name": "GitHub",
131+ "url": "https://github.com/pyfar",
132+ "icon": "fa-brands fa-square-github",
133+ "type": "fontawesome",
134+ },
135+ ],
136+ # Configure secondary (right) side bar
137+ "show_toc_level": 3, # Show all subsections of notebooks
138+ "secondary_sidebar_items": ["page-toc"], # Omit 'show source' link that that shows notebook in json format
139+ "navigation_with_keys": True,
140+ # Configure navigation depth for section navigation
141+ "navigation_depth": 2,
142+ }
143+
144+ html_context = {
145+ "default_mode": "light"
146+ }
147+
148+ # redirect index to pyfar.html
149+ redirects = {
150+ "index": "{{ project_slug }}.html"
151+ }
152+
153+ # -- download navbar and style files from gallery -----------------------------
154+ branch = 'main'
155+ link = f'https://github.com/pyfar/gallery/raw/{branch}/docs/'
156+ folders_in = [
157+ '_static/css/custom.css',
158+ '_static/js/custom.js',
159+ '_static/favicon.ico',
160+ '_static/header.rst',
161+ '{{ logo_path_gallery }}',
162+ ]
163+
164+ def download_files_from_gallery(link, folders_in):
165+ c = urllib3.PoolManager()
166+ for file in folders_in:
167+ url = link + file
168+ filename = file
169+ os.makedirs(os.path.dirname(filename), exist_ok=True)
170+ with c.request('GET', url, preload_content=False) as res:
171+ if res.status == 200:
172+ with open(filename, 'wb') as out_file:
173+ shutil.copyfileobj(res, out_file)
174+
175+ download_files_from_gallery(link, folders_in)
176+ # if logo does not exist, use pyfar logo
177+ if not os.path.exists(html_logo):
178+ download_files_from_gallery(
179+ link, ['resources/logos/pyfar_logos_fixed_size_pyfar.png'])
180+ shutil.copyfile(
181+ 'resources/logos/pyfar_logos_fixed_size_pyfar.png', html_logo)
182+
183+
184+ # -- modify downloaded header file from the gallery to ----------------------
185+ # -- aline with the local toctree ---------------------------------------------
186+
187+ # read the header file from the gallery
188+ with open("_static/header.rst", "rt") as fin:
189+ lines = [line for line in fin]
190+
191+ # replace readthedocs link with internal link to this documentation
192+ lines_mod = [
193+ line.replace(f'https://{project}.readthedocs.io', project) for line in lines]
194+
195+ # if not found, add this documentation link to the end of the list, so that
196+ # it is in the doc tree
197+ contains_project = any(project in line for line in lines_mod)
198+ if not contains_project:
199+ lines_mod.append(f' {project} <{project}>\n')
200+
201+ # write the modified header file
202+ # to the doc\header.rst folder, so that it can be used in the documentation
203+ with open("header.rst", "wt") as fout:
204+ fout.writelines(lines_mod)
205+
206+
207+ # -- find position of pyfar package in toctree --------------------------------
208+ # -- this is required to define the dropdown of Packages in the header --------
209+
210+ # find line where pyfar package is mentioned, to determine the start of
211+ # the packages list in the header
212+ n_line_pyfar = 0
213+ for i, line in enumerate(lines):
214+ if 'https://pyfar.readthedocs.io' in line:
215+ n_line_pyfar = i
216+ break
217+
218+ # the first 4 lines of the header file are defining the settings and a empty
219+ # line of the toctree, therefore we need to subtract 4 from the line number
220+ # of the pyfar package to get the correct position in the toctree
221+ n_toctree_pyfar = n_line_pyfar - 4
222+
223+ if n_toctree_pyfar < 1:
224+ raise ValueError(
225+ "Could not find the line where pyfar package is mentioned. "
226+ "Please check the header.rst file in the gallery."
227+ )
228+
229+ # set dropdown header at pyfar appearance, so that pyfar is the first item in
230+ # the dropdown called Packages
231+ html_theme_options['header_links_before_dropdown'] = n_toctree_pyfar
0 commit comments