Skip to content

Commit 54cc058

Browse files
committed
new action sort
1 parent f8e675f commit 54cc058

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Sort Mods Table
2+
3+
on:
4+
push:
5+
paths:
6+
- 'addedMods.md'
7+
pull_request:
8+
paths:
9+
- 'addedMods.md'
10+
11+
jobs:
12+
sort-table:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: '3.x'
23+
24+
- name: Sort mods table
25+
run: |
26+
python .github/workflows/sort_mods_table.py
27+
28+
- name: Check for changes
29+
id: git-check
30+
run: |
31+
git diff --exit-code || echo "changes=true" >> $GITHUB_OUTPUT
32+
33+
- name: Commit changes
34+
if: steps.git-check.outputs.changes == 'true'
35+
run: |
36+
git config --global user.name 'GitHub Action'
37+
git config --global user.email '[email protected]'
38+
git add addedMods.md
39+
git commit -m "Sort mods table alphabetically [skip ci]"
40+
git push

.github/workflows/sort-mods-table.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import re
2+
import os
3+
4+
def sort_markdown_table(file_path):
5+
# Read the file content
6+
with open(file_path, 'r', encoding='utf-8') as file:
7+
content = file.read()
8+
9+
# Find the table by splitting at "## Mod List"
10+
parts = content.split("## Mod List")
11+
if len(parts) < 2:
12+
print("Table not found in the format expected")
13+
return False
14+
15+
# Keep the header and everything before the table
16+
header = parts[0] + "## Mod List\n"
17+
18+
# Split the rest at the first occurrence of "---" after the table
19+
table_and_rest = parts[1].split("\n---", 1)
20+
21+
# Extract the table lines (everything up to the "---")
22+
table_text = table_and_rest[0].strip()
23+
table_lines = table_text.split('\n')
24+
25+
# The first two lines are the header and separator
26+
header_rows = table_lines[0:2]
27+
content_rows = table_lines[2:] if len(table_lines) > 2 else []
28+
29+
# Filter out any rows that don't have proper table formatting
30+
valid_content_rows = []
31+
for i, row in enumerate(content_rows):
32+
if row.strip() and row.count('|') >= 3: # Need at least 3 pipe characters for a valid row
33+
valid_content_rows.append(row)
34+
else:
35+
print(f"Skipping invalid row {i+3}: '{row}'")
36+
37+
# Sort content rows alphabetically by mod name (first column)
38+
# Extract the actual mod name, removing Markdown formatting and ignoring comments
39+
def get_sort_key(row):
40+
# Get the mod name cell (second column)
41+
cells = row.split('|')
42+
if len(cells) <= 1:
43+
return ""
44+
45+
mod_name_cell = cells[1].strip()
46+
47+
# Extract the name from markdown link if present
48+
mod_name = re.sub(r'\[([^]]+)\].*', r'\1', mod_name_cell)
49+
50+
# Handle special prefix cases like "[Let's Do]"
51+
if mod_name.startswith('[') and 'Let\'s Do' in mod_name:
52+
# Remove the prefix for sorting purposes
53+
mod_name = mod_name.replace('[Let\'s Do]', '').strip()
54+
55+
# Handle "The" prefix
56+
if mod_name.startswith('The '):
57+
mod_name = mod_name[4:]
58+
59+
return mod_name.lower()
60+
61+
sorted_rows = sorted(valid_content_rows, key=get_sort_key)
62+
63+
# Reconstruct the table
64+
sorted_table = '\n'.join(header_rows + sorted_rows)
65+
66+
# Reconstruct the full content
67+
if len(table_and_rest) > 1:
68+
full_content = header + sorted_table + "\n---" + table_and_rest[1]
69+
else:
70+
full_content = header + sorted_table
71+
72+
# Write back to file directly
73+
with open(file_path, 'w', encoding='utf-8') as file:
74+
file.write(full_content)
75+
76+
print("Table sorted successfully.")
77+
return True
78+
79+
if __name__ == "__main__":
80+
# Get the repository root directory
81+
repo_root = os.environ.get('GITHUB_WORKSPACE', '.')
82+
file_path = os.path.join(repo_root, 'addedMods.md')
83+
sort_markdown_table(file_path)

0 commit comments

Comments
 (0)