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(
"-c",
"--captions",
help="Adds image captions based on EXIF metadata",
"-a",
"--alttexts",
help="Adds file alttext based on same named files",
action="store_true",
)

View File

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

View File

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