import sys import re import os import glob """ python script converter to html 1. Title and download link are given at top 2. Comments are shown in red 3. Image files (jpg, jpeg, png) are shown at last """ argv = sys.argv infile = None outfile = None if len(argv) >= 2: infile = argv[1] # global variables red1 = '' red2 = '' blue1 = '' blue2 = '' green1 = '' green2 = '' def escape(t): """HTML-escape the text in `t`.""" return (t # .replace("&", "&") # .replace("<", "<").replace(">", ">") # .replace("'", "'").replace('"', """) .replace(" ", " ") ) outfile = re.sub(r'\.[^\.]*?$', '.html', infile) fmask = re.sub(r'\.[^\.]*?$', '*.*', infile) imagefiles = glob.glob(fmask) nf = len(imagefiles) for i in range(nf-1, -1, -1): if not re.search(r'\.(jpg|jpeg|png)$', imagefiles[i]): del imagefiles[i] if len(imagefiles) == 0: imagefiles.append(re.sub(r'\.[^\.]*?$', '.jpg', infile)) print("in file :", infile) print("out file :", outfile) print("Image files ({}):".format(fmask)) for f in imagefiles: print(" :", f) fin = open(infile, 'r', encoding='utf-8') lines = fin.readlines() fin.close() fout = open(outfile, 'w') IsComment = False title = 'No title' title1 = 'No title' for i in range(len(lines)): line = lines[i] line = line.rstrip() ret = re.search(r"^(.*?)('''|\"\"\")(.*)$", line) if ret: title = '' for j in range(i+1, len(lines)): if re.search(r"('''|\"\"\")", lines[j]): break title += lines[j] if j == i+1: title1 = lines[j] title = re.sub(r'\n', r'
\n', title.rstrip()) break print("Title1:", title1) print("Title :", title) fout.write("\n" + "\n") fout.write('\n') fout.write('\n') fout.write("{}\n".format(title1)) fout.write("\n" + "\n") fout.write('\n') fout.write('' + blue1 + title + blue2 + '

\n') fout.write('Download script from ' + blue1 + '{}'.format(infile, infile) + blue2 +'

\n') fout.write('\n') fout.write('


\n') fout.write('\n') for i in range(len(lines)): line = lines[i] line = line.rstrip() # ret = re.search(r"^(\s*)(.*)$", line) ret = re.search(r"^(\s*#?\s*)(.*)$", line) # ret = re.search(r"^(#?\s*)(.*)$", line) if ret: spc = ret[1] line = ret[2] spc = re.sub(r' ', ' ', spc) else: spc = '' spc = re.sub('\t', '    ', spc) line = spc + line ret = re.search(r"^(.*?)(''')(.*)$", line) if ret: if not IsComment: IsComment = True line = ret[1] + '{font_red_start}' + ret[2] + ret[3] else: IsComment = True line = ret[1] + ret[2] + '{font_red_end}' + ret[3] ret = re.search(r'^(.*?)(""")(.*)$', line) if ret: if not IsComment: IsComment = True line = ret[1] + '{font_red_start}' + ret[2] + ret[3] else: IsComment = True line = ret[1] + ret[2] + '{font_red_end}' + ret[3] # print("line: ", line) if re.search(r'^(.*?)(#.*?\?\?\?.*)$', line): line = re.sub(r'^(.*?)(#.*?\?\?\?.*)$', r'\1{font_blue_start}\2{font_blue_end}', line) else: line = re.sub(r'^(.*?)(#)(.*)$', r'\1{font_red_start}\2\3{font_red_end}', line) line = re.sub(r'{font_red_start}', red1, line) line = re.sub(r'{font_red_end}', red2, line) line = re.sub(r'{font_blue_start}', blue1, line) line = re.sub(r'{font_blue_end}', blue2, line) # line = re.sub(r'(https?://\S+)', '$1', line) match = re.search(r'((http|ftp)s?://\S+)', line) if match is not None: print("link = ", match[1]) line = re.sub(r'(https?://\S+)', '%s' % (match[1],match[1]), line) fout.write(line + "
\n") fout.write("
\n") fout.write("\n") fout.write("
\n") for f in imagefiles: fout.write('\n\n\n'.format(f, f)) fout.write('
\n') fout.write("\n") fout.close()