2019-07-28 23:27:23 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-06-17 18:17:53 +08:00
|
|
|
import os
|
2022-07-11 16:19:52 +08:00
|
|
|
from collections.abc import Iterator
|
2019-07-28 23:27:23 +08:00
|
|
|
|
2019-06-17 18:17:53 +08:00
|
|
|
|
2020-03-04 20:40:28 +08:00
|
|
|
def good_file_paths(top_dir: str = ".") -> Iterator[str]:
|
|
|
|
for dir_path, dir_names, filenames in os.walk(top_dir):
|
|
|
|
dir_names[:] = [d for d in dir_names if d != "scripts" and d[0] not in "._"]
|
2019-07-28 23:27:23 +08:00
|
|
|
for filename in filenames:
|
|
|
|
if filename == "__init__.py":
|
|
|
|
continue
|
|
|
|
if os.path.splitext(filename)[1] in (".py", ".ipynb"):
|
2020-03-04 20:40:28 +08:00
|
|
|
yield os.path.join(dir_path, filename).lstrip("./")
|
2019-10-05 13:14:13 +08:00
|
|
|
|
2019-06-17 18:17:53 +08:00
|
|
|
|
2019-07-28 23:27:23 +08:00
|
|
|
def md_prefix(i):
|
2019-10-26 01:33:24 +08:00
|
|
|
return f"{i * ' '}*" if i else "\n##"
|
2019-06-17 18:17:53 +08:00
|
|
|
|
|
|
|
|
2019-07-28 23:27:23 +08:00
|
|
|
def print_path(old_path: str, new_path: str) -> str:
|
|
|
|
old_parts = old_path.split(os.sep)
|
|
|
|
for i, new_part in enumerate(new_path.split(os.sep)):
|
2023-03-02 00:23:33 +08:00
|
|
|
if (i + 1 > len(old_parts) or old_parts[i] != new_part) and new_part:
|
|
|
|
print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}")
|
2019-07-28 23:27:23 +08:00
|
|
|
return new_path
|
2019-06-17 18:17:53 +08:00
|
|
|
|
|
|
|
|
2019-07-28 23:27:23 +08:00
|
|
|
def print_directory_md(top_dir: str = ".") -> None:
|
|
|
|
old_path = ""
|
2020-06-03 03:14:12 +08:00
|
|
|
for filepath in sorted(good_file_paths(top_dir)):
|
2019-07-28 23:27:23 +08:00
|
|
|
filepath, filename = os.path.split(filepath)
|
|
|
|
if filepath != old_path:
|
|
|
|
old_path = print_path(old_path, filepath)
|
|
|
|
indent = (filepath.count(os.sep) + 1) if filepath else 0
|
2022-06-22 12:01:05 +08:00
|
|
|
url = "/".join((filepath, filename)).replace(" ", "%20")
|
2019-10-26 01:33:24 +08:00
|
|
|
filename = os.path.splitext(filename.replace("_", " ").title())[0]
|
2019-07-28 23:27:23 +08:00
|
|
|
print(f"{md_prefix(indent)} [{filename}]({url})")
|
2019-06-17 18:17:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-07-28 23:27:23 +08:00
|
|
|
print_directory_md(".")
|