Add image alt system

This commit is contained in:
Sewon Ahn 2021-01-12 22:22:32 +09:00
parent ddab74b785
commit 11a63f6614
5 changed files with 58 additions and 19 deletions

View file

@ -18,6 +18,8 @@ from distribusi.ignore import Ignore
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
ignore = Ignore()
def caption(image): def caption(image):
try: try:
@ -32,6 +34,7 @@ def caption(image):
except Exception as e: except Exception as e:
caption = '' caption = ''
print(e) print(e)
return caption return caption
@ -156,19 +159,21 @@ def render_dir(args, directory):
relative = lv[len(lv) - 1] relative = lv[len(lv) - 1]
relative_path = "./{}/{}".format(relative, name) relative_path = "./{}/{}".format(relative, name)
if 'index.html' not in name: if ignore.test(name):
pass
elif 'index.html' not in name:
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)
# example: MIME plain/text becomes 'type' plain 'subtype' text # example: MIME plain/text becomes 'type' plain 'subtype' text
type_, subtype = mime.split('/') type_, subtype = mime.split('/')
caption = name c = name
if args.verbose: if args.verbose:
print('Found file in dir ', name, 'as', mime) print('Found file in dir ', name, 'as', mime)
if type_ in FILE_TYPES: if type_ in FILE_TYPES:
a = FILE_TYPES[type_].format(relative_path, caption) a = FILE_TYPES[type_].format(relative_path, c, c)
# expansion for different kind of text files # expansion for different kind of text files
if type_ == 'text': if type_ == 'text':
@ -189,14 +194,25 @@ def render_dir(args, directory):
# a = FILE_TYPES[type_] # a = FILE_TYPES[type_]
if type_ == 'image': if type_ == 'image':
a = FILE_TYPES[type_].format(relative_path, caption) a = FILE_TYPES[type_].format(relative_path, c, c)
if args.thumbnail: if args.thumbnail:
a = thumbnail(full_path, relative_path, args) a = thumbnail(full_path, relative_path, args)
if args.no_filenames: if args.no_filenames:
caption = "" c = ""
if args.captions: if args.captions:
caption = caption(relative_path) c = caption(relative_path)
a = FILE_TYPES[type_].format(relative_path, caption) a = FILE_TYPES[type_].format(relative_path, c, c)
# ALT 처리
alt_path = full_path + ".alt"
if os.path.isfile(alt_path):
f = open(alt_path, 'r', encoding='utf-8')
alt = ''
while True:
line = f.readline()
if not line: break
alt = alt + line + ' '
a = FILE_TYPES[type_].format(relative_path, alt, c)
if subtype in SUB_TYPES: if subtype in SUB_TYPES:
a = SUB_TYPES[subtype] a = SUB_TYPES[subtype]
@ -220,7 +236,7 @@ def render_dir(args, directory):
def distribusify(args, directory, freg): # noqa def distribusify(args, directory, freg): # noqa
ignore = Ignore()
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
ignore.add(root) ignore.add(root)
@ -260,14 +276,14 @@ def distribusify(args, directory, freg): # noqa
# example: MIME plain/text becomes 'type' plain 'subtype' text # example: MIME plain/text becomes 'type' plain 'subtype' text
type_, subtype = mime.split('/') type_, subtype = mime.split('/')
caption = name c = name
if args.verbose: if args.verbose:
print('Found', name, 'as', mime) print('Found', name, 'as', mime)
if type_ in FILE_TYPES: if type_ in FILE_TYPES:
a = FILE_TYPES[type_].format(name, caption) a = FILE_TYPES[type_].format(name, c, c)
# expansion for different kind of text files # expansion for different kind of text files
if type_ == 'text': if type_ == 'text':
@ -291,10 +307,23 @@ def distribusify(args, directory, freg): # noqa
if args.thumbnail: if args.thumbnail:
a = thumbnail(full_path, name, args) a = thumbnail(full_path, name, args)
if args.no_filenames: if args.no_filenames:
caption = "" c = ""
if args.captions: if args.captions:
caption = caption(full_path) c = caption(full_path)
a = FILE_TYPES[type_].format(name, caption) a = FILE_TYPES[type_].format(name, c, c)
# ALT 처리
alt_path = full_path + ".alt"
if os.path.isfile(alt_path):
f = open(alt_path, 'r', encoding='utf-8')
alt = ''
while True:
line = f.readline()
if not line: break
alt = alt + line + ' '
a = FILE_TYPES[type_].format(name, alt, c)
if subtype in SUB_TYPES: if subtype in SUB_TYPES:
a = SUB_TYPES[subtype] a = SUB_TYPES[subtype]

View file

@ -57,7 +57,11 @@ class Fragments:
meta_path = os.path.join(directory, f) meta_path = os.path.join(directory, f)
with open(meta_path) as json_file: with open(meta_path) as json_file:
meta = json.load(json_file) meta = json.load(json_file)
occupation = meta["occupation"] try:
occupation = meta["occupation"]
except KeyError:
return
if occupation > -1: if occupation > -1:
origin_path = os.path.join(directory, file) origin_path = os.path.join(directory, file)
date = self.creation_date(origin_path) date = self.creation_date(origin_path)

View file

@ -1,5 +1,5 @@
import os import os
import re
class Ignore: class Ignore:
def __init__(self): def __init__(self):
@ -19,5 +19,10 @@ class Ignore:
def test(self, target): def test(self, target):
if target in self.ignore: if target in self.ignore:
return True return True
else:
return False for ig in self.ignore:
reg = re.compile(ig)
if bool(re.match(reg, target)):
return True
return False

View file

@ -2,7 +2,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': '<figure><img class="image" src="{}" alt="{}" ><figcaption>{}</figcaption></figure>',
'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>'),

View file

@ -10,3 +10,4 @@ participants.html
style.css style.css
404.html 404.html
main.js main.js
.+.alt