mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-03-29 04:23:34 +01:00
Optimized Index
This commit is contained in:
parent
756407d407
commit
9dfd019e0a
@ -5,7 +5,7 @@ from sphinx.util import logging # Sphinx logging is used elsewhere if needed
|
|||||||
from docutils.parsers.rst import Directive
|
from docutils.parsers.rst import Directive
|
||||||
from .nav_utils import natural_sort_key, extract_headings_from_file, group_headings, sort_tree, MAX_HEADING_LEVEL, DEFAULT_MAX_NAV_DEPTH
|
from .nav_utils import natural_sort_key, extract_headings_from_file, group_headings, sort_tree, MAX_HEADING_LEVEL, DEFAULT_MAX_NAV_DEPTH
|
||||||
|
|
||||||
# Use standard logging to set the level based on command-line args.
|
# Set up our logger based on command-line args.
|
||||||
logger = std_logging.getLogger(__name__)
|
logger = std_logging.getLogger(__name__)
|
||||||
if any(arg in sys.argv for arg in ["-v", "--verbose"]):
|
if any(arg in sys.argv for arg in ["-v", "--verbose"]):
|
||||||
logger.setLevel(std_logging.DEBUG)
|
logger.setLevel(std_logging.DEBUG)
|
||||||
@ -14,47 +14,6 @@ else:
|
|||||||
|
|
||||||
DEFAULT_MAX_NAV_DEPTH = 4
|
DEFAULT_MAX_NAV_DEPTH = 4
|
||||||
|
|
||||||
def postprocess_current(nodes, current_file, current_anchor):
|
|
||||||
"""
|
|
||||||
Recursively process the tree nodes in post-order.
|
|
||||||
If a node or any child node matches the current file and anchor,
|
|
||||||
mark that node as current.
|
|
||||||
Returns True if the current subtree contains a current node.
|
|
||||||
"""
|
|
||||||
found_in_subtree = False
|
|
||||||
for node in nodes:
|
|
||||||
# Process children first (post-order)
|
|
||||||
child_found = False
|
|
||||||
if 'children' in node and node['children']:
|
|
||||||
child_found = postprocess_current(node['children'], current_file, current_anchor)
|
|
||||||
|
|
||||||
# Get the file and anchor for the current node, trimming trailing slashes and whitespace.
|
|
||||||
node_file = node.get('link', '').rstrip('/')
|
|
||||||
node_anchor = node.get('anchor', '').strip()
|
|
||||||
|
|
||||||
# Debug: output the current node's values and the comparison values.
|
|
||||||
logger.debug("Checking node: text=%s, link=%s, anchor=%s",
|
|
||||||
node.get('text', ''),
|
|
||||||
node_file,
|
|
||||||
node_anchor)
|
|
||||||
logger.debug("Comparing with current_file=%s, current_anchor=%s",
|
|
||||||
current_file.rstrip('/'),
|
|
||||||
current_anchor.strip())
|
|
||||||
|
|
||||||
# Mark node as current if it exactly matches the current file and anchor.
|
|
||||||
if node_file == current_file.rstrip('/') and node_anchor == current_anchor.strip():
|
|
||||||
node['current'] = True
|
|
||||||
logger.debug("Node '%s' marked as current (exact match).", node.get('text', ''))
|
|
||||||
found = True
|
|
||||||
else:
|
|
||||||
node['current'] = child_found
|
|
||||||
if child_found:
|
|
||||||
logger.debug("Node '%s' marked as current (child match).", node.get('text', ''))
|
|
||||||
|
|
||||||
if node['current']:
|
|
||||||
found_in_subtree = True
|
|
||||||
return found_in_subtree
|
|
||||||
|
|
||||||
def add_local_file_headings(app, pagename, templatename, context, doctree):
|
def add_local_file_headings(app, pagename, templatename, context, doctree):
|
||||||
logger.debug("add_local_file_headings called with pagename: %s", pagename)
|
logger.debug("add_local_file_headings called with pagename: %s", pagename)
|
||||||
|
|
||||||
@ -94,23 +53,6 @@ def add_local_file_headings(app, pagename, templatename, context, doctree):
|
|||||||
sort_tree(tree)
|
sort_tree(tree)
|
||||||
|
|
||||||
logger.debug("Generated tree: %s", tree)
|
logger.debug("Generated tree: %s", tree)
|
||||||
|
|
||||||
# Determine current file and anchor.
|
|
||||||
# This implementation assumes that if an anchor is present, it is appended to pagename as "#anchor".
|
|
||||||
if '#' in pagename:
|
|
||||||
current_file, current_anchor = pagename.split('#', 1)
|
|
||||||
else:
|
|
||||||
current_file, current_anchor = pagename, ''
|
|
||||||
|
|
||||||
logger.debug("Current file: %s, Current anchor: %s", current_file, current_anchor)
|
|
||||||
|
|
||||||
# Postprocess the tree: bubble up the 'current' flag from children to parents.
|
|
||||||
if current_anchor:
|
|
||||||
postprocess_current(tree, current_file, current_anchor)
|
|
||||||
else:
|
|
||||||
logger.debug("No anchor provided; skipping current marking.")
|
|
||||||
|
|
||||||
logger.debug("Final tree after postprocessing: %s", tree)
|
|
||||||
context['local_md_headings'] = tree
|
context['local_md_headings'] = tree
|
||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
|
@ -41,13 +41,16 @@
|
|||||||
|
|
||||||
{% if local_md_headings or local_subfolders %}
|
{% if local_md_headings or local_subfolders %}
|
||||||
<div class="local-md-headings">
|
<div class="local-md-headings">
|
||||||
<h3>Overview</h3>
|
<h3>Index</h3>
|
||||||
<hr />
|
<p class="caption" role="heading">
|
||||||
<h4 class="toctree-l1">Current Index</h4>
|
<span class="caption-text">Current Index</span>
|
||||||
|
</p>
|
||||||
{% if local_md_headings %}
|
{% if local_md_headings %}
|
||||||
{{ render_headings(local_md_headings) }}
|
{{ render_headings(local_md_headings) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<h4 class="toctree-l1">File Explorer</h4>
|
<p class="caption" role="heading">
|
||||||
|
<span class="caption-text">Full Index</span>
|
||||||
|
</p>
|
||||||
{% if local_subfolders %}
|
{% if local_subfolders %}
|
||||||
{{ render_headings(local_subfolders) }}
|
{{ render_headings(local_subfolders) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user