# L-38 MCS 275 Fri 18 Apr 2008 : boldtext.py

# Illustration of HTMLParser to parse an html page.
# Writes everything in bold to screen.

from HTMLParser import HTMLParser
from urllib import urlopen

class BoldText(HTMLParser):
   """
   Exports a switch for bold text.
   """
   def __init__(self):
      """
      Initializes the switch to False.
      """
      HTMLParser.__init__(self)
      self.isbold = False

   def handle_starttag(self, tag, attrs):
      """
      Looks for tags equal to 'b'
      and flips on the isbold flag.
      """
      if tag == 'b': self.isbold = True

   def handle_endtag(self, tag):
      """
      Looks for tags equal to 'b'
      and flips off the isbold flag.
      """
      if tag == 'b': self.isbold = False

def main():
   """
   Opens a web page and parses it.
   """
   page = 'http://docs.python.org/lib/module-HTMLParser.html'
   print 'opening %s ...' % page
   f = urlopen(page)
   p = BoldText()
   s = ''
   while True:
      data = f.read(1)
      if data == '': break
      p.feed(data)
      if p.isbold:
         s = s + data
      else:
         if s != '': print s
         s = ''

if __name__=="__main__": main()
