docs: update TOC generation workflow and enhance script for dynamic directory handling

This commit is contained in:
LouisShark 2025-03-11 16:16:23 +08:00
parent 44af260493
commit f568e7aa17
5 changed files with 62 additions and 48 deletions

View file

@ -1,5 +1,9 @@
name: Generate TOC on PR Merge or Push name: Generate TOC on PR Merge or Push
# This workflow automatically updates the TOC.md files in the repository
# whenever there's a PR merge or direct push to the main branch.
# It ensures that the table of contents stays up-to-date without manual intervention.
on: on:
# Trigger on PR merge # Trigger on PR merge
pull_request_target: pull_request_target:
@ -13,18 +17,27 @@ on:
- main - main
jobs: jobs:
build_toc_on_pr_merge: build_toc:
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true # Only run on PR merge or direct push to main
if: github.event_name == 'push' || (github.event_name == 'pull_request_target' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v3 uses: actions/checkout@v3
with:
# Fetch all history for allowing proper git operations
fetch-depth: 0
- name: Set up Python 3 - name: Set up Python 3
uses: actions/setup-python@v3 uses: actions/setup-python@v3
with: with:
python-version: '3.x' python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f .scripts/requirements.txt ]; then pip install -r .scripts/requirements.txt; fi
- name: Run TOC generation script - name: Run TOC generation script
run: | run: |
chmod +x .scripts/idxtool.py chmod +x .scripts/idxtool.py
@ -34,37 +47,9 @@ jobs:
run: | run: |
git config --global user.name 'LouisShark' git config --global user.name 'LouisShark'
git config --global user.email 'mshark.louis@gmail.com' git config --global user.email 'mshark.louis@gmail.com'
git add . git add TOC.md prompts/*/TOC.md
git commit -m "docs: Update TOC.md" || echo "No changes to commit" git commit -m "docs: Update TOC.md files" || echo "No changes to commit"
git pull --rebase git pull --rebase origin main
git push origin HEAD:main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build_toc_on_push:
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python 3
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Run TOC generation script
run: |
chmod +x .scripts/idxtool.py
python3 .scripts/idxtool.py --toc
- name: Commit TOC updates
run: |
git config --global user.name 'LouisShark'
git config --global user.email 'mshark.louis@gmail.com'
git add .
git commit -m "docs: Update TOC.md" || echo "No changes to commit"
git pull --rebase
git push origin HEAD:main git push origin HEAD:main
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View file

@ -96,7 +96,7 @@ def rebuild_toc(toc_out: str = '') -> Tuple[bool, str]:
# Write a header for the TOC file # Write a header for the TOC file
out = [] out = []
out.append("# ChatGPT System Prompts - Table of Contents\n\n") out.append("# ChatGPT System Prompts \n\n")
out.append("This document contains a table of contents for the ChatGPT System Prompts repository.\n\n") out.append("This document contains a table of contents for the ChatGPT System Prompts repository.\n\n")
# Add links to TOC.md files in prompts directory subdirectories # Add links to TOC.md files in prompts directory subdirectories
@ -197,21 +197,37 @@ def find_gptfile(keyword, verbose=True):
def generate_toc_for_prompts_dirs() -> Tuple[bool, str]: def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
""" """
Generates a single TOC.md file for each of the three main directories under prompts: Generates a single TOC.md file for each directory under prompts:
gpts, official-product, and opensource-prj. - For the gpts directory, uses the original GPT-specific TOC generation logic.
For gpts directory, uses the original GPT-specific TOC generation logic. - For all other directories (including newly added ones), uses the generic recursive logic.
For other directories, includes all markdown files in the directory and its subdirectories.
This function automatically detects all subdirectories under prompts, ensuring future-proof
extensibility without requiring code changes when new directories are added.
""" """
prompts_base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'prompts')) prompts_base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'prompts'))
if not os.path.exists(prompts_base_path): if not os.path.exists(prompts_base_path):
return (False, f"Prompts directory '{prompts_base_path}' does not exist.") return (False, f"Prompts directory '{prompts_base_path}' does not exist.")
print(f"Generating TOC.md files for main directories under '{prompts_base_path}'") print(f"Generating TOC.md files for all directories under '{prompts_base_path}'")
success = True success = True
messages = [] messages = []
# Main directories we want to process # Dynamically discover all directories under prompts/
main_dirs = ["gpts", "official-product", "opensource-prj"] try:
all_dirs = [d for d in os.listdir(prompts_base_path)
if os.path.isdir(os.path.join(prompts_base_path, d))]
except Exception as e:
return (False, f"Error scanning prompts directory: {str(e)}")
if not all_dirs:
return (False, "No subdirectories found under prompts/")
# Define which directory needs special GPT-specific handling
# If you need to change the behavior, you only need to change this constant
SPECIAL_DIR = "gpts"
# Track if special directory was found and processed
special_dir_processed = False
def collect_files_recursively(dir_path, base_path=None): def collect_files_recursively(dir_path, base_path=None):
""" """
@ -278,8 +294,16 @@ def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
return result return result
def generate_gpts_toc(dir_path): def generate_gpts_toc(dir_path):
"""Generate TOC.md for gpts directory using the original GPT-specific logic. """
The file is completely regenerated, not preserving any existing content.""" Generate TOC.md for gpts directory using the original GPT-specific logic.
The file is completely regenerated, not preserving any existing content.
Args:
dir_path: Path to the gpts directory
Returns:
A tuple (success, message) indicating success/failure and a descriptive message
"""
toc_path = os.path.join(dir_path, TOC_FILENAME) toc_path = os.path.join(dir_path, TOC_FILENAME)
try: try:
with open(toc_path, 'w', encoding='utf-8') as toc_file: with open(toc_path, 'w', encoding='utf-8') as toc_file:
@ -321,14 +345,15 @@ def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
return (False, f"Error generating TOC.md for 'gpts': {str(e)}") return (False, f"Error generating TOC.md for 'gpts': {str(e)}")
# Process each top-level directory under prompts/ # Process each top-level directory under prompts/
for dirname in main_dirs: for dirname in sorted(all_dirs): # Sort for consistent processing order
dir_path = os.path.join(prompts_base_path, dirname) dir_path = os.path.join(prompts_base_path, dirname)
if not os.path.isdir(dir_path): if not os.path.isdir(dir_path):
messages.append(f"Directory '{dirname}' does not exist, skipping") messages.append(f"Directory '{dirname}' does not exist, skipping")
continue continue
# For gpts directory, use the original GPT-specific logic # For gpts directory, use the original GPT-specific logic
if dirname == "gpts": if dirname == SPECIAL_DIR:
special_dir_processed = True
ok, msg = generate_gpts_toc(dir_path) ok, msg = generate_gpts_toc(dir_path)
success = success and ok success = success and ok
messages.append(msg) messages.append(msg)
@ -398,6 +423,10 @@ def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
success = False success = False
messages.append(f"Error generating TOC.md for '{dirname}': {str(e)}") messages.append(f"Error generating TOC.md for '{dirname}': {str(e)}")
# Warn if special directory was expected but not found
if not special_dir_processed and SPECIAL_DIR in all_dirs:
messages.append(f"Warning: Special directory '{SPECIAL_DIR}' was found but could not be processed")
result_message = "\n".join(messages) result_message = "\n".join(messages)
return (success, result_message) return (success, result_message)

View file

@ -1,4 +1,4 @@
# gpts # gpts - Table of Contents
## GPTs (1107 total) ## GPTs (1107 total)

View file

@ -1,4 +1,4 @@
# official-product # official-product - Table of Contents
## Subdirectories ## Subdirectories

View file

@ -1,4 +1,4 @@
# opensource-prj # opensource-prj - Table of Contents
- [Claude_Sentience](./Claude_Sentience.md) - [Claude_Sentience](./Claude_Sentience.md)
- [RestGPT](./RestGPT.md) - [RestGPT](./RestGPT.md)