88import  re 
99import  logging 
1010import  os 
11+ import  subprocess 
1112from  textwrap  import  dedent 
1213
1314
@@ -195,15 +196,9 @@ def get_release_info(data, filename="rel-notes.md"):
195196        "无更新类型" : "" ,
196197    }
197198
198-     # 定义模板 
199199    header_template  =  Template (
200200        dedent (
201201            """\  
202- 
203-             hide: 
204-             -  toc 
205-             --- 
206- 
207202
208203
209204            本页列出 d.run 各项功能的一些重要变更。 
@@ -217,56 +212,89 @@ def get_release_info(data, filename="rel-notes.md"):
217212    entry_with_baseline_template  =  Template ("- [$primary_func] $baseline" )
218213    entry_without_baseline_template  =  Template ("- [$primary_func]" )
219214
220-     def  ensure_blank_line (lines_list ):
215+     def  emit (lines_list , line = "" ):
216+         """Add a line to the list, stripping trailing whitespace""" 
217+         if  line :
218+             lines_list .append (line .rstrip ())
219+ 
220+     def  ensure_single_blank_line (lines_list ):
221+         """Ensure there's exactly one blank line before adding new content""" 
221222        if  lines_list  and  lines_list [- 1 ] !=  "" :
222223            lines_list .append ("" )
224+         elif  len (lines_list ) >  1  and  lines_list [- 1 ] ==  ""  and  lines_list [- 2 ] ==  "" :
225+             lines_list .pop ()
223226
224227    lines  =  header_template .substitute ().splitlines ()
225-     ensure_blank_line (lines )
226228
227229    for  pub_date , modules  in  result .items ():
228-         ensure_blank_line (lines )
229-         lines .append (pub_date_template .substitute (pub_date = pub_date ))
230-         ensure_blank_line (lines )
230+         ensure_single_blank_line (lines )
231+         emit (lines , pub_date_template .substitute (pub_date = pub_date ))
231232        for  module , versions  in  modules .items ():
232233            for  version , update_types  in  versions .items ():
233-                 lines .append (
234-                     module_version_template .substitute (module = module , version = version )
234+                 ensure_single_blank_line (lines )
235+                 emit (
236+                     lines ,
237+                     module_version_template .substitute (module = module , version = version ),
235238                )
236-                 ensure_blank_line (lines )
237239                for  update_type  in  ["新功能" , "增强优化" , "故障修复" , "无更新类型" ]:
238-                     if  update_type  in  update_types :
239-                         entries  =  update_types [update_type ]
240-                         if  update_type  !=  "无更新类型" :
241-                             lines .append (
242-                                 update_type_template .substitute (
243-                                     emoji_title = emoji_map [update_type ]
244-                                 )
240+                     if  update_type  not  in update_types :
241+                         continue 
242+                     entries  =  update_types [update_type ]
243+                     if  not  entries :
244+                         continue 
245+                     if  update_type  !=  "无更新类型" :
246+                         ensure_single_blank_line (lines )
247+                         emit (
248+                             lines ,
249+                             update_type_template .substitute (
250+                                 emoji_title = emoji_map [update_type ]
251+                             ),
252+                         )
253+                         ensure_single_blank_line (lines )
254+                     for  primary_func , entry  in  entries :
255+                         baseline  =  entry .get ("基线参数" , "" )
256+                         if  baseline :
257+                             emit (
258+                                 lines ,
259+                                 entry_with_baseline_template .substitute (
260+                                     primary_func = primary_func , baseline = baseline 
261+                                 ),
262+                             )
263+                         else :
264+                             emit (
265+                                 lines ,
266+                                 entry_without_baseline_template .substitute (
267+                                     primary_func = primary_func 
268+                                 ),
245269                            )
246-                             ensure_blank_line (lines )
247-                         for  primary_func , entry  in  entries :
248-                             baseline  =  entry .get ("基线参数" , "" )
249-                             if  baseline :
250-                                 lines .append (
251-                                     entry_with_baseline_template .substitute (
252-                                         primary_func = primary_func , baseline = baseline 
253-                                     )
254-                                 )
255-                             else :
256-                                 lines .append (
257-                                     entry_without_baseline_template .substitute (
258-                                         primary_func = primary_func 
259-                                     )
260-                                 )
261-                         ensure_blank_line (lines )
262-     if  lines  and  lines [- 1 ] ==  "" :
270+ 
271+     while  lines  and  lines [- 1 ] ==  "" :
263272        lines .pop ()
264273
265274    md_content  =  "\n " .join (lines ) +  "\n " 
266275    try :
267276        with  open (filename , "w" , encoding = "utf-8" ) as  f :
268277            f .write (md_content )
269278        logging .info ("Release notes已保存到 %s" , filename )
279+         
280+         # Format Markdown file using mdformat 
281+         try :
282+             result  =  subprocess .run (
283+                 ["mdformat" , filename ],
284+                 capture_output = True ,
285+                 text = True ,
286+                 timeout = 30 ,
287+             )
288+             if  result .returncode  ==  0 :
289+                 logging .info ("✅ Markdown文件已格式化" )
290+             else :
291+                 logging .warning ("Markdown格式化警告: %s" , result .stderr )
292+         except  FileNotFoundError :
293+             logging .warning ("mdformat未安装,跳过格式化步骤。可通过 'pip install mdformat' 安装" )
294+         except  subprocess .TimeoutExpired :
295+             logging .warning ("Markdown格式化超时,跳过格式化步骤" )
296+         except  Exception  as  e :
297+             logging .warning ("Markdown格式化失败: %s" , e )
270298    except  Exception  as  e :
271299        logging .error ("写入release notes文件失败: %s" , e )
272300
0 commit comments