code: def format(self, soup):
          
          # Remove all the newline characters before a closing tag.
          for node in soup.findAll(text=True):
              if node.rstrip(" ").endswith("\n"):
                  node.replaceWith(node.rstrip(" ").rstrip("\n"))
          # Join the block elements lines into a single long line
          for tag in ['p', 'li']:
              for node in soup.findAll(tag):
                  text = unicode(node)
                  lines = [x.strip() for x in text.splitlines()]
                  text = ' '.join(lines)
                  node.replaceWith(BeautifulSoup.BeautifulSoup(text))
          soup = BeautifulSoup.BeautifulSoup(unicode(soup))
          # replace all <br/> tag by newline character
          for node in soup.findAll('br'):
              node.replaceWith("\n")
          soup = BeautifulSoup.BeautifulSoup(unicode(soup))
          return soup
docs: format a BeautifulSoup document
  
          This will transform the block elements content from
          multi-lines text into single line.
  
          This allow us to avoid having to deal with further text
          rendering once this step has been done.