1616
1717import argparse
1818from bs4 import BeautifulSoup
19+ import pdb
1920import markdown2
2021import os .path
2122from io import open
2223import re
23- import sys
2424
25- def generate_overview (readme_file , version ):
25+ def generate_overview (readme_file , version , overview_file_path ):
2626
2727 readme_exists = False
2828 if os .path .exists (readme_file ) and os .path .isfile (readme_file ):
@@ -33,7 +33,10 @@ def generate_overview(readme_file, version):
3333 # allow processing to continue without failing the build the way a raise would.
3434 print ('{} does not exist' .format (readme_file ))
3535
36- html_overview_file = str (readme_file ).lower ().replace ('readme.md' , 'readme_overview.html' )
36+ if overview_file_path :
37+ html_overview_file = overview_file_path + 'readme_overview.html'
38+ else :
39+ html_overview_file = str (readme_file ).lower ().replace ('readme.md' , 'readme_overview.html' )
3740
3841 if (readme_exists ):
3942 with open (readme_file , 'r' , encoding = 'utf-8' ) as f :
@@ -45,27 +48,36 @@ def generate_overview(readme_file, version):
4548 # The toc helps the anchor link to jump to the right place.
4649 html_readme_content = markdown2 .markdown (re .sub (pattern = '@' , repl = '{@literal @}' , string = readme_content , flags = re .MULTILINE ), extras = ["fenced-code-blocks" , "target-blank-links" , "toc" ])
4750
51+ # Now use BeautifulSoup to cleanup the generated HTML so that it conforms to Javadoc compliance.
52+ soup = BeautifulSoup (html_readme_content , features = "html.parser" )
53+
54+ # Find all anchor tags with the rel attribute and remove the attribute.
55+ anchors_with_rel = soup .find_all (name = 'a' , attrs = {'rel' :'noopener' })
56+ for anchor in anchors_with_rel :
57+ del anchor ['rel' ]
58+
4859 # The html_readme_content needs to be encapsulated inside of <body> tags in order
4960 # for the content to correctly be added to the landing page
5061 with open (html_overview_file , 'w' , encoding = 'utf-8' ) as f :
5162 # The literal strings have to be unicode otherwise the write will fail.
5263 # This will allow this code to work for python 2 and 3
5364 f .write ('<body>' )
5465 f .write ('Current version is {}, click <a href="https://azure.github.io/azure-sdk-for-java" target="new">here</a> for the index' .format (version ))
55- f .write ('<br/ >' )
66+ f .write ('<br>' )
5667 if (readme_exists ):
57- f .write (str (html_readme_content ))
68+ f .write (str (soup . encode ( formatter = "html5" ). decode ( 'utf-8' ) ))
5869 f .write ('</body>' )
5970
6071
6172def main ():
6273 parser = argparse .ArgumentParser (description = 'Generate a readme_overview.html from a README.md.' )
6374 parser .add_argument ('--readme-file' , '--rf' , help = 'path to the README.md file to readme_generate the overview.html file from.' , required = True )
75+ parser .add_argument ('--overview-file-path' , '--ofp' , help = 'path to the overview.html file.' )
6476 parser .add_argument ('--version' , '--v' , help = 'Version, used on the landing page to identify the version.' , required = True )
6577 args = parser .parse_args ()
6678 # verify the argument is a readme.md file
6779 if str (args .readme_file ).lower ().endswith ('readme.md' ):
68- generate_overview (args .readme_file , args .version )
80+ generate_overview (args .readme_file , args .version , args . overview_file_path )
6981 else :
7082 raise ValueError ('{} is not a readmefile. The --readme-file argument must be a readme.md file.' .format (args .readme_file ))
7183
0 commit comments