Last active
January 2, 2025 20:24
-
-
Save austinsonger/e5cfbdd7b4f36cf03a871226f4aa2fc0 to your computer and use it in GitHub Desktop.
Extracts all code from all files and creating a markdown file that consolidates all the code.
This file contains 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
import os | |
def create_markdown_from_directory(root_directory, output_markdown): | |
""" | |
Walks through a directory and subdirectories, extracting code from all files | |
and creating a markdown file that consolidates all the code. | |
Parameters: | |
root_directory (str): The root directory to traverse. | |
output_markdown (str): The path to the output markdown file. | |
""" | |
with open(output_markdown, 'w', encoding='utf-8') as markdown_file: | |
# Write header to markdown file | |
markdown_file.write("# Consolidated Code\n\n") | |
for dirpath, _, filenames in os.walk(root_directory): | |
if '/virtual/' in dirpath: | |
continue | |
for filename in filenames: | |
file_path = os.path.join(dirpath, filename) | |
# Skip files that are not code (you can customize this filter) | |
if not filename.endswith(('.py', '.js', '.html', '.css', '.java', '.cpp', '.txt')): | |
continue | |
markdown_file.write(f"## File: {file_path}\n\n```") | |
try: | |
with open(file_path, 'r', encoding='utf-8') as code_file: | |
markdown_file.write(code_file.read()) | |
except Exception as e: | |
markdown_file.write(f"Error reading file: {e}") | |
markdown_file.write("\n````\n\n") | |
if __name__ == "__main__": | |
root_dir = "/workspaces/gitrepo" # Replace with your root directory path | |
output_file = "output.md" # Replace with your desired output file path | |
create_markdown_from_directory(root_dir, output_file) | |
print(f"Markdown file created at {output_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment