22import re
33import math
44
5- # Function to find .yaml files recursively
5+ # Function to find .yaml files recursively in all directories under templates
66def find_yaml_files (root_dir ):
77 yaml_files = []
88 for dirpath , _ , filenames in os .walk (root_dir ):
@@ -11,36 +11,40 @@ def find_yaml_files(root_dir):
1111 yaml_files .append (os .path .join (dirpath , filename ))
1212 return yaml_files
1313
14- # Function to update README.md with an HTML table (5 columns, no .yaml extension )
15- def update_readme (yaml_files ):
14+ # Function to update README.md with an HTML table (5 columns, hyperlinks with directory structure )
15+ def update_readme (yaml_files , root_dir ):
1616 readme_file = 'README.md'
17+ github_base_url = "https://github.com/OWASP/www-project-asvs-security-evaluation-templates-with-nuclei/blob/dev/"
18+
1719 try :
1820 with open (readme_file , 'r' , encoding = 'utf-8' ) as file :
1921 readme_content = file .read ()
2022
21- # Remove .yaml extension and sort filenames
22- yaml_filenames = sorted (set (os .path .splitext (os .path .basename (f ))[0 ] for f in yaml_files ))
23-
2423 # Create a table with 5 columns
2524 table_rows = ""
26- num_files = len (yaml_filenames )
25+ num_files = len (yaml_files )
2726 num_columns = 5
28- num_rows = math .ceil (num_files / num_columns )
27+ num_rows = math .ceil (num_files / num_columns )
2928
3029 for i in range (num_rows ):
31- row_files = yaml_filenames [i * num_columns :(i + 1 ) * num_columns ]
32- table_rows += "<tr>" + "" .join (f"<td>{ file } </td>" for file in row_files ) + "</tr>\n "
30+ row_files = yaml_files [i * num_columns :(i + 1 ) * num_columns ]
31+ table_rows += "<tr>" + "" .join (
32+ '<td><a href="{}{}">{}</a></td>' .format (
33+ github_base_url ,
34+ os .path .relpath (file , root_dir ).replace (os .sep , '/' ),
35+ os .path .splitext (os .path .basename (file ))[0 ]
36+ ) for file in row_files
37+ ) + "</tr>\n "
3338
34- table_html = f""" <h2 align="center">Available Templates</h2>
39+ table_html = f''' <h2 align="center">Available Templates</h2>
3540<table border="1" cellpadding="5" cellspacing="0" align="center">
3641 { table_rows }
3742</table>
3843</center>
39- """
44+ '''
4045
4146 if "<h2 align=\" center\" >Available Templates</h2>" in readme_content :
4247 h2_index = readme_content .index ("<h2 align=\" center\" >Available Templates</h2>" )
43-
4448 readme_content = readme_content [:h2_index ]
4549
4650 readme_content += f'{ table_html } '
@@ -55,9 +59,9 @@ def update_readme(yaml_files):
5559 print (f"An error occurred: { e } " )
5660
5761if __name__ == '__main__' :
58- root_dir = '../../ '
62+ root_dir = 'templates '
5963 yaml_files = find_yaml_files (root_dir )
6064 if yaml_files :
61- update_readme (yaml_files )
65+ update_readme (yaml_files , root_dir )
6266 else :
6367 print ("No matching YAML files found." )
0 commit comments