webgen/mkindex.py

71 lines
2.7 KiB
Python
Raw Permalink Normal View History

2022-09-23 18:43:55 +02:00
#!/usr/bin/env python3
import sys, os, re, datetime, locale
2022-11-13 01:24:50 +01:00
webbasedir = os.path.expanduser("~/Media")
2022-09-23 18:43:55 +02:00
### for module mkdirindex:
2022-11-13 01:24:50 +01:00
targetdir = webbasedir + "/Videos"
2022-09-23 18:43:55 +02:00
indexfilename = "index.html"
2022-11-13 01:24:50 +01:00
targetdirheadline = "Something."
2022-09-23 18:43:55 +02:00
showsize = 0
####
def mkdirindex(targetdir, indexfilename, targetdirheadline, showsize):
"""Dig a directory and generate an index."""
head1 = (
'<!DOCTYPE HTML><html><head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ targetdirheadline
+ '</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="../">Main</a><br/><br/>'
body1 += '<div class="Statement">' + targetdirheadline + "</div> "
body1 += '<table border="0" class="Neutral">'
foot = '</table><br/><strong>Some byebye</strong><br/><hr class="myhr" ></body> </html>\n'
linecounter = 0
dirindexfile = open(targetdir + indexfilename, "w")
dirindexfile.write(head1 + style_fn + head2 + body1)
sortedtargetdir = os.listdir(targetdir)
sortedtargetdir.sort()
sortedtargetdir.reverse()
for file in sortedtargetdir:
sizestr = ""
# If showsize is 1 we have some filesdir and want to see the size of a file
if not re.match("^\.", file) and not file == indexfilename:
if showsize == 1:
size = os.lstat(targetdir + 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"
else:
entryname = re.sub(".html$", "", file)
# entryname = re.sub(".html$", "", re.sub("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] ", "", file))
line = str('<tr><td><a href="' + str(file) + '">' + entryname + "</a></td><td>&nbsp;" + sizestr + "</td></tr>")
dirindexfile.write(line)
linecounter += 1
dirindexfile.write(foot)
return linecounter
# Dig the files-directory and generate an index.
linecounter = mkdirindex(targetdir, indexfilename, targetdirheadline, showsize)
print("Dirindex in ", targetdir, " has ", linecounter, "lines")
# Have a nice time.