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