Beispiel #1
0
    def write_posts_to_file(self, posts, dir, fname, template, 
                    prev_page_url=None, next_page_url=None, tag=None):

        tools.mkdirp(dir)

        output_fname = os.path.join(dir, fname)

        # Render template
        template = self.j2.get_template(template)
        
        html = template.render(
            blog_title=self.s.BLOG_TITLE,
            blog_url=self.s.BLOG_URL,
            blog_description=self.s.BLOG_DESCRIPTION,
            posts=posts,
            prev_page_url=prev_page_url,
            next_page_url=next_page_url,
            tag=tag,
            pages=self.get_all_pages_permalinks(),
            archive=self.get_monthly_archives_permalinks()
        )

        # write post
        output_file = codecs.open(output_fname, "w", encoding=self.s.OUTPUT_ENCODING)
        output_file.write(html)
        output_file.close()

        logging.info("Wrote %d posts to %s." % (len(posts), output_fname))
Beispiel #2
0
    def write_html(self, dname, fname, html):

        tools.mkdirp(dname)

        # write post
        outf = codecs.open(os.path.join(dname, fname), "w", encoding=self.s.OUTPUT_ENCODING)
        outf.write(html)
        outf.close()
Beispiel #3
0
    def publish_pages(self, posts=None, force_publish=False):
        
        if force_publish:
            posts = [Post(os.path.join(self.s.PAGES_DIR, f), self.s) for f in os.listdir(self.s.PAGES_DIR) if f.endswith(self.s.MD_EXT)]

        for post in posts:

            post.content = smartypants(self.md_to_html(post.content))
            post.title = smartypants(post.title)

            html_fname = "%s%s" % (post.slug, self.s.HTML_EXT)
            html_dir = os.path.join(self.s.WWW_DIR, self.s.WWW_PAGES_URL)
            html_full_path = os.path.join(html_dir, html_fname)

            tools.mkdirp(html_dir)
            # TODO: check dir owner/permission
            self.write_single_post_to_file(post=post, fname=html_full_path, template=self.s.PAGES_TEMPLATE)
Beispiel #4
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)
Beispiel #5
0
    def publish_tags(self, posts=None, force_publish=False):

        
        if force_publish:
            file_posts = self.get_all_file_posts_by_date()
            
            posts = [ Post(f, self.s) for f in file_posts]


        for post in posts:
            post.content = smartypants(self.md_to_html(post.content))
            post.title = smartypants(post.title)


        tagged = {} 

        for post in posts:
            for tag in post.tags:
                if tag not in tagged:
                    tagged[tag.strip()] = [post ,]
                else:
                    tagged[tag.strip()].append(post)


        for tag in tagged.iterkeys():
            # no pagination yet...
            dest_dir = os.path.join(self.s.TAGGED_DIR, tag.strip())
            tools.mkdirp(dest_dir)

            self.write_posts_to_file(
                posts=tagged[tag],
                fname=self.s.TAGGED_PAGE,
                dir=dest_dir,
                template=self.s.TAGGED_TEMPLATE,
                prev_page_url=None,
                next_page_url=None,
                tag=tag
            )
Beispiel #6
0
    def publish_permalinks(self, posts=None, force_publish=False):


        if force_publish or posts is None:
            file_posts = self.get_all_file_posts_by_date()
            
            posts = [ Post(f, self.s) for f in self.get_all_file_posts_by_date()]

        for post in posts:
            post.content = smartypants(self.md_to_html(post.content))
            post.title = smartypants(post.title)


        for p in posts:
            html_fname = "%s%s" % (p.slug, self.s.HTML_EXT)
            html_dir = os.path.join(self.www_dir, 
                "%04d" % int(p.date.year), 
                "%02d" % int(p.date.month),
                "%02d" % int(p.date.day))
            html_full_path = os.path.join(html_dir, html_fname)

            tools.mkdirp(html_dir)
            # TODO: check dir owner/permission
            self.write_single_post_to_file(post=p, fname=html_full_path, template=self.s.PERMALINK_TEMPLATE)
Beispiel #7
0
    def publish_monthly_archive(self, posts=None, force_publish=False):
        """
        Publishes pages with monthly articles.
        """
        
        if force_publish:
            file_posts = self.get_all_file_posts_by_date()
            
            posts = [ Post(f, self.s) for f in self.get_all_file_posts_by_date()]

        for post in posts:
            post.content = smartypants(self.md_to_html(post.content))
            post.title = smartypants(post.title)



        c_month = None
        c_year = None
        batch = []
        publish = False

        for post in posts:

            if (c_month != post.date.month) or (c_year != post.date.year):
                if batch:

                    dest_dir = os.path.join(self.s.WWW_DIR, "%04d" % c_year, "%02d" % c_month )
                    tools.mkdirp(dest_dir)
                    fname = self.s.ARCHIVE_PAGE

                    self.write_posts_to_file(
                        posts=batch,
                        fname=fname,
                        dir=dest_dir,
                        template=self.s.ARCHIVE_TEMPLATE,
                        prev_page_url=None,
                        next_page_url=None
                    )

                batch = [ ]
                c_month = post.date.month
                c_year = post.date.year

            batch.append(post)

        if batch:

            dest_dir = os.path.join(self.s.WWW_DIR, "%04d" % c_year, "%02d" % c_month )
            tools.mkdirp(dest_dir)
            fname = self.s.ARCHIVE_PAGE

            self.write_posts_to_file(
                posts=batch,
                fname=fname,
                dir=dest_dir,
                template=self.s.ARCHIVE_TEMPLATE,
                prev_page_url=None,
                next_page_url=None
            )

        # Write archive index page
        #       self.write_single_post_to_file(
        #           post=None,
        #           fname=os.path.join(self.s.WWW_DIR, self.s.ARCHIVE_INDEX_PAGE),
        #           template=self.s.ARCHIVE_INDEX_TEMPLATE,
        #       )
        self.write_posts_to_file(
            posts=posts,
            fname=os.path.join(self.s.WWW_DIR, self.s.ARCHIVE_INDEX_PAGE),
            dir=self.s.WWW_DIR,
            template=self.s.ARCHIVE_INDEX_TEMPLATE,
            prev_page_url=None,
            next_page_url=None
        )
Beispiel #8
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))