|
10 | 10 | import sys |
11 | 11 | import urllib3 |
12 | 12 | import shutil |
| 13 | +import numpy as np |
13 | 14 | sys.path.insert(0, os.path.abspath('..')) |
14 | 15 |
|
15 | 16 | import sofar |
|
117 | 118 | "navbar_start": ["navbar-logo"], |
118 | 119 | "navbar_end": ["navbar-icon-links", "theme-switcher"], |
119 | 120 | "navbar_align": "content", |
120 | | - "header_links_before_dropdown": 8, |
| 121 | + "header_links_before_dropdown": None, # will be automatically set later based on headers.rst |
| 122 | + "header_dropdown_text": "Packages", # Change dropdown name from "More" to "Packages" |
121 | 123 | "icon_links": [ |
122 | 124 | { |
123 | 125 | "name": "GitHub", |
|
152 | 154 | '_static/header.rst', |
153 | 155 | 'resources/logos/pyfar_logos_fixed_size_sofar.png', |
154 | 156 | ] |
155 | | -c = urllib3.PoolManager() |
156 | | -for file in folders_in: |
157 | | - url = link + file |
158 | | - filename = file |
159 | | - os.makedirs(os.path.dirname(filename), exist_ok=True) |
160 | | - with c.request('GET', url, preload_content=False) as res, open(filename, 'wb') as out_file: |
161 | | - shutil.copyfileobj(res, out_file) |
| 157 | + |
| 158 | +def download_files_from_gallery(link, folders_in): |
| 159 | + c = urllib3.PoolManager() |
| 160 | + for file in folders_in: |
| 161 | + url = link + file |
| 162 | + filename = file |
| 163 | + os.makedirs(os.path.dirname(filename), exist_ok=True) |
| 164 | + with c.request('GET', url, preload_content=False) as res: |
| 165 | + if res.status == 200: |
| 166 | + with open(filename, 'wb') as out_file: |
| 167 | + shutil.copyfileobj(res, out_file) |
| 168 | + |
| 169 | +download_files_from_gallery(link, folders_in) |
| 170 | +# if logo does not exist, use pyfar logo |
| 171 | +if not os.path.exists(html_logo): |
| 172 | + download_files_from_gallery( |
| 173 | + link, ['resources/logos/pyfar_logos_fixed_size_pyfar.png']) |
| 174 | + shutil.copyfile( |
| 175 | + 'resources/logos/pyfar_logos_fixed_size_pyfar.png', html_logo) |
162 | 176 |
|
163 | 177 | # replace sofar hard link to internal link |
164 | 178 | with open("_static/header.rst", "rt") as fin: |
165 | 179 | with open("header.rst", "wt") as fout: |
166 | | - for line in fin: |
167 | | - fout.write(line.replace(f'https://{project}.readthedocs.io', project)) |
| 180 | + lines = [line.replace(f'https://{project}.readthedocs.io', project) for line in fin] |
| 181 | + contains_project = any(project in line for line in lines) |
| 182 | + |
| 183 | + fout.writelines(lines) |
| 184 | + |
| 185 | + # add project to the list of projects if not in header |
| 186 | + if not contains_project: |
| 187 | + fout.write(f' {project} <{project}>\n') |
| 188 | + |
| 189 | + # count the number of gallery headings |
| 190 | + count_gallery_headings = np.sum( |
| 191 | + ['https://pyfar-gallery.readthedocs.io' in line for line in lines]) |
| 192 | + |
| 193 | + |
| 194 | +# set dropdown header after gallery headings |
| 195 | +html_theme_options['header_links_before_dropdown'] = count_gallery_headings+1 |
| 196 | + |
| 197 | + |
0 commit comments