-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodepack.py
More file actions
220 lines (177 loc) · 6.91 KB
/
codepack.py
File metadata and controls
220 lines (177 loc) · 6.91 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""
CodePack: A tool to pack a directory of code files into a single Markdown file.
This script traverses a given directory, creates a visual representation of its
structure, and combines the content of all files into a single Markdown document.
It's useful for code sharing and documentation purposes.
"""
import os
import argparse
from pathlib import Path
import fnmatch
DEFAULT_IGNORE_PATTERNS = [
'.git', '.git/**', # Ignore the entire .git directory and its contents
'.gitignore', '.gitattributes',
'.vscode/', '.idea/', '*.swp', '*.swo', '*~',
'__pycache__/', '*.pyc',
'venv/', 'env/', '.env',
'.DS_Store', 'Thumbs.db',
'build/', 'dist/',
'node_modules/',
'*.log',
'*.bak', '*.tmp'
]
def load_ignore_patterns(directory):
"""
Load ignore patterns from .gitignore or .codepackignore file.
Args:
directory (Path): The directory to search for ignore files.
Returns:
list: List of ignore patterns.
"""
gitignore = directory / '.gitignore'
codepackignore = directory / '.codepackignore'
if gitignore.exists():
with open(gitignore, 'r') as f:
patterns = [line.strip() for line in f if line.strip() and not line.startswith('#')]
elif codepackignore.exists():
with open(codepackignore, 'r') as f:
patterns = [line.strip() for line in f if line.strip() and not line.startswith('#')]
else:
patterns = DEFAULT_IGNORE_PATTERNS
return patterns
def should_ignore(path, ignore_patterns, base_dir):
"""
Check if a path should be ignored based on ignore patterns.
Args:
path (str): The path to check.
ignore_patterns (list): List of ignore patterns.
base_dir (str): The base directory for relative path calculation.
Returns:
bool: True if the path should be ignored, False otherwise.
"""
rel_path = os.path.relpath(path, base_dir)
for pattern in ignore_patterns:
if fnmatch.fnmatch(rel_path, pattern) or fnmatch.fnmatch(os.path.basename(path), pattern):
return True
return False
def is_binary_file(file_path):
"""
Check if a file is binary.
Args:
file_path (str): The path to the file.
Returns:
bool: True if the file is binary, False otherwise.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
file.read(1024)
return False
except UnicodeDecodeError:
return True
def create_project_structure(directory, ignore_patterns):
"""
Generate a visual representation of the project structure.
Args:
directory (Path): The path to the directory to process.
ignore_patterns (list): List of ignore patterns.
Returns:
str: A string representation of the project structure.
"""
structure = ["# Project Structure"]
def add_to_structure(path, prefix=''):
if should_ignore(path, ignore_patterns, directory):
return
rel_path = os.path.relpath(path, directory)
name = os.path.basename(path)
if os.path.isdir(path):
structure.append(f"{prefix}└── {name}")
items = sorted(os.listdir(path))
for i, item in enumerate(items):
item_path = os.path.join(path, item)
new_prefix = prefix + " "
if i < len(items) - 1:
new_prefix = prefix + "│ "
add_to_structure(item_path, new_prefix)
elif not is_binary_file(path):
structure.append(f"{prefix}├── {name}")
structure.append(f"/{os.path.basename(directory)}")
items = sorted(os.listdir(directory))
for i, item in enumerate(items):
item_path = os.path.join(directory, item)
if i == len(items) - 1:
add_to_structure(item_path, "")
else:
add_to_structure(item_path, "│")
return '\n'.join(structure)
def read_file_content(file_path):
"""
Read and return the content of a file.
Args:
file_path (Path): The path to the file to read.
Returns:
str: The content of the file.
"""
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def get_file_extension(file_path):
"""
Get the file extension, defaulting to 'txt' if not present.
Args:
file_path (Path): The path to the file.
Returns:
str: The file extension without the dot, or 'txt' if no extension.
"""
return file_path.suffix[1:] if file_path.suffix else 'txt'
def create_markdown_content(directory, ignore_patterns):
"""
Create the full Markdown content including structure and file contents.
Args:
directory (Path): The path to the directory to process.
ignore_patterns (list): List of ignore patterns.
Returns:
str: The complete Markdown content.
"""
content = [create_project_structure(directory, ignore_patterns), ""]
for root, _, files in os.walk(directory):
rel_root = os.path.relpath(root, directory)
if should_ignore(rel_root, ignore_patterns, directory):
continue
for file in sorted(files):
file_path = os.path.join(root, file)
if should_ignore(file_path, ignore_patterns, directory) or is_binary_file(file_path):
continue
relative_path = os.path.relpath(file_path, directory)
extension = get_file_extension(Path(file_path))
content.extend([
f"\n# {relative_path}",
f"```{extension}",
read_file_content(file_path),
"```"
])
return '\n'.join(content)
def main():
"""
Main function to run the script.
Parses command-line arguments, processes the specified directory,
and writes the output to a Markdown file.
"""
parser = argparse.ArgumentParser(description="Pack a directory of code files into a single Markdown file.")
parser.add_argument("directory", nargs='?', default='.', help="Path to the directory to process (default: current directory)")
parser.add_argument("-o", "--output", help="Path for the output Markdown file (default: {directory_name}.md in the current directory)")
args = parser.parse_args()
directory = Path(args.directory).expanduser().resolve()
if not directory.is_dir():
print(f"Error: {directory} is not a valid directory.")
return
ignore_patterns = load_ignore_patterns(directory)
if args.output:
output_path = Path(args.output).expanduser().resolve()
else:
output_path = Path(f"{directory.name}.md")
markdown_content = create_markdown_content(directory, ignore_patterns)
with open(output_path, 'w', encoding='utf-8') as output_file:
output_file.write(markdown_content)
print(f"Markdown file created successfully: {output_path}")
if __name__ == "__main__":
main()