mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-03-31 14:24:16 +02:00
Solved header bug
This commit is contained in:
parent
e1da02db3d
commit
d84ffc375b
@ -33,7 +33,7 @@ html_sidebars = {
|
|||||||
#'globaltoc.html',
|
#'globaltoc.html',
|
||||||
# 'relations.html',
|
# 'relations.html',
|
||||||
# 'sourcelink.html',
|
# 'sourcelink.html',
|
||||||
'local_md_files.html', # Include your custom template
|
'structure.html', # Include your custom template
|
||||||
# 'searchbox.html',
|
# 'searchbox.html',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,11 @@ def extract_headings_from_file(filepath, max_level=MAX_HEADING_LEVEL):
|
|||||||
Extract headings from a file.
|
Extract headings from a file.
|
||||||
For Markdown (.md) files, looks for lines starting with '#' (up to max_level).
|
For Markdown (.md) files, looks for lines starting with '#' (up to max_level).
|
||||||
For reStructuredText (.rst) files, looks for a line immediately followed by an underline.
|
For reStructuredText (.rst) files, looks for a line immediately followed by an underline.
|
||||||
|
If no headings are found and the file is an index file while a README.md exists in the same folder,
|
||||||
|
it will try to extract headings from the README.md instead.
|
||||||
Returns a list of dictionaries with keys: 'level', 'text', and 'anchor' (if applicable).
|
Returns a list of dictionaries with keys: 'level', 'text', and 'anchor' (if applicable).
|
||||||
"""
|
"""
|
||||||
|
import os, re
|
||||||
headings = []
|
headings = []
|
||||||
ext = os.path.splitext(filepath)[1].lower()
|
ext = os.path.splitext(filepath)[1].lower()
|
||||||
try:
|
try:
|
||||||
@ -47,10 +50,22 @@ def extract_headings_from_file(filepath, max_level=MAX_HEADING_LEVEL):
|
|||||||
if len(underline) >= 3 and re.fullmatch(r'[-=~\^\+"\'`]+', underline):
|
if len(underline) >= 3 and re.fullmatch(r'[-=~\^\+"\'`]+', underline):
|
||||||
level = 1
|
level = 1
|
||||||
heading_text = text_line.strip()
|
heading_text = text_line.strip()
|
||||||
# For reST, the anchor is left empty (can be generated later if needed)
|
|
||||||
headings.append({'level': level, 'text': heading_text, 'anchor': ''})
|
headings.append({'level': level, 'text': heading_text, 'anchor': ''})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Error reading {filepath}: {e}")
|
print(f"Warning: Error reading {filepath}: {e}")
|
||||||
|
|
||||||
|
# If no headings were found and the file is an index file,
|
||||||
|
# then try to load headings from a README.md in the same folder.
|
||||||
|
if not headings:
|
||||||
|
base = os.path.basename(filepath).lower()
|
||||||
|
if base == 'index.rst':
|
||||||
|
folder = os.path.dirname(filepath)
|
||||||
|
readme_path = os.path.join(folder, 'README.md')
|
||||||
|
if os.path.isfile(readme_path):
|
||||||
|
try:
|
||||||
|
headings = extract_headings_from_file(readme_path, max_level)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Warning: Error reading fallback README.md in {folder}: {e}")
|
||||||
return headings
|
return headings
|
||||||
|
|
||||||
def group_headings(headings):
|
def group_headings(headings):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user