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
# 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:
# Trigger on PR merge
pull_request_target:
@ -13,18 +17,27 @@ on:
- main
jobs:
build_toc_on_pr_merge:
if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == true
build_toc:
# 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
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
# Fetch all history for allowing proper git operations
fetch-depth: 0
- name: Set up Python 3
uses: actions/setup-python@v3
with:
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
run: |
chmod +x .scripts/idxtool.py
@ -34,37 +47,9 @@ jobs:
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
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 add TOC.md prompts/*/TOC.md
git commit -m "docs: Update TOC.md files" || echo "No changes to commit"
git pull --rebase origin main
git push origin HEAD:main
env:
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
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")
# 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]:
"""
Generates a single TOC.md file for each of the three main directories under prompts:
gpts, official-product, and opensource-prj.
For gpts directory, uses the original GPT-specific TOC generation logic.
For other directories, includes all markdown files in the directory and its subdirectories.
Generates a single TOC.md file for each directory under prompts:
- For the gpts directory, uses the original GPT-specific TOC generation logic.
- For all other directories (including newly added ones), uses the generic recursive logic.
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'))
if not os.path.exists(prompts_base_path):
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
messages = []
# Main directories we want to process
main_dirs = ["gpts", "official-product", "opensource-prj"]
# Dynamically discover all directories under prompts/
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):
"""
@ -278,8 +294,16 @@ def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
return result
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)
try:
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)}")
# 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)
if not os.path.isdir(dir_path):
messages.append(f"Directory '{dirname}' does not exist, skipping")
continue
# 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)
success = success and ok
messages.append(msg)
@ -398,6 +423,10 @@ def generate_toc_for_prompts_dirs() -> Tuple[bool, str]:
success = False
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)
return (success, result_message)

View file

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

View file

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

View file

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