5
5
import argparse
6
6
from datetime import datetime
7
7
8
+ import frontmatter
9
+ from slugify import slugify
10
+
8
11
from util import par_dir , mkdir_p
9
12
from leetcode import Leetcode
10
13
from ojhtml2markdown import problem2md
@@ -20,6 +23,8 @@ def curr_time():
20
23
parser = argparse .ArgumentParser (description = 'Helper for GitBook algorithm' )
21
24
parser .add_argument ('--new' , type = str , dest = 'new' ,
22
25
help = 'create new post with given leetcode/lintcode url.' )
26
+ parser .add_argument ('--dir' , type = str , dest = 'dir' ,
27
+ help = 'create md under dir.' )
23
28
parser .add_argument ('--update' , nargs = '*' , dest = 'update' ,
24
29
help = 'update post with given title in post and summary.' )
25
30
parser .add_argument ('--migrate' , type = str , dest = 'migrate' ,
@@ -31,8 +36,24 @@ def curr_time():
31
36
32
37
ROOTDIR = par_dir (BASEDIR )
33
38
raw_url = args .new
39
+ problem_md = ''
40
+ problem_slug = ''
34
41
if raw_url .startswith ('https://leetcode' ):
35
42
leetcode = Leetcode ()
36
43
problem = leetcode .get_problem_all (raw_url )
44
+ problem_slug = slugify (problem ['title' ], separator = "_" )
37
45
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