cli.py (1687B) - raw
1 """ 2 Command line interface for boxnotes2html 3 """ 4 import argparse 5 import os 6 import sys 7 8 from boxnotes2html.boxnote import BoxNote 9 10 11 def run(): 12 run_with_args(sys.argv[1:]) 13 14 15 def run_with_args(args): 16 parser = argparse.ArgumentParser() 17 parser.add_argument( 18 "files", 19 help="file or files to process. If passed a directory, will process everything in that directory with the .boxnote extension.", 20 nargs="*", 21 ) 22 # TODO: implement 23 parser.add_argument( 24 "-r", "--recurse", help="recursively look through a folder", action="store_true" 25 ) 26 parser.add_argument( 27 "-f", 28 "--filetype", 29 help="output filetype: markdown or html or plaintext. Default html", 30 choices=["md", "html", "txt"], 31 default="html", 32 ) 33 args = parser.parse_args(args) 34 35 for filepath in args.files: 36 if os.path.isdir(filepath): 37 for root, dirs, files in os.walk(filepath): 38 for subfile in files: 39 full_path = os.path.join(root, subfile) 40 if full_path.endswith(".boxnote"): 41 write_file(full_path, args.filetype) 42 else: 43 write_file(filepath, args.filetype) 44 45 46 def write_file(filepath, filetype): 47 note = BoxNote.from_file(filepath) 48 if filetype == "html": 49 out_string = note.as_html() 50 elif filetype == "md": 51 out_string = note.as_markdown() 52 elif filetype == "txt": 53 out_string = note.as_text() 54 output_path = os.path.splitext(filepath)[0] + ".{}".format(filetype) 55 print("writing file {}".format(output_path)) 56 with open(output_path, "w") as f: 57 f.write(out_string)