#!/usr/bin/env python
#Adds trivial "alt=pix" tag to
, in order to
#satisfy "Bobby compliance". http://www.cast.org/bobby/
#example usage: bobby.py *.html
from sys import * # with this import sys.argv is simply argv
import re #with this import re.sub is still re.sub
#Make two "regular expression objects". Note the use of "raw strings" with
#a r" ", rather than " ". Raw strings do not convert \\ to a single \. So
#not really needed here. But raw strings are the safe habit for regular expressions.
#Note the 5 groups, meaning the patterns grouped in ( ).
re_img=re.compile(r"(^.*?)(<\s*img\s+src)(.*?)(>)(.*$)")
#This one is simple, just to check if "alt=" already exists:
re_alt=re.compile(r"alt=")
#in Python, argv[0] is the calling program, the rest of argv is the *.html glob
for file in argv[1:]:
print file #print name of file that is being modified
infile=open(file)
all=infile.readlines() #put the lines of the file into a list
infile.close()
outfile=open(file,'w') #reopen the file for writing
for line in all:
found_img=re_img.search(line) #search for html "