Generate the whole static site.

        Iterates through all existing s2 pages, rendering and writing
        them (and copying all common files along). 
        It also generates the toc, a sitemap, and the atom feed
        etc. (in the future it should handle tags and categories)

def generate(self):
        '''Generate the whole static site.

        Iterates through all existing s2 pages, rendering and writing
        them (and copying all common files along). 
        It also generates the toc, a sitemap, and the atom feed
        etc. (in the future it should handle tags and categories)

        '''
        if self._dirs['base'] == None or not self._tree_ready:
            #there's NO base here or up the chain
            raise ValueError   #cannot generate!

        # wipe www dir & recreate
        self._wipe_www_dir()#copy common files
        #shutil.copytree(self.dirs['common'],
        #                os.path.join(self.dirs['www'],"common"))
        slist = glob.glob(os.path.join(self.dirs['common'],"*"))
        for fo in slist:
            rfn = os.path.split(fo)[1]
            if os.path.isdir(fo):
                shutil.copytree(fo, os.path.join(self.dirs['www'], rfn))
            else:
                shutil.copy(fo, self.dirs['www'])

        # init atom file
        title = self.site_config['site_title']
        if title == '':
            title = "<No title>"
        feed = AtomFeed(title=title,
                subtitle=self.site_config['site_subtitle'],
                feed_url= os.path.join( self.site_config['site_url'],"atom.xml"),
                url=self.site_config['site_url'],
                author=self.site_config['default_author'])


        themes_to_copy = []  # full paths!
        generated_page_info = []
        for slug in self._pages_to_generate():  #this list of pages is in reverse chrono order
            p = s2page.Page(self, slug, isslug=True)
            generated_page_info.append( {'slug': p.slug,
                                         'title':p.title,
                                         'date': p.creation_date,
                                         'in_toc': p.in_toc })
            t = p.theme_path
            if not t in themes_to_copy:
                themes_to_copy.append(t)
            # wipe destination.
            self._wipe_www_page(slug)
            pg_content = p.generate() #generate page
            # add atom entry
            try:
                cdd = datetime.strptime(p.creation_date, '%Y-%m-%d') # feed.add needs the dat in datetime format
            except:
                print "Wrong date format in page '%s'. It should be YYYY-MM-DD."%p.slug
                print "Site Generation stopped!!  correct the date and generate again."
                self._wipe_www_dir()
                sys.exit()
            feed.add(title= p.title,
                     content=pg_content,
                     content_type="html",
                     author=p.author,
                     url=os.path.join( self.site_config['site_url'],"atom.xml") ,
                     updated=cdd)

        # copy themes
        wthemesdir = os.path.join(self.dirs['www'],"themes")
        os.mkdir(wthemesdir)
        for d in themes_to_copy:
            dname = os.path.split(d)[1]
            destpath = os.path.join(wthemesdir, dname)
            shutil.copytree(d, destpath)
            # delete tpl files
            ttr = glob.glob(os.path.join(destpath,"*tpl"))
            for f in ttr:
                os.remove(f)

        # write atom file
        atomfile= codecs.open(os.path.join(self.dirs['www'],"atom.xml"), "w", encoding="utf-8", errors="xmlcharrefreplace")
        atomfile.write(feed.to_string())
        atomfile.close()

        # create front page/s
        #print "generated_page_info for gf ",generated_page_info
        ff = self.site_config['fixed_frontpage']
        if ff != None and ff != '':
            self._set_fixed_frontpage(ff)
        else:
            self.generate_front(generated_page_info)
        self._generate_site_map(generated_page_info)