From bab8d0b26a80ab3c9b38311d26fccad938ec2220 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 7 Aug 2013 00:10:26 +0200 Subject: [PATCH] Move the "find image with an empty alt" block in a function. --- pelican/readers.py | 79 +++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/pelican/readers.py b/pelican/readers.py index e5283de7..3b3bfd12 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -416,40 +416,9 @@ class Readers(object): content, reader_metadata = reader.read(path) metadata.update(reader_metadata) - # create warnings for all images with empty alt (up to a certain - # number) # as they are really likely to be accessibility flaws if content: # find images with empty alt - imgs = re.compile(r""" - (?: - # src before alt - ]* - src=(['"])(.*)\1 - [^\>]* - alt=(['"])\3 - )|(?: - # alt before src - ]* - alt=(['"])\4 - [^\>]* - src=(['"])(.*)\5 - ) - """, re.X) - matches = re.findall(imgs, content) - # find a correct threshold - nb_warnings = 10 - if len(matches) == nb_warnings + 1: - nb_warnings += 1 # avoid bad looking case - # print one warning per image with empty alt until threshold - for match in matches[:nb_warnings]: - logger.warning('Empty alt attribute for image {} in {}'.format( - os.path.basename(match[1] + match[5]), path)) - # print one warning for the other images with empty alt - if len(matches) > nb_warnings: - logger.warning('{} other images with empty alt attributes' - .format(len(matches) - nb_warnings)) + find_empty_alt(content, path) # eventually filter the content with typogrify if asked so if content and self.settings['TYPOGRIFY']: @@ -467,6 +436,45 @@ class Readers(object): context=context) +def find_empty_alt(content, path): + """Find images with empty alt + + Create warnings for all images with empty alt (up to a certain number), + as they are really likely to be accessibility flaws. + + """ + imgs = re.compile(r""" + (?: + # src before alt + ]* + src=(['"])(.*)\1 + [^\>]* + alt=(['"])\3 + )|(?: + # alt before src + ]* + alt=(['"])\4 + [^\>]* + src=(['"])(.*)\5 + ) + """, re.X) + matches = re.findall(imgs, content) + # find a correct threshold + nb_warnings = 10 + if len(matches) == nb_warnings + 1: + nb_warnings += 1 # avoid bad looking case + # print one warning per image with empty alt until threshold + for match in matches[:nb_warnings]: + logger.warning('Empty alt attribute for image {} in {}'.format( + os.path.basename(match[1] + match[5]), path)) + # print one warning for the other images with empty alt + if len(matches) > nb_warnings: + logger.warning('{} other images with empty alt attributes' + .format(len(matches) - nb_warnings)) + + def default_metadata(settings=None, process=None): metadata = {} if settings: @@ -516,13 +524,12 @@ def parse_path_metadata(source_path, settings=None, process=None): subdir = os.path.basename(dirname) if settings: checks = [] - for key,data in [('FILENAME_METADATA', base), - ('PATH_METADATA', source_path), - ]: + for key, data in [('FILENAME_METADATA', base), + ('PATH_METADATA', source_path)]: checks.append((settings.get(key, None), data)) if settings.get('USE_FOLDER_AS_CATEGORY', None): checks.insert(0, ('(?P.*)', subdir)) - for regexp,data in checks: + for regexp, data in checks: if regexp and data: match = re.match(regexp, data) if match: