@@ -96,7 +96,7 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]:
96
96
97
97
# Write a header for the TOC file
98
98
out = []
99
- out .append ("# ChatGPT System Prompts - Table of Contents \n \n " )
99
+ out .append ("# ChatGPT System Prompts \n \n " )
100
100
out .append ("This document contains a table of contents for the ChatGPT System Prompts repository.\n \n " )
101
101
102
102
# Add links to TOC.md files in prompts directory subdirectories
@@ -197,21 +197,37 @@ def find_gptfile(keyword, verbose=True):
197
197
198
198
def generate_toc_for_prompts_dirs () -> Tuple [bool , str ]:
199
199
"""
200
- Generates a single TOC.md file for each of the three main directories under prompts:
201
- gpts, official-product, and opensource-prj.
202
- For gpts directory, uses the original GPT-specific TOC generation logic.
203
- For other directories, includes all markdown files in the directory and its subdirectories.
200
+ Generates a single TOC.md file for each directory under prompts:
201
+ - For the gpts directory, uses the original GPT-specific TOC generation logic.
202
+ - For all other directories (including newly added ones), uses the generic recursive logic.
203
+
204
+ This function automatically detects all subdirectories under prompts, ensuring future-proof
205
+ extensibility without requiring code changes when new directories are added.
204
206
"""
205
207
prompts_base_path = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' , 'prompts' ))
206
208
if not os .path .exists (prompts_base_path ):
207
209
return (False , f"Prompts directory '{ prompts_base_path } ' does not exist." )
208
210
209
- print (f"Generating TOC.md files for main directories under '{ prompts_base_path } '" )
211
+ print (f"Generating TOC.md files for all directories under '{ prompts_base_path } '" )
210
212
success = True
211
213
messages = []
212
214
213
- # Main directories we want to process
214
- main_dirs = ["gpts" , "official-product" , "opensource-prj" ]
215
+ # Dynamically discover all directories under prompts/
216
+ try :
217
+ all_dirs = [d for d in os .listdir (prompts_base_path )
218
+ if os .path .isdir (os .path .join (prompts_base_path , d ))]
219
+ except Exception as e :
220
+ return (False , f"Error scanning prompts directory: { str (e )} " )
221
+
222
+ if not all_dirs :
223
+ return (False , "No subdirectories found under prompts/" )
224
+
225
+ # Define which directory needs special GPT-specific handling
226
+ # If you need to change the behavior, you only need to change this constant
227
+ SPECIAL_DIR = "gpts"
228
+
229
+ # Track if special directory was found and processed
230
+ special_dir_processed = False
215
231
216
232
def collect_files_recursively (dir_path , base_path = None ):
217
233
"""
@@ -278,8 +294,16 @@ def collect_files_recursively(dir_path, base_path=None):
278
294
return result
279
295
280
296
def generate_gpts_toc (dir_path ):
281
- """Generate TOC.md for gpts directory using the original GPT-specific logic.
282
- The file is completely regenerated, not preserving any existing content."""
297
+ """
298
+ Generate TOC.md for gpts directory using the original GPT-specific logic.
299
+ The file is completely regenerated, not preserving any existing content.
300
+
301
+ Args:
302
+ dir_path: Path to the gpts directory
303
+
304
+ Returns:
305
+ A tuple (success, message) indicating success/failure and a descriptive message
306
+ """
283
307
toc_path = os .path .join (dir_path , TOC_FILENAME )
284
308
try :
285
309
with open (toc_path , 'w' , encoding = 'utf-8' ) as toc_file :
@@ -321,14 +345,15 @@ def gpts_sorter(key):
321
345
return (False , f"Error generating TOC.md for 'gpts': { str (e )} " )
322
346
323
347
# Process each top-level directory under prompts/
324
- for dirname in main_dirs :
348
+ for dirname in sorted ( all_dirs ): # Sort for consistent processing order
325
349
dir_path = os .path .join (prompts_base_path , dirname )
326
350
if not os .path .isdir (dir_path ):
327
351
messages .append (f"Directory '{ dirname } ' does not exist, skipping" )
328
352
continue
329
353
330
354
# For gpts directory, use the original GPT-specific logic
331
- if dirname == "gpts" :
355
+ if dirname == SPECIAL_DIR :
356
+ special_dir_processed = True
332
357
ok , msg = generate_gpts_toc (dir_path )
333
358
success = success and ok
334
359
messages .append (msg )
@@ -398,6 +423,10 @@ def gpts_sorter(key):
398
423
success = False
399
424
messages .append (f"Error generating TOC.md for '{ dirname } ': { str (e )} " )
400
425
426
+ # Warn if special directory was expected but not found
427
+ if not special_dir_processed and SPECIAL_DIR in all_dirs :
428
+ messages .append (f"Warning: Special directory '{ SPECIAL_DIR } ' was found but could not be processed" )
429
+
401
430
result_message = "\n " .join (messages )
402
431
return (success , result_message )
403
432
0 commit comments