55
66Import ("env" )
77
8- # Pfad relativ zur platformio.ini
98DIAG_DIR = join ("Elektronik" , "Wiring_Plan" )
109
1110def build_d2_diagrams (source , target , env ):
@@ -16,44 +15,43 @@ def build_d2_diagrams(source, target, env):
1615 print (f"\n [D2-INFO] Verzeichnis nicht gefunden: { abs_diag_dir } " )
1716 return
1817
19- print (f"\n [D2-INFO] Starte Export-Vorgang (SVG, PNG, PNG-Trans) ..." )
18+ print (f"\n [D2-INFO] Starte Export-Vorgang..." )
2019
2120 files_processed = 0
2221 for file in listdir (abs_diag_dir ):
2322 if file .endswith (".d2" ):
24- base_name = file .replace (".d2" , "" )
2523 input_file = join (abs_diag_dir , file )
24+ base_name = file .replace (".d2" , "" )
2625
27- # Export-Pfade
28- out_svg = join (abs_diag_dir , f"{ base_name } .svg" )
29- out_png = join (abs_diag_dir , f"{ base_name } .png" )
30- out_png_trans = join (abs_diag_dir , f"{ base_name } _transparent.png" )
26+ # Export-Formate
27+ formats = [
28+ {"ext" : "svg" , "args" : []},
29+ {"ext" : "png" , "args" : ["--theme" , "200" ]}, # Theme 200 ist meist neutral/hell
30+ {"ext" : "transparent.png" , "args" : ["--transparent" ]}
31+ ]
3132
32- try :
33- # 1. SVG (Standard)
34- subprocess .run (["d2" , input_file , out_svg ], check = True )
35-
36- # 2. PNG (Solid - nutzt Standard-Hintergrund des Themes, meist weiß)
37- # Wir erzwingen Theme 0 (Neutral), damit es sicher hell ist
38- subprocess .run (["d2" , "--theme" , "0" , input_file , out_png ], check = True )
33+ for fmt in formats :
34+ output_file = join (abs_diag_dir , f"{ base_name } .{ fmt ['ext' ]} " )
35+ cmd = ["d2" ] + fmt ["args" ] + [input_file , output_file ]
3936
40- # 3. PNG (Transparent)
41- subprocess .run (["d2" , "--transparent" , input_file , out_png_trans ], check = True )
42-
43- print (f" >> ERFOLG: { file } -> [SVG, PNG, PNG-Trans]" )
44- files_processed += 1
45- except Exception as e :
46- print (f" >> FEHLER beim Export von { file } : { e } " )
37+ try :
38+ # Capture_output=True erlaubt uns, die Fehlermeldung von D2 zu lesen
39+ result = subprocess .run (cmd , capture_output = True , text = True , check = True )
40+ except subprocess .CalledProcessError as e :
41+ print (f" >> FEHLER bei { file } ({ fmt ['ext' ]} ):" )
42+ print (f" Status: { e .returncode } " )
43+ print (f" Meldung: { e .stderr .strip ()} " )
44+ continue
45+
46+ print (f" >> ERFOLG: { file } verarbeitet." )
47+ files_processed += 1
4748
48- if files_processed > 0 :
49- print (f"[D2-INFO] Fertig. { files_processed } Datei(en) verarbeitet.\n " )
50- else :
51- print ("[D2-INFO] Keine Dateien erfolgreich verarbeitet.\n " )
49+ print (f"[D2-INFO] Fertig. { files_processed } Datei(en) verarbeitet.\n " )
5250
5351env .AddCustomTarget (
5452 name = "generate_docs" ,
5553 dependencies = None ,
5654 actions = [build_d2_diagrams ],
5755 title = "D2: Diagramme rendern" ,
58- description = "Erzeugt SVG und zwei PNG-Varianten"
59- )
56+ description = "Erzeugt SVG und PNG-Varianten"
57+ )
0 commit comments