webgen v4.0.4 + mkdirindex on files as function

This commit is contained in:
Wolfgang Nowak 2022-02-12 02:58:04 +01:00
parent f63e22d464
commit 987ca7161e

View file

@ -13,7 +13,6 @@ sourcedir = os.path.expanduser("~/python/webgen/")
filesdir = webbasedir + "files/" filesdir = webbasedir + "files/"
indexfilename = "index.html" indexfilename = "index.html"
filesdirheadline = "Files" filesdirheadline = "Files"
dirindexfile = open(filesdir + indexfilename, "w")
#### ####
fileending = "" fileending = ""
@ -111,6 +110,49 @@ def selectfile():
return sourcefile, targetfile, headline return sourcefile, targetfile, headline
def mkdirindex(filesdir, indexfilename, filesdirheadline):
"""Dig the files-directory and generate an index."""
head1 = (
'<!DOCTYPE HTML><html><head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ filesdirheadline
+ '</title>\n <meta name="syntax" content="markdown">\n <meta name="generator" content="'
+ "webgen.py"
+ ' markdown2htmlconverter">\n <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">'
)
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">'
head2 = "</head><body>"
body1 = '<a class="Statement" href="../">Back</a><br/><br/><hr>'
body1 += '<div class="Statement"> Some files. Maybe useful or not.</div> '
body1 += '<table border="0" class="Neutral">'
foot = "</table><br/><strong>You can't avoid chaos.</strong><br/><hr></body> </html>\n"
linecounter=0
dirindexfile = open(filesdir + indexfilename, "w")
dirindexfile.write(head1 + style_fn + head2 + body1)
for file in os.listdir(filesdir):
if not re.match("^\.", file) and not file == indexfilename:
size = os.lstat(filesdir + file).st_size
if size > 100000000000:
sizestr = str(int(size / 1073741824)) + " GiB"
elif size > 100000000:
sizestr = str(int(size / 1048576)) + " MiB"
elif size > 10000:
sizestr = str(int(size / 1024)) + " KiB"
else:
sizestr = str(size) + " B"
line = str('<tr><td><a href="' + str(file) + '">' + str(file) + "</a></td><td>&nbsp;" + sizestr + "</td></tr>")
dirindexfile.write(line)
linecounter+=1
dirindexfile.write(foot)
return linecounter
# Parse arguments "--html", "--stdout" and "Some Headline" # Parse arguments "--html", "--stdout" and "Some Headline"
# there are 3 parameters. if --html is given, the fileending will be .html. # there are 3 parameters. if --html is given, the fileending will be .html.
@ -206,42 +248,8 @@ html_out_file.close()
if outfile != sys.stdout: if outfile != sys.stdout:
print(outputfile) print(outputfile)
# Dig the files-directory and generate an index. # Dig the files-directory and generate an index.
linecounter=mkdirindex(filesdir, indexfilename, filesdirheadline)
head1 = ( print("LC=",linecounter)
'<!DOCTYPE HTML><html><head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ filesdirheadline
+ '</title>\n <meta name="syntax" content="markdown">\n <meta name="generator" content="'
+ "webgen.py"
+ ' markdown2htmlconverter">\n <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">'
)
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">'
head2 = "</head><body>"
body1 = '<a class="Statement" href="../">Back</a><br/><br/><hr>'
body1 += '<div class="Statement"> Some files. Maybe useful or not.</div> '
body1 += '<table border="0" class="Neutral">'
foot = "</table><br/><strong>You can't avoid chaos.</strong><br/><hr></body> </html>\n"
dirindexfile.write(head1 + style_fn + head2 + body1)
for file in os.listdir(filesdir):
if not re.match("^\.", file) and not file == indexfilename:
size = os.lstat(filesdir + file).st_size
if size > 100000000000:
sizestr = str(int(size / 1073741824)) + " GiB"
elif size > 100000000:
sizestr = str(int(size / 1048576)) + " MiB"
elif size > 10000:
sizestr = str(int(size / 1024)) + " KiB"
else:
sizestr = str(size) + " B"
line = str('<tr><td><a href="' + str(file) + '">' + str(file) + "</a></td><td>&nbsp;" + sizestr + "</td></tr>")
dirindexfile.write(line)
dirindexfile.write(foot)
# Have a nice time. # Have a nice time.