refactor(generate-contributors): Update prompt blocks in PROMPTS.md efficiently

This commit is contained in:
Fatih Kadir Akın
2025-12-24 11:39:45 +03:00
parent b205591f4d
commit 7d256f9161

View File

@@ -151,48 +151,92 @@ def format_contributor_links(contributor_field):
return ', '.join([f'[@{c}](https://github.com/{c})' for c in contributors])
def generate_prompts_md(prompts_dict, prompts_order, prompts_md_path):
"""Generate PROMPTS.md from current prompts dictionary"""
def generate_prompt_block(row):
"""Generate a single prompt's <details> block"""
act = row.get('act', 'Untitled')
prompt = row.get('prompt', '')
contributor = row.get('contributor', '')
prompt_type = row.get('type', 'TEXT').upper()
# Determine code block language based on type
if prompt_type == 'TEXT':
lang = 'md'
elif prompt_type == 'JSON':
lang = 'json'
elif prompt_type == 'YAML':
lang = 'yaml'
else:
lang = 'md'
contributor_links = format_contributor_links(contributor)
block = f'<details>\n'
block += f'<summary><strong>{act}</strong></summary>\n\n'
block += f'## {act}\n\n'
block += f'Contributed by {contributor_links}\n\n'
block += f'```{lang}\n'
block += f'{prompt}\n'
block += f'```\n\n'
block += f'</details>\n\n'
return block
def init_prompts_md(prompts_md_path):
"""Initialize PROMPTS.md with header if it doesn't exist"""
if not os.path.exists(prompts_md_path):
with open(prompts_md_path, 'w', encoding='utf-8') as f:
f.write('# Awesome ChatGPT Prompts\n\n')
f.write('> A curated list of prompts for ChatGPT and other AI models.\n\n')
f.write('---\n\n')
def append_prompt_to_md(row, prompts_md_path):
"""Append a new prompt block to PROMPTS.md"""
init_prompts_md(prompts_md_path)
block = generate_prompt_block(row)
with open(prompts_md_path, 'a', encoding='utf-8') as f:
f.write(block)
def update_prompt_in_md(row, prompts_md_path):
"""Update an existing prompt's block in PROMPTS.md"""
act = row.get('act', '')
if not os.path.exists(prompts_md_path):
append_prompt_to_md(row, prompts_md_path)
return
with open(prompts_md_path, 'r', encoding='utf-8') as f:
content = f.read()
# Find and replace the specific prompt block using regex
import re
# Pattern to match the entire <details> block for this prompt
pattern = rf'<details>\n<summary><strong>{re.escape(act)}</strong></summary>.*?</details>\n\n'
new_block = generate_prompt_block(row)
new_content, count = re.subn(pattern, new_block, content, flags=re.DOTALL)
if count > 0:
with open(prompts_md_path, 'w', encoding='utf-8') as f:
f.write(new_content)
else:
# Prompt not found, append it
append_prompt_to_md(row, prompts_md_path)
def remove_prompt_from_md(act, prompts_md_path):
"""Remove a prompt's block from PROMPTS.md"""
if not os.path.exists(prompts_md_path):
return
with open(prompts_md_path, 'r', encoding='utf-8') as f:
content = f.read()
import re
pattern = rf'<details>\n<summary><strong>{re.escape(act)}</strong></summary>.*?</details>\n\n'
new_content = re.sub(pattern, '', content, flags=re.DOTALL)
with open(prompts_md_path, 'w', encoding='utf-8') as f:
f.write('# Awesome ChatGPT Prompts\n\n')
f.write('> A curated list of prompts for ChatGPT and other AI models.\n\n')
f.write('---\n\n')
# Write prompts in order
for act in prompts_order:
if act not in prompts_dict:
continue
row = prompts_dict[act]
prompt = row.get('prompt', '')
contributor = row.get('contributor', '')
prompt_type = row.get('type', 'TEXT').upper()
# Determine code block language based on type
if prompt_type == 'TEXT':
lang = 'md'
elif prompt_type == 'JSON':
lang = 'json'
elif prompt_type == 'YAML':
lang = 'yaml'
else:
lang = 'md'
contributor_links = format_contributor_links(contributor)
f.write(f'<details>\n')
f.write(f'<summary><strong>{act}</strong></summary>\n\n')
f.write(f'## {act}\n\n')
f.write(f'Contributed by {contributor_links}\n\n')
f.write(f'```{lang}\n')
f.write(f'{prompt}\n')
f.write(f'```\n\n')
f.write(f'</details>\n\n')
f.write(new_content)
prompts_md_path = os.path.join(project_dir, 'PROMPTS.md')
# Build prompts order from remote (for consistent ordering)
prompts_order = [row.get('act', '').strip() for row in remote_prompts if row.get('act', '').strip()]
if not new_prompts and not updated_prompts and not deleted_prompts:
print("\nNo CSV changes detected. Already up to date!")
else:
@@ -217,8 +261,8 @@ else:
if row_act in local_prompts:
writer.writerow(local_prompts[row_act])
# Regenerate PROMPTS.md with this update
generate_prompts_md(local_prompts, prompts_order, prompts_md_path)
# Update only this prompt's block in PROMPTS.md
update_prompt_in_md(remote_row, prompts_md_path)
primary_author, co_authors = parse_contributors(contributor_field)
email = f"{primary_author}@users.noreply.github.com"
@@ -265,11 +309,11 @@ else:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writerow(row)
# Track in local_prompts for PROMPTS.md generation
# Track in local_prompts
local_prompts[act] = row
# Regenerate PROMPTS.md with this new prompt
generate_prompts_md(local_prompts, prompts_order, prompts_md_path)
# Append only this prompt's block to PROMPTS.md
append_prompt_to_md(row, prompts_md_path)
# Stage and commit
subprocess.run(['git', 'add', csv_file, prompts_md_path], check=True)
@@ -319,8 +363,8 @@ else:
for remaining_act, remaining_row in local_prompts.items():
writer.writerow(remaining_row)
# Regenerate PROMPTS.md without this prompt
generate_prompts_md(local_prompts, prompts_order, prompts_md_path)
# Remove only this prompt's block from PROMPTS.md
remove_prompt_from_md(act, prompts_md_path)
# Stage and commit
subprocess.run(['git', 'add', csv_file, prompts_md_path], check=True)