Repository: getpelican/pelican

Issue:
img alt text is invalid if it is not specified in source file
img alt text is invalid if no alt text is specified in source file. This is happening after new `_update_content`

Previously it was

```
static/pictures/Sushi.jpg
```

Now it is

```
|filename|/pictures/Sushi.jpg
```

Ideally, it should degrade gracefully and should be same as `src` of the image if not specified.



Discussion:
Is the problem where you do something like:

``` markdown
![](/static/pictures/Sushi.jpg)
```

Problem is when we try something like this without specifying alt text:

``` rst
.. image:: |filename|/images/image.png
```

The result is rendered as 

``` html
<img alt="|filename|/images/image.png" src="../static/images/image.png" />
```

Expected output was:

``` html
<img alt="../static/images/image.png" src="../static/images/image.png" />
```

https://github.com/getpelican/pelican/blob/3dcfe/pelican/contents.py#L174

The `alt` attribute could be added here. However I'm not sure we should be going down this route when there are lots of good reasons for setting the `alt` text properly.

You're right here that the problem only occurs when alt attribute is not specified. But I guess the app should handle the case anyway.

On 25-Mar-2013, at 11:10 PM, Rupert Bedford notifications@github.com wrote:

> https://github.com/getpelican/pelican/blob/3dcfe/pelican/contents.py#L174
> 
> The alt attribute could be added here. However I'm not sure we should be going down this route when there are lots of good reasons for setting the alt text.
> 
> —
> Reply to this email directly or view it on GitHub.

I agree with erfaan about expected result.

> there are lots of good reasons for setting the alt text properly

I agree with @rupert on that one!

Could you please look at #895 and share your thought there? Thanks!


Resolving patch:
diff --git a/pelican/readers.py b/pelican/readers.py
--- a/pelican/readers.py
+++ b/pelican/readers.py
@@ -37,7 +37,6 @@
 from pelican.contents import Page, Category, Tag, Author
 from pelican.utils import get_date, pelican_open
 
-
 logger = logging.getLogger(__name__)
 
 METADATA_PROCESSORS = {
@@ -104,6 +103,12 @@ def visit_abbreviation(self, node):
     def depart_abbreviation(self, node):
         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):
     enabled = bool(docutils)
@@ -391,6 +396,41 @@ def read_file(base_path, path, content_class=Page, fmt=None,
     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
+                <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
     if content and settings and settings['TYPOGRIFY']:
         from typogrify.filters import typogrify


Test patch: