-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_readme.py
More file actions
59 lines (49 loc) · 2.36 KB
/
generate_readme.py
File metadata and controls
59 lines (49 loc) · 2.36 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
import os
import re
from urllib.parse import quote
def extract_number(filename):
"""Extract numbers from filenames for sorting."""
match = re.search(r"(\d+\.\d+|\d+)", filename)
return float(match.group(1)) if match else float('inf')
def generate_markdown_link(path, base_url):
"""Generate GitHub markdown link for a given file path."""
readable_path = quote(path.replace('./', ''), safe='/')
return f"[{os.path.basename(path)}]({base_url}{readable_path})"
def list_files(directory, base_url, prefix=""):
"""Recursively list files in directory, generating markdown links."""
entries = os.listdir(directory)
entries.sort(key=extract_number)
markdown_content = ""
ignore_files = {'.git', 'generate_readme.py', 'README.md'}
for item in entries:
if item.startswith('.') or item in ignore_files:
continue
full_path = os.path.join(directory, item)
if os.path.isdir(full_path):
markdown_content += f"{prefix}- {item}\n" # Remove the slash for directories
markdown_content += list_files(full_path, base_url, prefix + " ")
else:
markdown_content += f"{prefix}- {generate_markdown_link(full_path, base_url)}\n"
return markdown_content
def update_readme_section(readme_path, new_content, start_marker, end_marker):
"""Update only a specific section of the README.md file."""
with open(readme_path, 'r+') as file:
content = file.read()
start_index = content.find(start_marker) + len(start_marker)
end_index = content.find(end_marker)
if start_index < len(start_marker) or end_index == -1:
raise ValueError("Start or end marker not found in README file.")
updated_content = content[:start_index] + "\n" + new_content + "\n" + content[end_index:]
file.seek(0)
file.write(updated_content)
file.truncate()
# Configuration
base_url = "https://github.com/Jungle-JavaScript-Study/effective-typescript/blob/main/"
directory_path = './' # Adjust as needed
readme_path = 'README.md' # Path to the README file
start_marker = "<!-- FOLDER_STRUCTURE_START -->"
end_marker = "<!-- FOLDER_STRUCTURE_END -->"
# Generate folder structure and update README.md
folder_structure = list_files(directory_path, base_url)
print(folder_structure)
update_readme_section(readme_path, folder_structure, start_marker, end_marker)