Update Index from README #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Index from README | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'README.md' | |
| workflow_dispatch: | |
| workflow_run: | |
| workflows: ["Sync with Awesome Game Decompilations"] | |
| types: | |
| - completed | |
| jobs: | |
| update-index: | |
| runs-on: ubuntu-latest | |
| if: github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| - name: Run update script | |
| shell: python | |
| run: | | |
| import re | |
| import os | |
| from datetime import datetime | |
| readme_path = 'README.md' | |
| index_path = 'index.html' | |
| def get_ordinal_date(): | |
| now = datetime.now() | |
| day = now.day | |
| if 11 <= (day % 100) <= 13: | |
| suffix = 'th' | |
| else: | |
| suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th') | |
| return f"{now.strftime('%B')} {day}{suffix}, {now.year}" | |
| if os.path.exists(readme_path) and os.path.exists(index_path): | |
| with open(readme_path, 'r', encoding='utf-8') as f: | |
| readme_content = f.read() | |
| start_marker = "Like what you see? [⭐ The Github Repo!](https://github.com/SamidyFR/Game-Decompilations) it will help out a ton." | |
| if start_marker in readme_content: | |
| list_section = readme_content.split(start_marker)[1].strip() | |
| html_items = [] | |
| for line in list_section.split('\n'): | |
| line = line.strip() | |
| if line.startswith('- '): | |
| text = line[2:] | |
| # Convert Markdown links | |
| text = re.sub(r'\[(.+?)\]\(([^)]+)\)', r'<a href="\2">\1</a>', text) | |
| # Convert Markdown bold | |
| text = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', text) | |
| html_items.append(f'<li>{text}</li>') | |
| if html_items: | |
| with open(index_path, 'r', encoding='utf-8') as f: | |
| index_content = f.read() | |
| ul_pattern = r'(<ul[^>]*id="Game-Decomps"[^>]*>)(.*?)(</ul>)' | |
| new_inner_html = '\n' + '\n'.join(html_items) + '\n' | |
| new_index_content = re.sub(ul_pattern, lambda m: m.group(1) + new_inner_html + m.group(3), index_content, flags=re.DOTALL) | |
| # Update Last Updated Date | |
| current_date = get_ordinal_date() | |
| date_pattern = r'<li>Last Updated: .*?</li>' | |
| new_index_content = re.sub(date_pattern, f'<li>Last Updated: {current_date}</li>', new_index_content) | |
| with open(index_path, 'w', encoding='utf-8') as f: | |
| f.write(new_index_content) | |
| print(f"Updated index.html with {len(html_items)} items and date {current_date}.") | |
| else: | |
| print("No items found.") | |
| else: | |
| print("Start marker not found.") | |
| - name: Commit and push changes | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| git add index.html | |
| git commit -m "Auto-update index.html from README.md" || echo "No changes to commit" | |
| git push |