chiark / gitweb /
m.images: make it possible to enforce alt text for all images and figures.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 24 Oct 2017 12:31:24 +0000 (14:31 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Tue, 24 Oct 2017 12:31:24 +0000 (14:31 +0200)
doc/plugins/images.rst
pelican-plugins/m/images.py

index 88eacd7b9bb4e68f3baaa3f71755295cc73cfb78..ee78d7800f45b1e21f587fb0db889d92cd062537 100644 (file)
@@ -46,6 +46,7 @@ library installed. This plugin assumes presence of
 .. code:: python
 
     PLUGINS += ['m.htmlsanity', 'm.images']
+    M_IMAGES_REQUIRE_ALT_TEXT = False
 
 `Images, figures`_
 ==================
@@ -60,6 +61,9 @@ directives and:
     `figure <{filename}/css/components.rst#figures>`_ styling.
 -   Removes the :rst:`:align:`, :rst:`:figwidth:` and :rst:`:scale:` options,
     as this is better handled by ``m.css`` features.
+-   To maintain accessibility easier, makes it possible to enforce :rst:`:alt:`
+    text for every image and figure by setting :py:`M_IMAGES_REQUIRE_ALT_TEXT`
+    to :py:`True`.
 
 You can add `additional CSS classes <{filename}/css/components.rst#images>`_ to
 images or figures via the :rst:`:class:` or :rst:`:figclass:` options,
index d2e0cc77d109740532c717089f029a5554486880..19dc945b2927308d4aca32b7e5abbc642aa97c6c 100644 (file)
@@ -52,12 +52,13 @@ class Image(Directive):
     -   .m-image CSS class added
     -   adding a outer container for clickable image to make the clickable area
         cover only the image
+    -   optionally dying when alt text is not present
     """
 
     required_arguments = 1
     optional_arguments = 0
     final_argument_whitespace = True
-    option_spec = {'alt': directives.unchanged,
+    option_spec = {'alt': directives.unchanged_required,
                    'name': directives.unchanged,
                    'class': directives.class_option,
                    'target': directives.unchanged_required}
@@ -95,6 +96,13 @@ class Image(Directive):
         if 'classes' in self.options: del self.options['classes']
         image_node = nodes.image(self.block_text, **self.options)
 
+        if not 'alt' in self.options and settings['M_IMAGES_REQUIRE_ALT_TEXT']:
+            error = self.state_machine.reporter.error(
+                    'Images and figures require the alt text. See the M_IMAGES_REQUIRE_ALT_TEXT option.',
+                    image_node,
+                    line=self.lineno)
+            return [error]
+
         self.add_name(image_node)
         if reference_node:
             if self.image_class:
@@ -209,6 +217,7 @@ class ImageGrid(rst.Directive):
 
 def configure(pelicanobj):
     settings['PATH'] = pelicanobj.settings.get('PATH', 'content')
+    settings['M_IMAGES_REQUIRE_ALT_TEXT'] = pelicanobj.settings.get('M_IMAGES_REQUIRE_ALT_TEXT', False)
 
 def register():
     signals.initialized.connect(configure)