Implemented new template and optimized navigation

This commit is contained in:
2025-03-16 18:42:03 +01:00
parent 3e1a9e9dde
commit 18fba35173
5 changed files with 56 additions and 39 deletions

View File

@@ -1,25 +1,19 @@
{% macro render_headings(headings) %}
<ul>
{% for item in headings %}
<li>
<a href="{{ pathto(item.link).replace("#", "") + '#' + item.anchor }}">{{ item.text }}</a>
{% if item.children %}
{{ render_headings(item.children) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endmacro %}
{% if local_md_headings %}
<div class="local-md-headings">
<h3>Page Headings</h3>
{% set ns = namespace(current_level=0) %}
{% for item in local_md_headings %}
{# If the current heading level is greater than the previous, open new <ul> tags #}
{% if item.level > ns.current_level %}
{% for i in range(ns.current_level, item.level) %}
<ul>
{% endfor %}
{# If the current heading level is less than the previous, close the open <ul> tags #}
{% elif item.level < ns.current_level %}
{% for i in range(item.level, ns.current_level) %}
</ul>
{% endfor %}
{% endif %}
<li><a href="{{ pathto(item.link).replace("#","") + "#" + item.anchor}}">{{ item.text }}</a></li>
{% set ns.current_level = item.level %}
{% endfor %}
{# Close any remaining open <ul> tags #}
{% for i in range(ns.current_level) %}
</ul>
{% endfor %}
{{ render_headings(local_md_headings) }}
</div>
{% endif %}

View File

@@ -25,7 +25,7 @@ exclude_patterns = ['docs', 'venv', 'venv/**']
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
html_theme = 'alabaster'
html_theme = 'sphinxawesome_theme'
html_static_path = ['_static']
html_sidebars = {
@@ -40,7 +40,7 @@ html_sidebars = {
html_theme_options = {
'fixed_sidebar': True,
# 'fixed_sidebar': True,
}
# Liste der Dateiendungen, die Sphinx verarbeiten soll:

View File

@@ -47,14 +47,37 @@ def extract_headings_from_file(filepath, max_level=MAX_HEADING_LEVEL):
logger.warning(f"Error reading {filepath}: {e}")
return headings
def group_headings(headings):
"""
Converts a flat list of headings into a tree structure based on their level.
Each heading gets a 'children' list.
"""
tree = []
stack = []
for heading in headings:
heading['children'] = []
# Pop headings from the stack that are at or deeper than the current level
while stack and stack[-1]['level'] >= heading['level']:
stack.pop()
if stack:
# Append the current heading as a child of the last item in the stack
stack[-1]['children'].append(heading)
else:
tree.append(heading)
stack.append(heading)
return tree
def sort_tree(tree):
"""
Sorts a list of headings (and their children) by their text.
"""
tree.sort(key=lambda x: natural_sort_key(x['text']))
for node in tree:
if node.get('children'):
sort_tree(node['children'])
def add_local_md_headings(app, pagename, templatename, context, doctree):
"""
For every Markdown file in the same directory as the current page,
extract its headings, sort them in natural ascending order, and add them
to the context.
"""
srcdir = app.srcdir
# Determine the directory of the current page (e.g., "directory/file" -> "directory")
directory = os.path.dirname(pagename)
abs_dir = os.path.join(srcdir, directory)
if not os.path.isdir(abs_dir):
@@ -68,17 +91,18 @@ def add_local_md_headings(app, pagename, templatename, context, doctree):
filepath = os.path.join(abs_dir, file)
headings = extract_headings_from_file(filepath)
for heading in headings:
# Build file link: zunächst Pfad + Dateiname, dann Ersetzen der .md-Endung durch .html
file_link = os.path.join(directory, file).replace(".md","") if directory else file
base = file[:-3]
file_link = os.path.join(directory, base)
local_md_headings.append({
'level': heading['level'],
'text': heading['text'],
'link': file_link,
'anchor': heading['anchor']
})
# Sort headings in natural ascending order using natural_sort_key.
local_md_headings.sort(key=lambda x: natural_sort_key(x['text']))
context['local_md_headings'] = local_md_headings
# Proceed with grouping and sorting as before...
tree = group_headings(local_md_headings)
sort_tree(tree)
context['local_md_headings'] = tree
def setup(app):
app.connect('html-page-context', add_local_md_headings)

View File

@@ -1,3 +1,3 @@
myst-parser
sphinx
sphinx-rtd-theme
sphinxawesome-theme