예제 #1
0
    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
예제 #2
0
    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
예제 #3
0
class Engine:
    __instance = None

    def __init__(self):
        self.config   = Config.getInstance()
        self.dbdir    = os.path.join( self.config.dbpath, 'pages' )
        self.files    = []
       
        if os.path.exists( self.dbdir ):
            for folder, subdirs, files in os.walk( self.dbdir ):
                for fname in files:
                    self.files.append( os.path.realpath( os.path.join( folder, fname ) ) ) 

        self.path     = os.path.dirname( os.path.realpath( __file__ ) )
        self.pages    = []
        self.progress = None
        self.statics  = None
        self.index    = None
        self.e404     = None
        self.sitemap  = None
        self.feed     = None

    def getPageByTitle( self, title, caseSensitive = True ):
        lwr_title = title.lower() if caseSensitive is False else None
        for page in self.pages:
            if page.title == title or (caseSensitive is False and page.title.lower() == lwr_title):
                return page

        return None

    def getStaticPages( self ):
        if self.statics is None:
            self.statics = filter( lambda page: page.static is True, self.pages )

        return self.statics

    def new( self ):
        newitem = os.path.join( self.dbdir, "%s.md" % self.config.now.strftime("%Y-%m-%d %H:%M:%S") )
        fd      = open( newitem, 'w+t' )

        fd.write( """\
Date: %s
Author:
Categories:
Tags:
Title:

""" % self.config.now.strftime("%Y-%m-%d %H:%M:%S") )

        fd.close()
        
        os.system( "%s %s" % ( self.config.editor, newitem ) )

        if os.path.exists( newitem ):
            print "@ Item '%s' created, you can now regenerate the website ." % newitem
        else:
            print "@ Item was not saved, quitting ."

    def create( self, destfolder ):
        if os.path.exists(destfolder):
            sys.exit( "@ The folder '%s' already exists, operation interrupted for security reasons." % destfolder )  
        else:
            print "@ Creating SWG basic website structure inside the '%s' folder ..." % destfolder

            shutil.copytree( os.path.join( self.path, 'basic' ), destfolder )

            print """\
@ Basic website initialized, now run:

    cd %s
    swg --generate

To generate the html contents or:

    cd %s
    swg --serve

To test the website locally.""" % (destfolder,destfolder)

    def serve( self ):

        class SWGServer(SocketServer.TCPServer):
            allow_reuse_address = True

        self.config.siteurl = 'http://*****:*****@ Serving the site on http://localhost:8080/ press ctrl+c to exit ..."
        
        try:
            SWGServer( ("",8080), SimpleHTTPServer.SimpleHTTPRequestHandler ).serve_forever()
        except KeyboardInterrupt:
            print "\n@ Bye :)"

    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" ) )
        
    @classmethod
    def getInstance(cls):
        if cls.__instance is None:
            cls.__instance = Engine()
        return cls.__instance
예제 #4
0
    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" ) )