#Instructions: open your command line or terminal in the directory containing your file then run "python texcleanup.py input_filename output_filename". # Add -nd as an option if you don't want \today to be changed to today's actual date. # Created by: Al Baraa Abd Aldaim. # DISCLAIMER: original creator not responsible for any errors, mistakes, or omisssions arising from the use of this script. import sys from os.path import exists from datetime import date if len(sys.argv)==1: sys.exit("Please input (1) the name of your tex file (including .tex) \n (2) the name you'd like for the new file (make sure there are no files with the same name in the directory, and include .tex).\n (3) (OPTIONAL) add '-nd' if you don't want \\today changed to today's fixed date.\n\n DISCLAIMER: do NOT use this script on your only copy of your tex file!\n") if len(sys.argv) not in [3,4] : sys.exit("Incorrect number of arguments, please input no more than 3 arguments and at least (1) the name of your tex file and (2) the name you'd like for the new file.\n\n DISCLAIMER: do NOT use this script on your only copy of your tex file!\n") if len(sys.argv)==4 and sys.argv[3]!="-nd": sys.exit("Option "+sys.argv[3]+" not recognized.") if sys.argv[1][-4:]!=".tex" or sys.argv[2][-4:]!=".tex": sys.exit("Please include .tex in your file names!") if exists(sys.argv[2]): if input("Are you sure you want to overwrite the file "+sys.argv[2]+"? Type Y to continue: ")!="Y": sys.exit() try: f = open(sys.argv[1], "r") except: sys.exit("Could not open your file! Make sure you input the correct filename!") new = "" count = 0 for line in f: newline = "" for i in range(len(line)): if line[0]=="%" or (i>0 and line[i]=="%" and line[i-1]!= "\\"): count+=1 newline+="\n" break newline+=line[i] new+=newline f.close() print(str(count)+" comments deleted!") if len(sys.argv)==4 and sys.argv[3]=="-nd": print("No date change requested.") else: if new.find("\\date{\\today}")==-1: print("No \\date{\\today} command found!") else: today_date = date.today().strftime("%B %d, %Y") new = new.replace("\\date{\\today}", "\\date{"+today_date+"}", 1) print("Date set to "+today_date) nf = open(sys.argv[2], "w") nf.write(new) nf.close()