captions are now alttext, figure tags not used for images that are not figures

This commit is contained in:
crunk 2024-06-23 22:25:28 +02:00
parent c0b40c7d30
commit 5934122c6e
3 changed files with 24 additions and 21 deletions

View File

@ -41,9 +41,9 @@ def build_argparser():
) )
parser.add_argument( parser.add_argument(
"-c", "-a",
"--captions", "--alttexts",
help="Adds image captions based on EXIF metadata", help="Adds file alttext based on same named files",
action="store_true", action="store_true",
) )

View File

@ -3,22 +3,23 @@ import os
import magic import magic
from PIL import Image from PIL import Image
from exif import Image as ExifImage
from distribusi.page_template import html_footer, html_head from distribusi.page_template import html_footer, html_head
from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
def caption(image): def add_alttext(full_path_image):
try: try:
with open(image, "rb") as image_file: image_filename_no_ext = os.path.splitext(full_path_image)[0]
exif_image = ExifImage(image_file) alttext_filename = f'{image_filename_no_ext}_alttext.txt'
caption = exif_image.communication 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()
except Exception as e: except Exception as e:
print(e) print(f"exception {e} raised while making alttext")
caption = "" return
return caption
def thumbnail(full_path_image, name, args): def thumbnail(full_path_image, name, args):
@ -63,7 +64,6 @@ def check_distribusi_index(args, index):
""" """
check whether a index.html file is generated by distribusi check whether a index.html file is generated by distribusi
""" """
if not args.force: if not args.force:
with open(index, "r") as f: with open(index, "r") as f:
if '<meta name="generator" content="distribusi" />' in f.read(): if '<meta name="generator" content="distribusi" />' in f.read():
@ -114,15 +114,15 @@ def handle_text_files(name, full_path):
def handle_image_files(name, full_path, args): def handle_image_files(name, full_path, args):
if args.thumbnail: if args.thumbnail:
caption = ""
thumbnail_filename = thumbnail(full_path, name, args) thumbnail_filename = thumbnail(full_path, name, args)
if thumbnail_filename is None: if thumbnail_filename is None:
return return
if args.captions: if args.alttexts:
caption = caption(full_path) image_alttext = add_alttext(full_path)
return f"<figure><a href='{name}'><img class='thumbnail' src='{thumbnail_filename}'></a><figcaption>{caption}</figcaption></figure>" if image_alttext is None:
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>'
return FILE_TYPES[type_].format(name, caption) return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>'
return FILE_TYPES[type_].format(name, image_alttext)
def remove_index_html(root, files): def remove_index_html(root, files):
@ -169,10 +169,13 @@ def distribusify(args, directory): # noqa
if name.endswith("_thumbnail.jpg"): if name.endswith("_thumbnail.jpg"):
continue continue
if name.endswith("_alttext.txt"):
continue
full_path = os.path.join(root, name) full_path = os.path.join(root, name)
mime = MIME_TYPE.from_file(full_path) mime = MIME_TYPE.from_file(full_path)
type_, subtype = mime.split("/") type_, subtype = mime.split("/")
caption = name alttext = name
if args.verbose: if args.verbose:
print("Found", name, "as", mime) print("Found", name, "as", mime)
@ -185,7 +188,7 @@ def distribusify(args, directory): # noqa
if tag is None: if tag is None:
continue continue
case _: case _:
tag = FILE_TYPES[type_].format(name, caption) tag = FILE_TYPES[type_].format(name, alttext)
if subtype in SUB_TYPES: if subtype in SUB_TYPES:
tag = SUB_TYPES[subtype] tag = SUB_TYPES[subtype]

View File

@ -1,7 +1,7 @@
CODE_TYPES = ["x-c", "x-shellscript", "x-python"] CODE_TYPES = ["x-c", "x-shellscript", "x-python"]
FILE_TYPES = { FILE_TYPES = {
"image": '<figure><img class="image" src="{}"><figcaption>{}</figcaption></figure>', "image": '<img class="image" src="{}" alt="{}">',
"text": '<a href="{}" class="text">{}</a>', "text": '<a href="{}" class="text">{}</a>',
"video": ("<video controls>" '<source src="{}"></video>'), "video": ("<video controls>" '<source src="{}"></video>'),
"audio": ('<audio controls class="audio">' '<source src="{}"></audio>'), "audio": ('<audio controls class="audio">' '<source src="{}"></audio>'),