55import argparse
66import time
77import re
8+ from string import Template
89
910import markdown
1011
1415 <head>
1516 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1617 <meta http-equiv="X-UA-Compatible" content="IE=edge">
17- <title>{title}</title>
18- <link href="https://stackpath.bootstrapcdn.com/bootswatch/4.4.1/slate/bootstrap.min.css" rel="stylesheet" integrity="sha384-G9YbB4o4U6WS4wCthMOpAeweY4gQJyyx0P3nZbEBHyz+AtNoeasfRChmek1C2iqV" crossorigin="anonymous">
18+ <title>$title</title>
19+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css" integrity="sha384-SI27wrMjH3ZZ89r4o+fGIJtnzkAnFs3E4qz9DIYioCQ5l9Rd/7UAa8DHcaL8jkWt" crossorigin="anonymous">
20+ <style>
21+ pre { background: #f5f5f5; border: 1px solid #ddd; border-radius: 4px; padding: 0.6em 1em; }
22+ h1,h2 { margin-top: 1em; }
23+ div.navbar { padding: 8px 0; }
24+ div.toc { float: right; }
25+ </style>
1926 </head>
2027 <body>
2128 <div class="container">
22- { html_content}
29+ $ html_content
2330 <hr/>
2431 <footer class="text-center text-muted">
25- Generated: { date}
32+ Generated: $ date
2633 </footer>
2734 <hr/>
2835 </div>
2936 </body>
3037</html>'''
3138
32-
3339extensions = ['toc' , 'codehilite' , 'meta' , 'fenced_code' , 'tables' ]
3440extension_configs = {
3541 'toc' : [('anchorlink' , True )],
@@ -51,18 +57,18 @@ def convert(infile, outdir):
5157 else :
5258 md_content_lines .append (line )
5359 md_content = '\n ' .join (md_content_lines )
54-
60+
5561 title_found = re .compile ("# *(.+)" ).findall (md_content )
5662 title = '' if not title_found else title_found [0 ]
57-
63+
5864 html_content = md .convert (md_content )
59-
60- html_doc = html_template . format (date = time .strftime ("%Y. %m. %d" ), title = title , html_content = html_content )
65+
66+ html_doc = Template ( html_template ). substitute (date = time .strftime ("%Y. %m. %d" ), title = title , html_content = html_content )
6167
6268 if not os .path .exists (outdir ):
6369 os .makedirs (outdir )
6470
65- outfile = os .path .join (outdir , os .path .splitext (os .path .basename (infile ))[0 ] + '.html' )
71+ outfile = os .path .join (outdir , 'index.html' if is_root_entry else ( os .path .splitext (os .path .basename (infile ))[0 ] + '.html' ) )
6672 with open (outfile , 'w' ) as html_file :
6773 html_file .write (html_doc )
6874
@@ -72,9 +78,10 @@ def main():
7278 parser .add_argument ("-i" , "--in-files" , nargs = '+' , help = '<Required> Markdown files to convert' , required = True )
7379 parser .add_argument ("-o" , "--out-dir" , help = '<Required> The directory to output converted html files' , required = True )
7480 args = parser .parse_args ()
75-
81+
7682 for in_file in args .in_files :
7783 convert (in_file , args .out_dir )
7884
7985if __name__ == "__main__" :
80- main ()
86+ main ()
87+
0 commit comments