refactoring

This commit is contained in:
crunk 2024-11-22 16:15:46 +01:00
parent eec6016737
commit e93a3e3b28
2 changed files with 26 additions and 38 deletions

View File

@ -100,19 +100,12 @@ def check_distribusi_index(args, index):
return True
def write_index(args, index, html):
def write_index_html(args, index, html):
with open(index, "w") as index_file:
if args.style:
file_style = open(args.style, "r")
style = file_style.read()
styled_html_head = html_head % style
else:
styled_html_head = html_head % ""
styled_html_head = html_head.format(cssfile=args.style)
index_file.write(styled_html_head)
for line in html:
index_file.write(line + "\n")
index_file.write(html_footer)
@ -187,6 +180,23 @@ def remove_hidden_files_and_folders(dirs, files):
return dirs, files
def is_skipped_file(name, args):
if "index.html" in name:
return True
if name.endswith("_thumbnail.jpg"):
return True
if name.endswith("_alttext.txt"):
return True
if name.endswith("_dv_description.txt"):
return True
if args.style in name:
return True
def distribusify(args, directory): # noqa
for root, dirs, files in os.walk(directory):
html = []
@ -208,16 +218,7 @@ def distribusify(args, directory): # noqa
print("Generating directory listing for", root)
for name in sorted(files):
if "index.html" in name:
continue
if name.endswith("_thumbnail.jpg"):
continue
if name.endswith("_alttext.txt"):
continue
if name.endswith("_dv_description.txt"):
if is_skipped_file(name, args):
continue
full_path = os.path.join(root, name)
@ -271,8 +272,4 @@ def distribusify(args, directory): # noqa
html.insert(0, format_div(args, "dir", "dir", tag, "folder"))
index = os.path.join(root, "index.html")
if os.path.exists(index):
if check_distribusi_index(args, index):
write_index(args, index, html)
elif not os.path.exists(index):
write_index(args, index, html)
write_index_html(args, index, html)

View File

@ -1,25 +1,16 @@
html_head = """
html_head = '''\
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Generated with distribusi https://git.vvvvvvaria.org/crunk/distribusi -->
<meta name="generator" content="distribusi" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.image{max-width: 100%%;}
.pdf object{width:640px;height: 640px;}
.dir::before{content:"📁 ";font-size:18px;}
.filename{display:block;font-family:mono;}
.unkown-file::before{content:"📄 ";font-size:18px;}
div{max-width: 640px;display:inline-block;vertical-align:top;margin:1em;padding:1em;}
video {width:640px;max-height:640px;}
%s
</style>
<link rel="stylesheet" href="{cssfile}" crossorigin="anonymous">
</head>
<body>
"""
'''
html_footer = """
html_footer ='''\
</body>
</html>
"""
'''