Added current directory

This commit is contained in:
Kevin Veen-Birkenbach 2025-03-17 13:19:23 +01:00
parent 75a110fde9
commit c3988ce812
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
2 changed files with 39 additions and 10 deletions

View File

@ -45,9 +45,9 @@ def collect_folder_tree(dir_path, base_url):
folder_title = headings[0]['text'] if headings else os.path.basename(dir_path)
folder_link = os.path.join(base_url, os.path.splitext(rep_file)[0])
# Remove the representative file from the list to avoid duplication
# Remove the representative file from the list to avoid duplication,
# and filter out any additional "readme.md" or "index.rst" files.
files.remove(rep_file)
# Also filter out any files that are explicitly "readme.md" or "index.rst"
files = [f for f in files if f.lower() not in ['readme.md', 'index.rst']]
# Process the remaining files in the current directory
@ -86,13 +86,41 @@ def collect_folder_tree(dir_path, base_url):
'filename': os.path.basename(dir_path)
}
def mark_current(node, active):
"""
Recursively mark nodes as current if the active page (pagename)
matches the node's link or is a descendant of it.
The function sets node['current'] = True if:
- The node's link matches the active page exactly, or
- The active page begins with the node's link plus a separator (indicating a child).
Additionally, if any child node is current, the parent is marked as current.
"""
is_current = False
node_link = node.get('link', '').rstrip('/')
active = active.rstrip('/')
if node_link and (active == node_link or active.startswith(node_link + '/')):
is_current = True
# Recurse into children if they exist
children = node.get('children', [])
for child in children:
if mark_current(child, active):
is_current = True
node['current'] = is_current
return is_current
def add_local_subfolders(app, pagename, templatename, context, doctree):
"""
Sets the 'local_subfolders' context variable with the entire folder tree
starting from app.srcdir.
starting from app.srcdir, and marks the tree with the 'current' flag up
to the active page.
"""
root_dir = app.srcdir
folder_tree = collect_folder_tree(root_dir, '')
if folder_tree:
mark_current(folder_tree, pagename)
context['local_subfolders'] = [folder_tree] if folder_tree else []
def setup(app):

View File

@ -2,18 +2,20 @@
<ul class="toctree-l{{ level }}" style="list-style: none; padding-left: 0;">
{% for item in headings %}
<li class="toctree-l{{ level }}{% if item.current %} current{% endif %}"
{% if item.children %} x-data="{ expanded: false }" {% endif %}
{% if item.children %}
x-data="{ expanded: {{ 'true' if item.current else 'false' }} }"
{% endif %}
style="white-space: nowrap;">
<div class="menu-item" style="display: inline-flex; align-items: center; justify-content: space-between; width: 100%; white-space: nowrap;">
<!-- Link- und "Datei öffnen"-Bereich -->
<!-- Link and file open section -->
<div style="display: inline-flex; align-items: center; white-space: nowrap;">
<a class="reference internal{% if item.children %} expandable{% endif %}"
<a class="reference internal{% if item.children %} expandable{% endif %}{% if item.current %} current{% endif %}"
href="{{ pathto(item.link).replace('#', '') }}{% if item.anchor %}#{{ item.anchor }}{% endif %}"
style="text-decoration: none; white-space: nowrap;">
{{ item.text }}
</a>
</div>
<!-- Expand-Toggle-Button -->
<!-- Expand-Toggle Button -->
{% if item.children %}
<button @click.prevent.stop="expanded = !expanded" type="button" class="toggle-button"
style="background: none; border: none; padding: 0; margin-left: auto;">
@ -37,16 +39,15 @@
</ul>
{% endmacro %}
{% if local_md_headings or local_subfolders %}
<div class="local-md-headings">
<h3>Overview</h3>
<hr />
<h4 class="toctree-l1">Current Index</h3>
<h4 class="toctree-l1">Current Index</h4>
{% if local_md_headings %}
{{ render_headings(local_md_headings) }}
{% endif %}
<h4 class="toctree-l1">File Explorer</h3>
<h4 class="toctree-l1">File Explorer</h4>
{% if local_subfolders %}
{{ render_headings(local_subfolders) }}
{% endif %}