1414from .ProblemPlasTeX import ProblemsetMacros
1515from . import template
1616
17+ def convert (args : list [str ]| None = None ) -> None :
18+ options = parse_args (args )
1719
18- def convert (problem , options = None ):
19- problem = os .path .realpath (problem )
20+ problem = os .path .realpath (options .problem )
2021
2122 problembase = os .path .splitext (os .path .basename (problem ))[0 ]
2223 destdir = string .Template (options .destdir ).safe_substitute (problem = problembase )
@@ -92,7 +93,7 @@ def convert(problem, options=None):
9293
9394 # identify any large generated files (especially images)
9495 if not options .quiet :
95- for path , dirs , files in os .walk ('.' ):
96+ for path , _dirs , files in os .walk ('.' ):
9697 for f in files :
9798 file_size_kib = os .stat (os .path .join (path , f )).st_size // 1024
9899 if file_size_kib > 1024 :
@@ -109,46 +110,31 @@ def convert(problem, options=None):
109110 # restore cwd
110111 os .chdir (origcwd )
111112
112- return True
113-
114-
115- class ConvertOptions :
116- available = [
117- ['bodyonly' , 'store_true' , '-b' , '--body-only' ,
118- 'only generate HTML body, no HTML headers' , False ],
119- ['css' , 'store_false' , '-c' , '--no-css' ,
120- "don't copy CSS file to output directory" , True ],
121- ['headers' , 'store_false' , '-H' , '--headers' ,
122- "don't generate problem headers (title, problem id, time limit)" , True ],
123- ['tidy' , 'store_false' , '-m' , '--messy' ,
124- "don't run tidy to postprocess the HTML" , True ],
125- ['destdir' , 'store' , '-d' , '--dest-dir' ,
126- "output directory" , '${problem}_html' ],
127- ['destfile' , 'store' , '-f' , '--dest-file' ,
128- "output file name" , 'index.html' ],
129- ['language' , 'store' , '-l' , '--language' ,
130- 'choose alternate language (2-letter code)' , None ],
131- ['loglevel' , 'store' , '-L' , '--log-level' ,
132- 'set log level (debug, info, warning, error, critical)' , 'warning' ],
133- ['quiet' , 'store_true' , '-q' , '--quiet' ,
134- "quiet" , False ],
135- ]
136-
137- def __init__ (self ):
138- for (dest , _ , _ , _ , _ , default ) in ConvertOptions .available :
139- setattr (self , dest , default )
140- self .imgbasedir = ''
141-
142-
143- def main ():
144- options = ConvertOptions ()
113+
114+ def parse_args (args : list [str ]| None ) -> argparse .Namespace :
145115 parser = argparse .ArgumentParser (formatter_class = argparse .ArgumentDefaultsHelpFormatter )
146- for (dest , action , short , _long , _help , default ) in ConvertOptions .available :
147- parser .add_argument (short , _long , dest = dest , help = _help , action = action , default = default )
116+
117+ parser .add_argument ('-b' , '--body-only' , dest = 'bodyonly' , action = 'store_true' , help = 'only generate HTML body, no HTML headers' , default = False )
118+ parser .add_argument ('-c' , '--no-css' , dest = 'css' , action = 'store_false' , help = "don't copy CSS file to output directory" , default = True )
119+ parser .add_argument ('-H' , '--headers' , dest = 'headers' , action = 'store_false' , help = "don't generate problem headers (title, problem id, time limit)" , default = True )
120+ parser .add_argument ('-m' , '--messy' , dest = 'tidy' , action = 'store_false' , help = "don't run tidy to postprocess the HTML" , default = True )
121+ parser .add_argument ('-d' , '--dest-dir' , dest = 'destdir' , help = "output directory" , default = '${problem}_html' )
122+ parser .add_argument ('-f' , '--dest-file' , dest = 'destfile' , help = "output file name" , default = 'index.html' )
123+ parser .add_argument ('-l' , '--language' , dest = 'language' , help = 'choose alternate language (2-letter code)' , default = None )
124+ parser .add_argument ('-L' , '--log-level' , dest = 'loglevel' , help = 'set log level (debug, info, warning, error, critical)' , default = 'warning' )
125+ parser .add_argument ('-q' , '--quiet' , dest = 'quiet' , action = 'store_true' , help = "quiet" , default = False )
148126 parser .add_argument ('problem' , help = 'the problem to convert' )
149127
150- options = parser .parse_args (namespace = options )
151- convert (options .problem , options )
128+ if args is not None :
129+ options = parser .parse_args (args )
130+ else :
131+ options = parser .parse_args ()
132+
133+ options .imgbasedir = ''
134+ return options
135+
136+ def main () -> None :
137+ convert ()
152138
153139
154140if __name__ == '__main__' :
0 commit comments