2626def sanitize_filename (name : str ) -> str :
2727 """
2828 Sanitize icon name to create a valid filename.
29-
29+
3030 Args:
3131 name: Original icon name
32-
32+
3333 Returns:
3434 Sanitized filename safe for all platforms
3535 """
3636 # Replace spaces with hyphens
3737 filename = name .replace (' ' , '-' )
38-
38+
3939 # Remove or replace special characters
4040 filename = re .sub (r'[^\w\-.]' , '' , filename )
41-
41+
4242 # Remove multiple consecutive hyphens
4343 filename = re .sub (r'-+' , '-' , filename )
44-
44+
4545 # Remove leading/trailing hyphens
4646 filename = filename .strip ('-' )
47-
47+
4848 return filename
4949
5050
5151def find_library_file (directory : Path ) -> Path :
5252 """
5353 Find the .excalidrawlib file in the given directory.
54-
54+
5555 Args:
5656 directory: Directory to search
57-
57+
5858 Returns:
5959 Path to the library file
60-
60+
6161 Raises:
6262 SystemExit: If no library file or multiple library files found
6363 """
6464 library_files = list (directory .glob ('*.excalidrawlib' ))
65-
65+
6666 if len (library_files ) == 0 :
6767 print (f"Error: No .excalidrawlib file found in { directory } " )
68- print (f"Please place a .excalidrawlib file in this directory first." )
68+ print (f"Please place a .excalidrawlib file in { directory } first." )
6969 sys .exit (1 )
70-
70+
7171 if len (library_files ) > 1 :
7272 print (f"Error: Multiple .excalidrawlib files found in { directory } " )
73- print (f"Please keep only one library file in the directory." )
73+ print (f"Please keep only one library file in { directory } ." )
7474 sys .exit (1 )
75-
75+
7676 return library_files [0 ]
7777
7878
7979def split_library (library_dir : str ) -> None :
8080 """
8181 Split an Excalidraw library file into individual icon files.
82-
82+
8383 Args:
8484 library_dir: Path to the directory containing the .excalidrawlib file
8585 """
8686 library_dir = Path (library_dir )
87-
87+
8888 if not library_dir .exists ():
8989 print (f"Error: Directory not found: { library_dir } " )
9090 sys .exit (1 )
91-
91+
9292 if not library_dir .is_dir ():
9393 print (f"Error: Path is not a directory: { library_dir } " )
9494 sys .exit (1 )
95-
95+
9696 # Find the library file
9797 library_path = find_library_file (library_dir )
9898 print (f"Found library: { library_path .name } " )
99-
99+
100100 # Load library file
101101 print (f"Loading library data..." )
102102 with open (library_path , 'r' , encoding = 'utf-8' ) as f :
103103 library_data = json .load (f )
104-
104+
105105 # Validate library structure
106106 if 'libraryItems' not in library_data :
107107 print ("Error: Invalid library file format (missing 'libraryItems')" )
108108 sys .exit (1 )
109-
109+
110110 # Create icons directory
111111 icons_dir = library_dir / 'icons'
112112 icons_dir .mkdir (exist_ok = True )
113113 print (f"Output directory: { library_dir } " )
114-
114+
115115 # Process each library item (icon)
116116 library_items = library_data ['libraryItems' ]
117117 icon_list = []
118-
118+
119119 print (f"Processing { len (library_items )} icons..." )
120-
120+
121121 for item in library_items :
122122 # Get icon name
123123 icon_name = item .get ('name' , 'Unnamed' )
124-
124+
125125 # Create sanitized filename
126126 filename = sanitize_filename (icon_name ) + '.json'
127-
127+
128128 # Save icon data
129129 icon_path = icons_dir / filename
130130 with open (icon_path , 'w' , encoding = 'utf-8' ) as f :
131131 json .dump (item , f , ensure_ascii = False , indent = 2 )
132-
132+
133133 # Add to reference list
134134 icon_list .append ({
135135 'name' : icon_name ,
136136 'filename' : filename
137137 })
138-
138+
139139 print (f" ✓ { icon_name } → { filename } " )
140-
140+
141141 # Sort icon list by name
142142 icon_list .sort (key = lambda x : x ['name' ])
143-
143+
144144 # Generate reference.md
145145 library_name = library_path .stem
146146 reference_path = library_dir / 'reference.md'
@@ -150,14 +150,14 @@ def split_library(library_dir: str) -> None:
150150 f .write ("## Available Icons\n \n " )
151151 f .write ("| Icon Name | Filename |\n " )
152152 f .write ("|-----------|----------|\n " )
153-
153+
154154 for icon in icon_list :
155155 f .write (f"| { icon ['name' ]} | `icons/{ icon ['filename' ]} ` |\n " )
156-
156+
157157 f .write ("\n ## Usage\n \n " )
158158 f .write ("Each icon JSON file contains the complete `elements` array needed to render that icon in Excalidraw.\n " )
159159 f .write ("You can copy the elements from these files into your Excalidraw diagrams.\n " )
160-
160+
161161 print (f"\n ✅ Successfully split library into { len (icon_list )} icons" )
162162 print (f"📄 Reference file created: { reference_path } " )
163163 print (f"📁 Icons directory: { icons_dir } " )
@@ -174,7 +174,7 @@ def main():
174174 print (" python split-excalidraw-library.py skills/excalidraw-diagram-generator/libraries/aws-architecture-icons/" )
175175 print ("\n Note: The directory should contain a .excalidrawlib file." )
176176 sys .exit (1 )
177-
177+
178178 library_dir = sys .argv [1 ]
179179 split_library (library_dir )
180180
0 commit comments