Skip to content

Commit 724b410

Browse files
Add a table of contents to DIRECTORY.md
Generate a linked table of contents of the top-level sections at the top of DIRECTORY.md so readers can jump straight to a category, as suggested in #13239 / #13111. The list is built in scripts/build_directory_md.py (with a new md_anchor helper + doctests) so DIRECTORY.md stays fully auto-generated.
1 parent 68e01f1 commit 724b410

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎DIRECTORY.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
## Table of Contents
2+
* [Audio Filters](#audio-filters)
3+
* [Backtracking](#backtracking)
4+
* [Bit Manipulation](#bit-manipulation)
5+
* [Blockchain](#blockchain)
6+
* [Boolean Algebra](#boolean-algebra)
7+
* [Cellular Automata](#cellular-automata)
8+
* [Ciphers](#ciphers)
9+
* [Computer Vision](#computer-vision)
10+
* [Conversions](#conversions)
11+
* [Data Compression](#data-compression)
12+
* [Data Structures](#data-structures)
13+
* [Digital Image Processing](#digital-image-processing)
14+
* [Divide And Conquer](#divide-and-conquer)
15+
* [Docs](#docs)
16+
* [Dynamic Programming](#dynamic-programming)
17+
* [Electronics](#electronics)
18+
* [File Transfer](#file-transfer)
19+
* [Financial](#financial)
20+
* [Fractals](#fractals)
21+
* [Fuzzy Logic](#fuzzy-logic)
22+
* [Genetic Algorithm](#genetic-algorithm)
23+
* [Geodesy](#geodesy)
24+
* [Geometry](#geometry)
25+
* [Graphics](#graphics)
26+
* [Graphs](#graphs)
27+
* [Greedy Methods](#greedy-methods)
28+
* [Hashes](#hashes)
29+
* [Knapsack](#knapsack)
30+
* [Linear Algebra](#linear-algebra)
31+
* [Linear Programming](#linear-programming)
32+
* [Machine Learning](#machine-learning)
33+
* [Maths](#maths)
34+
* [Matrix](#matrix)
35+
* [Networking Flow](#networking-flow)
36+
* [Neural Network](#neural-network)
37+
* [Other](#other)
38+
* [Physics](#physics)
39+
* [Project Euler](#project-euler)
40+
* [Quantum](#quantum)
41+
* [Scheduling](#scheduling)
42+
* [Searches](#searches)
43+
* [Sorts](#sorts)
44+
* [Strings](#strings)
45+
* [Tests](#tests)
46+
* [Web Programming](#web-programming)
147

248
## Audio Filters
349
* [Butterworth Filter](audio_filters/butterworth_filter.py)

‎scripts/build_directory_md.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def md_prefix(indent: int) -> str:
3434
return f"{indent * ' '}*" if indent else "\n##"
3535

3636

37+
def md_anchor(heading: str) -> str:
38+
"""
39+
GitHub-style anchor slug for a section heading, so the table of contents
40+
can link to it.
41+
42+
>>> md_anchor("Audio Filters")
43+
'audio-filters'
44+
>>> md_anchor("Bit Manipulation")
45+
'bit-manipulation'
46+
>>> md_anchor("Computer Vision")
47+
'computer-vision'
48+
"""
49+
return heading.strip().lower().replace(" ", "-")
50+
51+
3752
def print_path(old_path: str, new_path: str) -> str:
3853
old_parts = old_path.split(os.sep)
3954
for i, new_part in enumerate(new_path.split(os.sep)):
@@ -43,8 +58,22 @@ def print_path(old_path: str, new_path: str) -> str:
4358

4459

4560
def print_directory_md(top_dir: str = ".") -> None:
61+
filepaths = sorted(good_file_paths(top_dir))
62+
63+
# Top-level sections, in the order they appear, for the table of contents.
64+
sections = list(
65+
dict.fromkeys(
66+
fp.split(os.sep)[0].replace("_", " ").title()
67+
for fp in filepaths
68+
if os.sep in fp
69+
)
70+
)
71+
print("## Table of Contents")
72+
for section in sections:
73+
print(f"* [{section}](#{md_anchor(section)})")
74+
4675
old_path = ""
47-
for filepath in sorted(good_file_paths(top_dir)):
76+
for filepath in filepaths:
4877
filepath, filename = os.path.split(filepath)
4978
if filepath != old_path:
5079
old_path = print_path(old_path, filepath)

0 commit comments

Comments
 (0)