From 11a63f661403101973c673be18fe3475a1942c7d Mon Sep 17 00:00:00 2001 From: Sewon Ahn Date: Tue, 12 Jan 2021 22:22:32 +0900 Subject: [PATCH] Add image alt system --- distribusi/distribusi/distribusi.py | 55 ++++++++++++++++++++++------- distribusi/distribusi/fragments.py | 6 +++- distribusi/distribusi/ignore.py | 11 ++++-- distribusi/distribusi/mappings.py | 2 +- test_data/.ignore | 3 +- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/distribusi/distribusi/distribusi.py b/distribusi/distribusi/distribusi.py index f24df3e..7252c63 100644 --- a/distribusi/distribusi/distribusi.py +++ b/distribusi/distribusi/distribusi.py @@ -18,6 +18,8 @@ from distribusi.ignore import Ignore MIME_TYPE = magic.Magic(mime=True) +ignore = Ignore() + def caption(image): try: @@ -32,6 +34,7 @@ def caption(image): except Exception as e: caption = '' print(e) + return caption @@ -156,19 +159,21 @@ def render_dir(args, directory): relative = lv[len(lv) - 1] 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) mime = MIME_TYPE.from_file(full_path) # example: MIME plain/text becomes 'type' plain 'subtype' text type_, subtype = mime.split('/') - caption = name + c = name if args.verbose: print('Found file in dir ', name, 'as', mime) 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 if type_ == 'text': @@ -189,14 +194,25 @@ def render_dir(args, directory): # a = FILE_TYPES[type_] if type_ == 'image': - a = FILE_TYPES[type_].format(relative_path, caption) + a = FILE_TYPES[type_].format(relative_path, c, c) if args.thumbnail: a = thumbnail(full_path, relative_path, args) if args.no_filenames: - caption = "" + c = "" if args.captions: - caption = caption(relative_path) - a = FILE_TYPES[type_].format(relative_path, caption) + c = caption(relative_path) + 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: a = SUB_TYPES[subtype] @@ -220,7 +236,7 @@ def render_dir(args, directory): def distribusify(args, directory, freg): # noqa - ignore = Ignore() + for root, dirs, files in os.walk(directory): ignore.add(root) @@ -260,14 +276,14 @@ def distribusify(args, directory, freg): # noqa # example: MIME plain/text becomes 'type' plain 'subtype' text type_, subtype = mime.split('/') - caption = name + c = name if args.verbose: print('Found', name, 'as', mime) 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 if type_ == 'text': @@ -291,10 +307,23 @@ def distribusify(args, directory, freg): # noqa if args.thumbnail: a = thumbnail(full_path, name, args) if args.no_filenames: - caption = "" + c = "" if args.captions: - caption = caption(full_path) - a = FILE_TYPES[type_].format(name, caption) + c = caption(full_path) + 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: a = SUB_TYPES[subtype] diff --git a/distribusi/distribusi/fragments.py b/distribusi/distribusi/fragments.py index 04ace4d..6133016 100644 --- a/distribusi/distribusi/fragments.py +++ b/distribusi/distribusi/fragments.py @@ -57,7 +57,11 @@ class Fragments: meta_path = os.path.join(directory, f) with open(meta_path) as json_file: meta = json.load(json_file) - occupation = meta["occupation"] + try: + occupation = meta["occupation"] + except KeyError: + return + if occupation > -1: origin_path = os.path.join(directory, file) date = self.creation_date(origin_path) diff --git a/distribusi/distribusi/ignore.py b/distribusi/distribusi/ignore.py index f046e12..65c7e1f 100644 --- a/distribusi/distribusi/ignore.py +++ b/distribusi/distribusi/ignore.py @@ -1,5 +1,5 @@ import os - +import re class Ignore: def __init__(self): @@ -19,5 +19,10 @@ class Ignore: def test(self, target): if target in self.ignore: return True - else: - return False + + for ig in self.ignore: + reg = re.compile(ig) + if bool(re.match(reg, target)): + return True + + return False diff --git a/distribusi/distribusi/mappings.py b/distribusi/distribusi/mappings.py index 73d6d96..31134da 100644 --- a/distribusi/distribusi/mappings.py +++ b/distribusi/distribusi/mappings.py @@ -2,7 +2,7 @@ CODE_TYPES = ['x-c', 'x-shellscript', 'x-python'] FILE_TYPES = { - 'image': '
{}
', + 'image': '
{}
{}
', 'text': '{}', 'video': (''), 'audio': (''), diff --git a/test_data/.ignore b/test_data/.ignore index f228276..215a393 100644 --- a/test_data/.ignore +++ b/test_data/.ignore @@ -9,4 +9,5 @@ src participants.html style.css 404.html -main.js \ No newline at end of file +main.js +.+.alt \ No newline at end of file