-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathadd-toc-history.py
More file actions
419 lines (338 loc) · 12.3 KB
/
Copy pathadd-toc-history.py
File metadata and controls
419 lines (338 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
"""
T204-T205: Add Table of Contents and History Sections
Adds TOC after the first H1 heading and History section at the bottom of all markdown files.
Usage:
python add-toc-history.py # Dry-run on current directory
python add-toc-history.py --execute # Execute on current directory
python add-toc-history.py --path /path/to/dir --execute
python add-toc-history.py --help
Exit codes:
0 - Success
1 - Error
"""
import argparse
import re
import sys
from datetime import date
from pathlib import Path
from typing import NamedTuple
class ProcessingResult(NamedTuple):
"""Represents the result of processing a file."""
file_path: str
toc_added: bool
history_added: bool
skipped_toc: bool
skipped_history: bool
error: str | None
# Directories to exclude
EXCLUDED_DIRS = [
"node_modules",
".git",
"_backup",
"--old-structure",
]
def should_exclude_path(path: Path) -> bool:
"""Check if the path should be excluded based on directory patterns."""
path_str = str(path).replace("\\", "/")
for excluded in EXCLUDED_DIRS:
if excluded in path_str:
return True
return False
def extract_headings(content: str) -> list[tuple[int, str, str]]:
"""
Extract H2 and H3 headings from markdown content.
Returns:
List of tuples: (level, heading_text, anchor_id)
"""
headings = []
# Match ## and ### headings (not inside code blocks)
in_code_block = False
for line in content.split("\n"):
# Track code blocks
if line.strip().startswith("```"):
in_code_block = not in_code_block
continue
if in_code_block:
continue
# Match H2 and H3
h2_match = re.match(r"^##\s+(.+)$", line)
h3_match = re.match(r"^###\s+(.+)$", line)
if h2_match:
heading_text = h2_match.group(1).strip()
anchor = generate_anchor(heading_text)
headings.append((2, heading_text, anchor))
elif h3_match:
heading_text = h3_match.group(1).strip()
anchor = generate_anchor(heading_text)
headings.append((3, heading_text, anchor))
return headings
def generate_anchor(heading: str) -> str:
"""
Generate a GitHub-compatible anchor ID from a heading.
Removes emojis, special characters, converts to lowercase,
and replaces spaces with hyphens.
"""
# Remove emojis and special characters (keep alphanumeric, spaces, hyphens)
anchor = re.sub(r"[^\w\s-]", "", heading, flags=re.UNICODE)
# Remove extra whitespace
anchor = " ".join(anchor.split())
# Convert to lowercase and replace spaces with hyphens
anchor = anchor.lower().replace(" ", "-")
# Remove consecutive hyphens
anchor = re.sub(r"-+", "-", anchor)
# Strip leading/trailing hyphens
anchor = anchor.strip("-")
return anchor
def generate_toc(headings: list[tuple[int, str, str]]) -> str:
"""
Generate a Table of Contents markdown section.
Args:
headings: List of (level, heading_text, anchor) tuples
Returns:
TOC markdown string
"""
if not headings:
return ""
lines = ["", "## Table of Contents", ""]
for level, text, anchor in headings:
# Skip the Table of Contents heading itself and History section
if text.lower() in ["table of contents", "history"]:
continue
# Indent H3 headings
indent = " " if level == 3 else ""
lines.append(f"{indent}- [{text}](#{anchor})")
lines.append("")
return "\n".join(lines)
def generate_history_section() -> str:
"""Generate the History section markdown."""
today = date.today().isoformat()
return f"""
---
## History
| Date | Author | Changes |
|------|--------|---------|
| {today} | Migration | Initial TOC and history section added |
"""
def has_toc(content: str) -> bool:
"""Check if the content already has a Table of Contents section."""
# Look for ## Table of Contents (case-insensitive, allows emojis/prefixes)
return bool(re.search(r"^##\s+.*Table\s+of\s+Contents", content, re.MULTILINE | re.IGNORECASE))
def has_history(content: str) -> bool:
"""Check if the content already has a History section."""
# Look for ## History (case-insensitive)
return bool(re.search(r"^##\s+History", content, re.MULTILINE | re.IGNORECASE))
def find_first_h1_position(content: str) -> tuple[int, int] | None:
"""
Find the position after the first H1 heading.
Returns:
Tuple of (line_index, char_position) after the H1 line, or None if not found.
"""
lines = content.split("\n")
in_frontmatter = False
frontmatter_ended = False
for i, line in enumerate(lines):
# Handle YAML frontmatter
if i == 0 and line.strip() == "---":
in_frontmatter = True
continue
if in_frontmatter:
if line.strip() == "---":
in_frontmatter = False
frontmatter_ended = True
continue
# Look for H1 (# Heading)
if re.match(r"^#\s+.+$", line):
# Return position after this line
return i, sum(len(l) + 1 for l in lines[:i+1])
return None
def process_file(file_path: Path, execute: bool = False) -> ProcessingResult:
"""
Process a single markdown file to add TOC and History sections.
Args:
file_path: Path to the markdown file
execute: If True, actually modify the file; if False, dry-run
Returns:
ProcessingResult with details of what was/would be done
"""
file_str = str(file_path)
try:
content = file_path.read_text(encoding="utf-8")
except Exception as e:
return ProcessingResult(
file_path=file_str,
toc_added=False,
history_added=False,
skipped_toc=False,
skipped_history=False,
error=f"Could not read file: {e}"
)
toc_added = False
history_added = False
skipped_toc = has_toc(content)
skipped_history = has_history(content)
modified_content = content
# Add TOC if not present
if not skipped_toc:
h1_pos = find_first_h1_position(content)
if h1_pos is not None:
headings = extract_headings(content)
# Filter out headings that come before the TOC would be inserted
toc = generate_toc(headings)
if toc: # Only add if there are headings to list
line_idx, char_pos = h1_pos
lines = modified_content.split("\n")
# Insert TOC after the H1 line
lines.insert(line_idx + 1, toc.rstrip())
modified_content = "\n".join(lines)
toc_added = True
# Add History section if not present
if not skipped_history:
history = generate_history_section()
# Ensure we don't double up on newlines at the end
modified_content = modified_content.rstrip() + history
history_added = True
# Write if executing and changes were made
if execute and (toc_added or history_added):
try:
file_path.write_text(modified_content, encoding="utf-8")
except Exception as e:
return ProcessingResult(
file_path=file_str,
toc_added=False,
history_added=False,
skipped_toc=skipped_toc,
skipped_history=skipped_history,
error=f"Could not write file: {e}"
)
return ProcessingResult(
file_path=file_str,
toc_added=toc_added,
history_added=history_added,
skipped_toc=skipped_toc,
skipped_history=skipped_history,
error=None
)
def find_markdown_files(directory: Path) -> list[Path]:
"""
Find all markdown files in a directory, excluding specified patterns.
Args:
directory: Root directory to search
Returns:
List of Path objects for markdown files
"""
md_files = []
for md_file in directory.rglob("*.md"):
if not should_exclude_path(md_file):
md_files.append(md_file)
return sorted(md_files)
def main() -> int:
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Add Table of Contents and History sections to markdown files.",
epilog="""
Examples:
python add-toc-history.py # Dry-run in current directory
python add-toc-history.py --execute # Execute in current directory
python add-toc-history.py --path ./docs --execute # Execute in specific directory
python add-toc-history.py --verbose # Show detailed output
Behavior:
- TOC is added after the first H1 heading
- TOC is generated from H2 and H3 headings
- History section is added at the bottom of the file
- Files already containing these sections are skipped
Excluded directories:
node_modules, .git, _backup*, --old-structure*
"""
)
parser.add_argument(
"--path",
type=Path,
default=Path.cwd(),
help="Directory to process (default: current directory)"
)
parser.add_argument(
"--execute",
action="store_true",
help="Actually modify files (default is dry-run)"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Show verbose output including per-file details"
)
parser.add_argument(
"-q", "--quiet",
action="store_true",
help="Suppress all output except errors"
)
args = parser.parse_args()
if not args.path.is_dir():
print(f"Error: {args.path} is not a directory")
return 1
mode = "EXECUTE" if args.execute else "DRY-RUN"
if not args.quiet:
print(f"{'='*60}")
print(f"Add TOC and History Sections - {mode} Mode")
print(f"{'='*60}")
print(f"Directory: {args.path.absolute()}")
print()
# Find all markdown files
md_files = find_markdown_files(args.path)
if not args.quiet:
print(f"Found {len(md_files)} markdown files to process")
print()
# Process each file
results: list[ProcessingResult] = []
for md_file in md_files:
result = process_file(md_file, execute=args.execute)
results.append(result)
if args.verbose and not args.quiet:
status_parts = []
if result.toc_added:
status_parts.append("TOC added")
elif result.skipped_toc:
status_parts.append("TOC exists")
else:
status_parts.append("No H1/headings")
if result.history_added:
status_parts.append("History added")
elif result.skipped_history:
status_parts.append("History exists")
if result.error:
status_parts.append(f"ERROR: {result.error}")
print(f" {md_file}: {', '.join(status_parts)}")
# Calculate statistics
toc_added_count = sum(1 for r in results if r.toc_added)
history_added_count = sum(1 for r in results if r.history_added)
toc_skipped_count = sum(1 for r in results if r.skipped_toc)
history_skipped_count = sum(1 for r in results if r.skipped_history)
error_count = sum(1 for r in results if r.error)
if not args.quiet:
print()
print(f"{'='*60}")
print("Summary")
print(f"{'='*60}")
print(f"Total files processed: {len(results)}")
print()
print("Table of Contents:")
print(f" - Added: {toc_added_count}")
print(f" - Skipped (already exists): {toc_skipped_count}")
print(f" - No H1/headings: {len(results) - toc_added_count - toc_skipped_count}")
print()
print("History Section:")
print(f" - Added: {history_added_count}")
print(f" - Skipped (already exists): {history_skipped_count}")
print()
if error_count > 0:
print(f"Errors: {error_count}")
for r in results:
if r.error:
print(f" - {r.file_path}: {r.error}")
if not args.execute:
print()
print("NOTE: This was a DRY-RUN. No files were modified.")
print(" Run with --execute to apply changes.")
return 0 if error_count == 0 else 1
if __name__ == "__main__":
sys.exit(main())