44import os
55import sys
66import argparse
7+ from notion2md .console import print_error
78
8- def cli ():
9+ class Config (object ):
10+ __slots__ = ("file_name" , "target_id" , "output_path" ,"exporter_type" )
11+ def __init__ (self ,args :dict ):
12+ self .file_name = args ["name" ] if args ["name" ] else args ["id" ]
13+
14+ self .output_path = os .path .abspath (args ["path" ]) if args ["path" ] \
15+ else os .path .join (os .getcwd (),'notion2md-output' )
16+
17+ self .target_id = get_id (args ["url" ]) if args ["url" ] else args ["id" ]
18+
19+ if not self .target_id :
20+ print_error ("please enter Notion page's id or url" )
21+ print ()
22+ sys .exit (1 )
23+
24+ self .exporter_type = args ["type" ]
25+
26+ def parse_config () -> dict :
927 parser = argparse .ArgumentParser (description = "Notion2md: Notion to Markdown Exporter" )
1028 parser .add_argument ('--type' ,'-t' ,type = str ,help = "Set a type of target page: block, page, database" ,default = "block" )
1129 parser .add_argument ('--url' ,'-u' ,type = str ,help = "Set an url of target page" )
@@ -14,32 +32,26 @@ def cli():
1432 parser .add_argument ('--name' ,'-n' ,type = str ,help = "Set a custom name of output file" )
1533 parser .add_argument ('--version' ,'-v' , action = 'store_true' ,help = "Show a version of Notion2Md" )
1634
17- args = parser .parse_args ()
18-
19- if args .version :
20- print (notion2md .__version__ )
21- sys .exit ()
22-
23- def get_url ():
24- return input ("Enter Notion Url: " )
25-
26- custom_name = args .name if args .name else ""
27-
28- target_id = get_id (args .url ) if args .url \
29- else ( args .id if args .id \
30- else get_id (get_url ()))
31-
32- output_path = os .path .abspath (args .path ) if args .path \
33- else os .path .join (os .getcwd (),'notion2md-output' )
35+ return vars (parser .parse_args ())
3436
37+ def call_exporter (config :Config ):
3538 target_type_map = {
3639 'block' : block_exporter ,
3740 # 'page': page_exporter,
3841 # 'database': database
3942 }
4043
41- target_type_map [args .type ](target_id ,output_path ,custom_name )
44+ target_type_map [config .exporter_type ](config )
45+
46+ def cli ():
47+
48+ args = parse_config ()
4249
43- # page_exporter()
50+ if args ["version" ]:
51+ print (notion2md .__version__ )
52+ sys .exit (None )
4453
45- # database_exporter()
54+ config = Config (args )
55+ print ()
56+ call_exporter (config )
57+ print ()
0 commit comments