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 -%} 'numpy': ('https://numpy.org/doc/stable/', None),{% endif %}
96+ {% if 'scipy' in dependencies -%}
97+ 'scipy': ('https://docs.scipy.org/doc/scipy/', None),{% endif %}
98+ {% if 'matplotlib' in dependencies -%}
99+ 'matplotlib': ('https://matplotlib.org/stable/', None),{% endif %}
100+ {% if 'pyfar' in dependencies -%}
101+ 'pyfar': ('https://pyfar.readthedocs.io/en/stable/', None),{% endif %}
102+ }
103+
104+ # -- Options for HTML output -------------------------------------------------
105+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
106+
107+ html_theme = 'pydata_sphinx_theme'
108+ html_static_path = ['_static']
109+ html_css_files = ['css/custom.css']
110+ html_js_files = ['js/custom.js']
111+ html_logo = '{{ logo_path_gallery }}'
112+ html_title = "{{ project_slug }}"
113+ html_favicon = '_static/favicon.ico'
114+
115+ # -- HTML theme options
116+ # https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/layout.html
117+ html_sidebars = {
118+ "{{ project_slug }}": []
119+ }
120+
121+ html_theme_options = {
122+ "navbar_start": ["navbar-logo"],
123+ "navbar_end": ["navbar-icon-links", "theme-switcher"],
124+ "navbar_align": "content",
125+ "header_links_before_dropdown": None, # will be automatically set later based on headers.rst
126+ "header_dropdown_text": "Packages", # Change dropdown name from "More" to "Packages"
127+ "icon_links": [
128+ {
129+ "name": "GitHub",
130+ "url": "https://github.com/pyfar",
131+ "icon": "fa-brands fa-square-github",
132+ "type": "fontawesome",
133+ },
134+ ],
135+ # Configure secondary (right) side bar
136+ "show_toc_level": 3, # Show all subsections of notebooks
137+ "secondary_sidebar_items": ["page-toc"], # Omit 'show source' link that that shows notebook in json format
138+ "navigation_with_keys": True,
139+ # Configure navigation depth for section navigation
140+ "navigation_depth": 2,
141+ }
142+
143+ html_context = {
144+ "default_mode": "light"
145+ }
146+
147+ # redirect index to pyfar.html
148+ redirects = {
149+ "index": "{{ project_slug }}.html"
150+ }
151+
152+ # -- download navbar and style files from gallery -----------------------------
153+ branch = 'main'
154+ link = f'https://github.com/pyfar/gallery/raw/{branch}/docs/'
155+ folders_in = [
156+ '_static/css/custom.css',
157+ '_static/js/custom.js',
158+ '_static/favicon.ico',
159+ '_static/header.rst',
160+ '{{ logo_path_gallery }}',
161+ ]
162+
163+ def download_files_from_gallery(link, folders_in):
164+ c = urllib3.PoolManager()
165+ for file in folders_in:
166+ url = link + file
167+ filename = file
168+ os.makedirs(os.path.dirname(filename), exist_ok=True)
169+ with c.request('GET', url, preload_content=False) as res:
170+ if res.status == 200:
171+ with open(filename, 'wb') as out_file:
172+ shutil.copyfileobj(res, out_file)
173+
174+ download_files_from_gallery(link, folders_in)
175+ # if logo does not exist, use pyfar logo
176+ if not os.path.exists(html_logo):
177+ download_files_from_gallery(
178+ link, ['resources/logos/pyfar_logos_fixed_size_pyfar.png'])
179+ shutil.copyfile(
180+ 'resources/logos/pyfar_logos_fixed_size_pyfar.png', html_logo)
181+
182+
183+ # -- modify downloaded header file from the gallery to ----------------------
184+ # -- aline with the local toctree ---------------------------------------------
185+
186+ # read the header file from the gallery
187+ with open("_static/header.rst", "rt") as fin:
188+ lines = [line for line in fin]
189+
190+ # replace readthedocs link with internal link to this documentation
191+ lines_mod = [
192+ line.replace(f'https://{project}.readthedocs.io', project) for line in lines]
193+
194+ # if not found, add this documentation link to the end of the list, so that
195+ # it is in the doc tree
196+ contains_project = any(project in line for line in lines_mod)
197+ if not contains_project:
198+ lines_mod.append(f' {project} <{project}>\n')
199+
200+ # write the modified header file
201+ # to the doc\header.rst folder, so that it can be used in the documentation
202+ with open("header.rst", "wt") as fout:
203+ fout.writelines(lines_mod)
204+
205+
206+ # -- find position of pyfar package in toctree --------------------------------
207+ # -- this is required to define the dropdown of Packages in the header --------
208+
209+ # find line where pyfar package is mentioned, to determine the start of
210+ # the packages list in the header
211+ n_line_pyfar = 0
212+ for i, line in enumerate(lines):
213+ if 'https://pyfar.readthedocs.io' in line:
214+ n_line_pyfar = i
215+ break
216+
217+ # the first 4 lines of the header file are defining the settings and a empty
218+ # line of the toctree, therefore we need to subtract 4 from the line number
219+ # of the pyfar package to get the correct position in the toctree
220+ n_toctree_pyfar = n_line_pyfar - 4
221+
222+ if n_toctree_pyfar < 1:
223+ raise ValueError(
224+ "Could not find the line where pyfar package is mentioned. "
225+ "Please check the header.rst file in the gallery."
226+ )
227+
228+ # set dropdown header at pyfar appearance, so that pyfar is the first item in
229+ # the dropdown called Packages
230+ html_theme_options['header_links_before_dropdown'] = n_toctree_pyfar
0 commit comments