55import argparse
66from datetime import datetime
77
8+ import frontmatter
9+ from slugify import slugify
10+
811from util import par_dir , mkdir_p
912from leetcode import Leetcode
1013from ojhtml2markdown import problem2md
@@ -20,6 +23,8 @@ def curr_time():
2023 parser = argparse .ArgumentParser (description = 'Helper for GitBook algorithm' )
2124 parser .add_argument ('--new' , type = str , dest = 'new' ,
2225 help = 'create new post with given leetcode/lintcode url.' )
26+ parser .add_argument ('--dir' , type = str , dest = 'dir' ,
27+ help = 'create md under dir.' )
2328 parser .add_argument ('--update' , nargs = '*' , dest = 'update' ,
2429 help = 'update post with given title in post and summary.' )
2530 parser .add_argument ('--migrate' , type = str , dest = 'migrate' ,
@@ -31,8 +36,24 @@ def curr_time():
3136
3237 ROOTDIR = par_dir (BASEDIR )
3338 raw_url = args .new
39+ problem_md = ''
40+ problem_slug = ''
3441 if raw_url .startswith ('https://leetcode' ):
3542 leetcode = Leetcode ()
3643 problem = leetcode .get_problem_all (raw_url )
44+ problem_slug = slugify (problem ['title' ], separator = "_" )
3745 problem_md = problem2md (problem )
38- print (problem_md )
46+
47+ if args .dir :
48+ post_dir = os .path .join (ROOTDIR , args .dir )
49+ post_fn = os .path .join (post_dir , problem_slug + '.md' )
50+ summary_path = args .dir .strip ('/' ).split ('/' )[- 1 ] + '/' + problem_slug + '.md'
51+ summary_line = '* [{title}]({path})' .format (title = problem ['title' ], path = summary_path )
52+ print (summary_line )
53+ mkdir_p (post_dir )
54+ with open (post_fn , 'w' , encoding = 'utf-8' ) as f :
55+ print ('create post file {}...' .format (post_fn ))
56+ f .write (problem_md )
57+
58+ if args .fix_summary :
59+ update_summary (ROOTDIR )
0 commit comments