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('Download script from ' + blue1 + '{}'.format(infile, infile) + blue2 +'
\n') fout.write('\n') fout.write('