in directory_listing.py [0:0]
def create_plain_index(root, dirs, files, dirname=None):
'''
Creates the fairly plain HTML folder index
including a preview of a single image file, if any is present.
Returns the HTML source as string.
Args:
root: string, path to the root directory, all paths in *dirs* and
*files* are relative to this one
dirs: list of strings, the directories in *root*
files: list of string, the files in *root*
dirname: name to print in the html, which may be different than *root*
Returns: HTML source of the directory listing
'''
if dirname is None:
dirname = root or '/'
html = "<!DOCTYPE html>\n"
html += "<html lang='en'><head>"
html += "<title>Index of {}</title>\n".format(dirname)
html += "<meta charset='UTF-8'>\n"
html += "<style>\n"
html += "body { font-family: Segoe UI, Helvetica, Arial, sans-serif; }\n"
html += "a { text-decoration:none; }\n"
html += "</style>\n"
html += "</head><body>\n"
html += "<h1>Index of {}</h1>\n".format(dirname)
# Insert preview image
jpg_files = [f for f in files if is_image_file(f)]
if len(jpg_files) > 0:
# This is slow, so defaulting to just the first image:
#
# Use the largest image file as this is most likely to contain
# useful content.
#
# jpg_file_sizes = [os.path.getsize(f) for f in jpg_files]
# largest_file_index = max(range(len(jpg_files)), key=lambda x: jpg_file_sizes[x])
html += "<a href='{0}'><img style='height:200px; float:right;' src='{0}' alt='Preview image'></a>\n".format(jpg_files[0])
else:
html += "\n"
# html += "<p style='width:15em; float:right; margin:0;'>[No preview available]</p>\n"
if root:
html += "<p><a href='../index.html'>To parent directory</a></p>\n"
else:
html += "\n"
# html += "<p>This is the root directoy.</p>\n"
html += "<h2>Folders</h2>\n"
if len(dirs) > 0:
html += "<ul style='list-style-type: none; padding-left:1em;'>\n"
for dir in sorted(dirs):
html += "<li>📁 <a href='{0}/index.html'>{0}</a></li>\n".format(dir)
html += "</ul>\n"
else:
html += "<p style='padding-left:1em;'>No folders</p>\n"
html += "<h2>Files</h2>\n"
if len(files) > 0:
html += "<ul style='list-style-type: none; padding-left:1em;'>\n"
for fname in sorted(files):
if is_image_file(fname):
html += "<li>🖻 <a href='{0}'>{0}</a></li>\n".format(fname)
else:
html += "<li>🖺 <a href='{0}'>{0}</a></li>\n".format(fname)
html += "</ul>\n"
else:
html += "<p style = 'padding-left:1em;'>No files</p>\n"
# Add some space at the bottom because the browser's status bar might hide stuff
html += "<p style='margin:2em;'> </p>\n"
html += "</body></html>\n"
return html