-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_json.py
More file actions
executable file
·81 lines (60 loc) · 2.81 KB
/
generate_json.py
File metadata and controls
executable file
·81 lines (60 loc) · 2.81 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
#!/usr/bin/env python3
import os
import json
import re
def extract_system_prompt(file_path):
"""Extract the content of a markdown file as the system prompt."""
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
return content
def get_prompt_name(file_path):
"""Extract a clean name from the file path."""
# Get the base filename without extension
base_name = os.path.splitext(os.path.basename(file_path))[0]
# Convert kebab-case to Title Case
name = ' '.join(word.capitalize() for word in base_name.split('-'))
return name
def process_directory(directory, output_dir):
"""Process all markdown files in a directory and its subdirectories."""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
# Skip the README.md file
if file.lower() == 'readme.md':
continue
# Get the prompt name
name = get_prompt_name(file_path)
# Extract the system prompt content
system_prompt = extract_system_prompt(file_path)
# Create JSON structure
prompt_json = {
"name": name,
"system_prompt": system_prompt
}
# Determine output path - maintain the same directory structure
rel_path = os.path.relpath(file_path, directory)
output_subdir = os.path.dirname(rel_path)
output_path = os.path.join(output_dir, output_subdir)
if output_subdir and not os.path.exists(output_path):
os.makedirs(output_path)
# Create JSON file
json_filename = f"{os.path.splitext(os.path.basename(file_path))[0]}.json"
json_path = os.path.join(output_path, json_filename)
with open(json_path, 'w', encoding='utf-8') as json_file:
json.dump(prompt_json, json_file, indent=2)
print(f"Created JSON for: {name} -> {json_path}")
def main():
# Base directory containing markdown files
base_dir = os.path.dirname(os.path.abspath(__file__))
# Output directory for JSON files
output_dir = os.path.join(base_dir, 'json')
print(f"Scanning directory: {base_dir}")
print(f"Output directory: {output_dir}")
# Process all markdown files
process_directory(base_dir, output_dir)
print("JSON generation complete!")
if __name__ == "__main__":
main()