Posted By

noah on 07/03/07


Tagged

html alt filter images accessibility standards optimization 2001 beginnersperlorg


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hollowmyth


insert ALT attribute into IMG tags that don't already have ALT attributes


 / Published in: Perl
 

At one point I wanted to give every image on my site an ALT tag. I did, with some help from [email protected]

  1. #/usr/local/bin/perl -w
  2. use strict;
  3.  
  4.  
  5. ############################################################
  6. # #
  7. # #
  8. # #
  9. # NOAH SUSSMAN #
  10. # #
  11. # insert_alt #
  12. # #
  13. # Created 5/11/01 at 01:38 PM #
  14. # #
  15. # Insert ALT element into IMG tags that lack it. The alt #
  16. # text inserted is identical to the contents of the <TITLE>#
  17. # tag -- or not. #
  18. # #
  19. # #
  20. ############################################################
  21.  
  22. =item THIS NEEDS TO BE ADJUSTED SO IT WORKS WITH JSP
  23. (02:35:50) VERSUSearth: I'll have to adjust my insert_alt script to take JSP into account next time
  24. (02:36:35) mitiege: yep- I'm guessing you are looking for the first closing sign and inserting before that..
  25. (02:36:47) mitiege: a simple fix would be to put the alt first in the img tag...
  26. (02:37:06) VERSUSearth: yeah that's probably a good idea
  27. =cut
  28.  
  29.  
  30. $^I=".bk";
  31.  
  32. #undef $/; # read in whole file, not just one line
  33.  
  34. my $text = "" ; #Insert blank alt attribute
  35.  
  36. while (<>) {
  37.  
  38. #m{<title>(.*?)</title>}ix;
  39. #my $text = $1; #Use the document title as the ALT text
  40.  
  41. unless (m{<img.*?alt=.*?>}ix){
  42. s{(<img)(.*?)>}{$1$2 alt="$text">}gsix;
  43. }
  44. print "$_";
  45.  
  46. }

Report this snippet  

You need to login to post a comment.