def parse(self, filename): ItemParser.parse(self, PageParser.MANDATORY_FIELDS, filename, PageParser.OPTIONAL_FIELDS) page = Page( self.info['title'], "%s.tpl" % self.info['template'] if 'template' in self.info else None) if 'static' in self.info: page.static = self.info['static'] author = AuthorManager.getInstance().get(self.info['author']) author.items.append(page) categories = [] for title in self.info['categories']: category = CategoryManager.getInstance().get(title) category.items.append(page) categories.append(category) tags = [] for title in self.info['tags']: tag = TagManager.getInstance().get(title) tag.items.append(page) tags.append(tag) page.datetime = self.info['date'] page.author = author page.categories = categories page.tags = tags page.abstract = self.abstract page.content = self.body # reset the state ItemParser.__init__(self) return page
def generate( self ): start = time.time( ) parser = PageParser( ) print "@ Parsing pages ..." for file in self.files: if re.match( '^.+\..+$', file ): filename = os.path.join( self.dbdir, file ) page = parser.parse( filename ) self.pages.append(page) print "@ Sorting pages by date ..." self.pages.sort( reverse=True, key=lambda p: p.datetime ) # delete output directory and recreate it if os.path.exists(self.config.outputpath ): print "@ Removing old '%s' path ..." % self.config.outputpath shutil.rmtree( self.config.outputpath ) print "@ Creating '%s' path ..." % self.config.outputpath os.mkdir( self.config.outputpath ) for source, destination in self.config.copypaths.items(): print "@ Importing '%s' to '%s' ..." % (source, destination) if os.path.isfile(source): shutil.copy( source, destination ) elif os.path.isdir(source): shutil.copytree( source, destination ) else: raise Exception("Unexpected type of '%s' ." % source ) if os.path.exists( os.path.join( self.config.tplpath, 'index.tpl' ) ): print "@ Creating index file ..." self.index = Page( 'index', 'index.tpl' ) self.index.addObjects( { 'pages' : self.pages, 'swg' : self } ) self.index.create() else: raise Exception( "No index template found." ) if os.path.exists( os.path.join( self.config.tplpath, '404.tpl' ) ): print "@ Creating 404 file ..." self.e404 = Page( '404', '404.tpl' ) self.e404.addObjects( { 'pages' : self.pages, 'swg' : self } ) self.e404.create() if os.path.exists( os.path.join( self.config.tplpath, 'feed.tpl' ) ): print "@ Creating feed.xml file ..." self.feed = Page( 'feed', 'feed.tpl' ) self.feed.addObjects( { 'pages' : self.pages, 'swg' : self } ) self.feed.extension = 'xml' self.feed.create() self.progress = ProgressBar( 0, len(self.pages) ) for page in self.pages: page.addObjects( { 'pages' : self.pages, 'swg' : self } ).create() self.progress.increment_amount() sys.stdout.write( "@ Rendering %d pages : %s\r" % ( len(self.pages), self.progress ) ) sys.stdout.flush() self.progress.update_amount( len(self.pages) ) sys.stdout.write( "@ Rendering %d pages : %s\r" % ( len(self.pages), self.progress ) ) sys.stdout.flush() print "\n", if os.path.exists( os.path.join( self.config.tplpath, 'sitemap.tpl' ) ): print "@ Creating sitemap.xml file ..." self.sitemap = Page( 'sitemap', 'sitemap.tpl' ) self.sitemap.addObjects( { 'index' : self.index, 'pages' : self.pages, 'swg' : self } ) self.sitemap.extension = 'xml' self.sitemap.create() print "@ Website succesfully generated in %s .\n" % time.strftime('%H:%M:%S', time.gmtime( time.time() - start ) ) if self.config.transfer is not None: os.system( self.config.transfer.encode( "UTF-8" ) )