|
|
@ -1,4 +1,3 @@ |
|
|
|
import base64 |
|
|
|
import os |
|
|
|
|
|
|
|
import magic |
|
|
@ -12,16 +11,35 @@ MIME_TYPE = magic.Magic(mime=True) |
|
|
|
def add_alttext(full_path_image): |
|
|
|
try: |
|
|
|
image_filename_no_ext = os.path.splitext(full_path_image)[0] |
|
|
|
alttext_filename = f'{image_filename_no_ext}_alttext.txt' |
|
|
|
if os.path.isfile(alttext_filename): |
|
|
|
print(f"{image_filename_no_ext} has {alttext_filename}") |
|
|
|
with open(alttext_filename, 'r') as alttext_file: |
|
|
|
return alttext_file.read() |
|
|
|
alttext_filename = f"{image_filename_no_ext}_alttext.txt" |
|
|
|
return _read_matching_text_file( |
|
|
|
image_filename_no_ext, alttext_filename |
|
|
|
) |
|
|
|
except Exception as e: |
|
|
|
print(f"exception {e} raised while making alttext") |
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
def add_description(full_path_image): |
|
|
|
try: |
|
|
|
image_filename_no_ext = os.path.splitext(full_path_image)[0] |
|
|
|
description_filename = f"{image_filename_no_ext}_dv_description.txt" |
|
|
|
return _read_matching_text_file( |
|
|
|
image_filename_no_ext, description_filename |
|
|
|
) |
|
|
|
except Exception as e: |
|
|
|
print(f"exception {e} raised while adding description") |
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
def _read_matching_text_file(image_filename_no_ext, filename): |
|
|
|
if not os.path.isfile(filename): |
|
|
|
return |
|
|
|
print(f"{image_filename_no_ext} has {filename}") |
|
|
|
with open(filename, "r") as text_file: |
|
|
|
return text_file.read() |
|
|
|
|
|
|
|
|
|
|
|
def thumbnail(full_path_image, name, args): |
|
|
|
if full_path_image.endswith("_thumbnail.jpg"): |
|
|
|
return |
|
|
@ -94,7 +112,7 @@ def write_index(args, index, html, html_head, html_footer): |
|
|
|
index_file.write(html_footer) |
|
|
|
|
|
|
|
|
|
|
|
def handle_text_files(name, full_path): |
|
|
|
def handle_text_files(name, full_path, subtype): |
|
|
|
if name.endswith(".html") or subtype == "html": |
|
|
|
subtype = "html" |
|
|
|
# what types of text files to expand |
|
|
@ -119,9 +137,15 @@ def handle_image_files(name, full_path, args): |
|
|
|
return |
|
|
|
if args.alttexts: |
|
|
|
image_alttext = add_alttext(full_path) |
|
|
|
if args.alttexts: |
|
|
|
image_description = add_description(full_path) |
|
|
|
if image_alttext is None: |
|
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>' |
|
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>' |
|
|
|
if image_description is None: |
|
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>' |
|
|
|
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}"></a><figcaption>{image_description}</figcaption></figure>' |
|
|
|
if image_description is None: |
|
|
|
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>' |
|
|
|
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a><figcaption>{image_description}</figcaption></figure>' |
|
|
|
return FILE_TYPES[type_].format(name, image_alttext) |
|
|
|
|
|
|
|
|
|
|
@ -150,7 +174,9 @@ def distribusify(args, directory): # noqa |
|
|
|
|
|
|
|
if args.exclude_directory: |
|
|
|
if args.verbose: |
|
|
|
print("Excluding directory:", ", ".join(args.exclude_directory)) |
|
|
|
print( |
|
|
|
"Excluding directory:", ", ".join(args.exclude_directory) |
|
|
|
) |
|
|
|
dirs[:] = [d for d in dirs if d not in args.exclude_directory] |
|
|
|
|
|
|
|
if args.no_hidden: |
|
|
@ -172,6 +198,9 @@ def distribusify(args, directory): # noqa |
|
|
|
if name.endswith("_alttext.txt"): |
|
|
|
continue |
|
|
|
|
|
|
|
if name.endswith("_dv_description.txt"): |
|
|
|
continue |
|
|
|
|
|
|
|
full_path = os.path.join(root, name) |
|
|
|
mime = MIME_TYPE.from_file(full_path) |
|
|
|
type_, subtype = mime.split("/") |
|
|
@ -182,7 +211,7 @@ def distribusify(args, directory): # noqa |
|
|
|
if type_ in FILE_TYPES: |
|
|
|
match type_: |
|
|
|
case "text": |
|
|
|
subtype, tag = handle_text_files(name, full_path) |
|
|
|
subtype, tag = handle_text_files(name, full_path, subtype) |
|
|
|
case "image": |
|
|
|
tag = handle_image_files(name, full_path, args) |
|
|
|
if tag is None: |
|
|
@ -197,7 +226,9 @@ def distribusify(args, directory): # noqa |
|
|
|
# catch exceptions not yet defined in FILE_TYPES or SUB_TYPES |
|
|
|
tag = "<a href='{}'>{}</a>" |
|
|
|
if args.verbose: |
|
|
|
message = "not in list of file types, adding as plain href: \n" |
|
|
|
message = ( |
|
|
|
"not in list of file types, adding as plain href: \n" |
|
|
|
) |
|
|
|
print(type_, subtype, message, name) |
|
|
|
subtype = subtype + " unkown-file" |
|
|
|
|
|
|
|