From: Vladimír Vondruš Date: Tue, 24 Oct 2017 12:31:24 +0000 (+0200) Subject: m.images: make it possible to enforce alt text for all images and figures. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=5b1da14c42d15e605d6451df6d4859698750525c;p=blog.git m.images: make it possible to enforce alt text for all images and figures. --- diff --git a/doc/plugins/images.rst b/doc/plugins/images.rst index 88eacd7b..ee78d780 100644 --- a/doc/plugins/images.rst +++ b/doc/plugins/images.rst @@ -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, diff --git a/pelican-plugins/m/images.py b/pelican-plugins/m/images.py index d2e0cc77..19dc945b 100644 --- a/pelican-plugins/m/images.py +++ b/pelican-plugins/m/images.py @@ -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)