Ejemplo n.º 1
0
    def process_drafts(self):
        """
        Traverse drafts dir looking for posts to publish
        """     

        tools.mkdirp(self.s.DRAFTS_PUBLISH_NOW_DIR)


        for f in os.listdir(self.s.DRAFTS_DIR):
            if f.endswith(self.s.MD_EXT):

                # skip files scheduled for publishing
                if os.path.exists(os.path.join(self.s.DRAFTS_PUBLISH_NOW_DIR, f)): continue


                # Create post instance from file
                logging.info("Creating post instance from file %s." % f)
                
                try:
                    post = Post(os.path.join(self.s.DRAFTS_DIR, f), self.s)
                except PostIsDraftException:
                    continue

                if post.status and (post.status.upper() == self.s.POST_STATUS_PUBLISH.upper()):

                    post.normalize()
                    self.prepare_post(post)

                    template = self.j2.get_template(self.s.PERMALINK_TEMPLATE)
                    html = template.render(
                        blog_title=self.s.BLOG_TITLE,
                        blog_url=self.s.BLOG_URL,
                        blog_description=self.s.BLOG_DESCRIPTION,
                        post=post, 
                        prev_page_url=None,
                        next_page_url=None,
                    )

                    fname = "%s%s" % (os.path.splitext(os.path.split(f)[1])[0], self.s.HTML_EXT)

                    self.write_html(self.s.DRAFTS_PREVIEW_DIR, fname, html)
                    logging.info("Wrote preview post to %s" % fname)
Ejemplo n.º 2
0
    def process_publish_now(self, publish_future=False):
        """
            Scans PUBLISH_NOW_DIR for post with 'publish' status.
            
            Args:
                publish_future:
                    if False, won't publish posts with date later than today.
                    if True, will publish everything in directory.        
        """
        
        for f in os.listdir(self.s.DRAFTS_PUBLISH_NOW_DIR):
            if f.endswith(self.s.MD_EXT):

                source_file = os.path.join(self.s.DRAFTS_PUBLISH_NOW_DIR, f)

                post = Post(source_file, self.s)
                
                if not publish_future:
                    # skip future posts
                    if self.is_in_future(post):
                        continue
                
                post.set_status(published=True)
                post.normalize()

                # Check layout:  post or page?
                if post.layout.lower()==self.s.POST_TYPE_PAGE.lower():

                    dest_fname = "%s%s" % (post.slug, self.s.MD_EXT)
                    dest_dir = self.s.PAGES_DIR
                    tools.mkdirp(dest_dir)

                    try:
                        shutil.copy(source_file, os.path.join(dest_dir, dest_fname))
                        os.remove(source_file)
                        self.remove_post_review(post)
                        
                    except IOError as e:
                        logging.error('Could not move file %s to %s. Error: %s.' % (source_file, 
                            os.path.join(dest_dir, dest_fname), e))

                    
                else:
                    sequence = self.get_next_sequence(post.date.year, post.date.month, post.date.day)
                    # output filename has form YYYYMMDD-p##-slug.md
                    if sequence > self.s.MAX_POSTS_PER_DAY:
                        raise Exception("Max posts per day is 99.  Got %d posts for %04d-%02d-%02d." % 
                                sequence, post.date.year, post.date.month, post.date.day
                            )


                    dest_fname = "%04d%02d%02d-p%02d-%s%s" % (post.date.year, 
                        post.date.month, post.date.day, 
                        sequence,
                        post.slug, self.s.MD_EXT)


                    # copy post to posts dir YYYY/MM
                    dest_dir = os.path.join(self.s.POSTS_DIR, "%04d" % post.date.year, "%02d" % post.date.month)

                    tools.mkdirp(dest_dir)

                    try:
                        shutil.copy(source_file, os.path.join(dest_dir, dest_fname))
                        os.remove(source_file)
                    except IOError as e:
                        logging.error('Could not move file %s to %s. Error: %s.' % (source_file, 
                            os.path.join(dest_dir, dest_fname), e))