Created
July 10, 2025 11:42
-
-
Save realamirhe/e462332bb096af2c4e1f78d4f50ddffc to your computer and use it in GitHub Desktop.
create LLM structures from architecture markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| def parse_tree_structure(tree_str: str): | |
| """Parse tree-like structure and return list of directories and files as Paths.""" | |
| lines = [line.split('#')[0].rstrip() for line in tree_str.strip().splitlines()] | |
| cleaned_lines = [line for line in lines if line.strip()] | |
| paths = [] | |
| path_stack = [] | |
| for line in cleaned_lines: | |
| stripped = line.lstrip("│├└─ ") | |
| depth = (len(line) - len(stripped)) // 4 | |
| name = stripped.strip() | |
| # Update current path stack | |
| path_stack = path_stack[:depth] | |
| path_stack.append(name) | |
| current_path = Path(*path_stack) | |
| paths.append(current_path) | |
| return paths | |
| def should_ignore_root_dir(paths): | |
| """Return True if current directory is empty, meaning we can skip the root dir.""" | |
| return not any(Path('.').iterdir()) | |
| def split_dirs_and_files(paths): | |
| """Split parsed paths into directory and file lists.""" | |
| dirs = set() | |
| files = [] | |
| for path in paths: | |
| if str(path).endswith('/'): | |
| dirs.add(path) | |
| elif path.suffix: # Has file extension | |
| files.append(path) | |
| # Also add all parent directories (ensures all parent folders are created) | |
| for parent in path.parents: | |
| if parent == Path(): | |
| continue | |
| dirs.add(parent) | |
| return sorted(dirs), files | |
| def create_directories(dirs, ignore_root): | |
| for d in dirs: | |
| if ignore_root: | |
| d = Path(*d.parts[1:]) | |
| d.mkdir(parents=True, exist_ok=True) | |
| def create_files(files, ignore_root): | |
| for file in files: | |
| final_path = Path(*file.parts[1:]) if ignore_root else file | |
| final_path.parent.mkdir(parents=True, exist_ok=True) | |
| final_path.touch(exist_ok=True) | |
| def main(tree_str): | |
| paths = parse_tree_structure(tree_str) | |
| ignore_root = should_ignore_root_dir(paths) | |
| dirs, files = split_dirs_and_files(paths) | |
| create_directories(dirs, ignore_root) | |
| create_files(files, ignore_root) | |
| print("Paste your tree structure below. End with an empty line (press Enter twice):") | |
| lines = [] | |
| while True: | |
| line = input() | |
| if line.strip() == "": | |
| break | |
| lines.append(line) | |
| tree_input = "\n".join(lines) | |
| main(tree_input) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python -i <(curl -s https://gist.githubusercontent.com/realamirhe/e462332bb096af2c4e1f78d4f50ddffc/raw/f3bedd0651ab377f11d08f8b43af5e7b52665dc6/architect.py)