mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Add warnings for img with empty alts. Fixes #647.
Unify Rst and Mkd: if alt is not specified at all, create an empty alt. Warns the user in case of empty alts.
This commit is contained in:
parent
1b904ae767
commit
a75eac6c0f
1 changed files with 41 additions and 1 deletions
|
|
@ -37,7 +37,6 @@ except ImportError:
|
||||||
from pelican.contents import Page, Category, Tag, Author
|
from pelican.contents import Page, Category, Tag, Author
|
||||||
from pelican.utils import get_date, pelican_open
|
from pelican.utils import get_date, pelican_open
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
METADATA_PROCESSORS = {
|
METADATA_PROCESSORS = {
|
||||||
|
|
@ -104,6 +103,12 @@ class PelicanHTMLTranslator(HTMLTranslator):
|
||||||
def depart_abbreviation(self, node):
|
def depart_abbreviation(self, node):
|
||||||
self.body.append('</abbr>')
|
self.body.append('</abbr>')
|
||||||
|
|
||||||
|
def visit_image(self, node):
|
||||||
|
# set an empty alt if alt is not specified
|
||||||
|
# avoids that alt is taken from src
|
||||||
|
node['alt'] = node.get('alt', '')
|
||||||
|
return HTMLTranslator.visit_image(self, node)
|
||||||
|
|
||||||
|
|
||||||
class RstReader(Reader):
|
class RstReader(Reader):
|
||||||
enabled = bool(docutils)
|
enabled = bool(docutils)
|
||||||
|
|
@ -391,6 +396,41 @@ def read_file(base_path, path, content_class=Page, fmt=None,
|
||||||
content, reader_metadata = reader.read(path)
|
content, reader_metadata = reader.read(path)
|
||||||
metadata.update(reader_metadata)
|
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
|
||||||
|
<img
|
||||||
|
[^\>]*
|
||||||
|
src=(['"])(.*)\1
|
||||||
|
[^\>]*
|
||||||
|
alt=(['"])\3
|
||||||
|
)|(?:
|
||||||
|
# alt before src
|
||||||
|
<img
|
||||||
|
[^\>]*
|
||||||
|
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))
|
||||||
|
|
||||||
# eventually filter the content with typogrify if asked so
|
# eventually filter the content with typogrify if asked so
|
||||||
if content and settings and settings['TYPOGRIFY']:
|
if content and settings and settings['TYPOGRIFY']:
|
||||||
from typogrify.filters import typogrify
|
from typogrify.filters import typogrify
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue