January 6, 2008
Adding accessiblity warnings to HTML-generating Lisp code
Image objects in HTML should have at least an ALT attribute for text browsers and screen readers, so let’s automate the checking:
(asdf:oos 'asdf:load-op 'cl-who) (defmacro demand-image-attrs (img want-attrs have-attrs) "Check an image's accessiblity information for completeness." (let ((missing-attrs (gensym))) `(let* ((,missing-attrs (loop for attr in ,want-attrs when (not (find attr ,have-attrs)) collect attr))) (unless (null ,missing-attrs) (warn "image ~S is missing the following attributes: ~{~A~^, ~}" ,img ,missing-attrs)))) (defmacro render-image (src &rest attrs) "Produce HTML code from an image location." (declare (type string src)) `(demand-image-attrs src (list :alt :title) attrs) `(cl-who:with-html-output (*standard-output*) (:img :src ,src ,@attrs))) CL-USER> (render-image "cat.png" :title "Tom") WARNING: image "cat.png" is missing the following attributes: ALT "<img src='cat.png' title='Tom' />"
Simple and effective.