|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from typing import Iterator |
| 6 | + |
| 7 | +if(".py" in sys.argv[0]): |
| 8 | + sys.argv.pop(0) |
| 9 | + |
| 10 | + |
| 11 | +if(len(sys.argv) < 3 or len(sys.argv) > 5): |
| 12 | + print( |
| 13 | + "Arguments:\n[0] - Language\n[1] - Base path\n[2] - Allowed filenames\n[3] - Files or folders to ignore (optional)\n[4] - Folders to ignore, but include children (optional)") |
| 14 | + exit(1) |
| 15 | + |
| 16 | +ignore = [] |
| 17 | +skip = [] |
| 18 | +if(len(sys.argv) == 4): |
| 19 | + ignore = sys.argv[3].split(",") |
| 20 | +if(len(sys.argv) == 5): |
| 21 | + skip = sys.argv[4].split(",") |
| 22 | + |
| 23 | +URL_BASE = "https://github.com/TheAlgorithms/" + \ |
| 24 | + sys.argv[0] + "/blob/master" |
| 25 | + |
| 26 | + |
| 27 | +def good_file_paths(top_dir: str = ".") -> Iterator[str]: |
| 28 | + for dir_path, dir_names, filenames in os.walk(top_dir): |
| 29 | + dir_names[:] = [d for d in dir_names if d != |
| 30 | + "scripts" and d[0] not in "._"] |
| 31 | + for filename in filenames: |
| 32 | + if filename == "__init__.py": |
| 33 | + continue |
| 34 | + if any(e.lower() in os.path.join(dir_path, filename).lower() for e in ignore): |
| 35 | + continue |
| 36 | + if os.path.splitext(filename)[1] in sys.argv[2].split(","): |
| 37 | + path = os.path.join(dir_path, filename).lstrip(".").lstrip("/") |
| 38 | + for e in skip: |
| 39 | + path = path.replace(e + "/", "") |
| 40 | + path = path.replace(e + "\\", "") |
| 41 | + yield path |
| 42 | + |
| 43 | + |
| 44 | +def md_prefix(i): |
| 45 | + return f"{i * ' '}*" if i else "\n##" |
| 46 | + |
| 47 | + |
| 48 | +def print_path(old_path: str, new_path: str) -> str: |
| 49 | + old_parts = old_path.split(os.sep) |
| 50 | + for i, new_part in enumerate(new_path.split(os.sep)): |
| 51 | + if i + 1 > len(old_parts) or old_parts[i] != new_part: |
| 52 | + if new_part: |
| 53 | + print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") |
| 54 | + return new_path |
| 55 | + |
| 56 | + |
| 57 | +def print_directory_md(top_dir: str = ".") -> None: |
| 58 | + old_path = "" |
| 59 | + for filepath in sorted(good_file_paths(top_dir)): |
| 60 | + filepath, filename = os.path.split(filepath) |
| 61 | + if filepath != old_path: |
| 62 | + old_path = print_path(old_path, filepath) |
| 63 | + indent = (filepath.count(os.sep) + 1) if filepath else 0 |
| 64 | + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") |
| 65 | + filename = os.path.splitext(filename.replace("_", " ").title())[0] |
| 66 | + print(f"{md_prefix(indent)} [{filename}]({url})") |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + print_directory_md(sys.argv[1]) |
| 71 | + |
| 72 | + |
0 commit comments