@@ -48,11 +48,13 @@ def gather_metadata(directory, id_key, component_type):
4848def generate_cross_references ():
4949 tests = gather_metadata ("MASTG/tests" , "id" , "TEST" )
5050 demos = gather_metadata ("MASTG/demos" , "id" , "DEMO" )
51+ best_practices_metadata = gather_metadata ("MASTG/best-practices" , "id" , "BEST" )
5152
5253 cross_references = {
5354 "weaknesses" : {},
5455 "tests" : {},
55- "best-practices" : {}
56+ "best-practices" : {},
57+ "weakness-to-best-practices" : {}
5658 }
5759
5860 for test_id , test_meta in tests .items ():
@@ -67,6 +69,25 @@ def generate_cross_references():
6769 if weakness_id not in cross_references ["weaknesses" ]:
6870 cross_references ["weaknesses" ][weakness_id ] = []
6971 cross_references ["weaknesses" ][weakness_id ].append ({"id" : test_id , "path" : test_path , "title" : test_title , "platform" : test_platform })
72+
73+ # Create cross-references for weaknesses to best-practices
74+ # Collect best-practices from tests that reference each weakness
75+ if best_practices_ids :
76+ if weakness_id not in cross_references ["weakness-to-best-practices" ]:
77+ cross_references ["weakness-to-best-practices" ][weakness_id ] = {}
78+ for best_practice_id in best_practices_ids :
79+ if best_practice_id not in cross_references ["weakness-to-best-practices" ][weakness_id ]:
80+ # Get the best-practice metadata if available
81+ best_practice_meta = best_practices_metadata .get (best_practice_id , {})
82+ best_practice_path = best_practice_meta .get ("path" , f"MASTG/best-practices/{ best_practice_id } .md" )
83+ best_practice_title = best_practice_meta .get ("title" , best_practice_id )
84+ best_practice_platform = best_practice_meta .get ("platform" , test_platform )
85+ cross_references ["weakness-to-best-practices" ][weakness_id ][best_practice_id ] = {
86+ "id" : best_practice_id ,
87+ "path" : best_practice_path ,
88+ "title" : best_practice_title ,
89+ "platform" : best_practice_platform
90+ }
7091
7192 # Create cross-references for best_practices listing all tests that reference each best_practice ID
7293 if best_practices_ids :
@@ -126,6 +147,19 @@ def on_page_markdown(markdown, page, config, **kwargs):
126147 relPath = os .path .relpath (test ['path' ], os .path .dirname (path ))
127148 tests_section += f"[{ get_platform_icon (test ['platform' ])} { test ['id' ]} : { test ['title' ]} ]({ relPath } ){{: .mas-test-button}} "
128149 markdown += f"\n \n { tests_section } "
150+
151+ # Add Best Practices section to weaknesses as buttons
152+ # ORIGIN: Cross-references from this script (collected from tests that reference this weakness)
153+
154+ if weakness_id in cross_references ["weakness-to-best-practices" ]:
155+ best_practices = cross_references ["weakness-to-best-practices" ][weakness_id ]
156+ meta ['best-practices' ] = list (best_practices .values ())
157+ if best_practices :
158+ best_practices_section = "## Best Practices\n \n "
159+ for best_practice_id , best_practice in best_practices .items ():
160+ relPath = os .path .relpath (best_practice ['path' ], os .path .dirname (path ))
161+ best_practices_section += f"[{ get_platform_icon (best_practice ['platform' ])} { best_practice ['id' ]} : { best_practice ['title' ]} ]({ relPath } ){{: .mas-best-button}} "
162+ markdown += f"\n \n { best_practices_section } "
129163
130164 if "MASTG-TEST-" in filename :
131165
0 commit comments