def __write_blog_categories(self, posts): """Write all the blog posts in categories""" root = os.path.join(self.blog_dir,config.blog_category_dir) chron_template = self.template_lookup.get_template("chronological.mako") chron_template.output_encoding = "utf-8" #Find all the categories: categories = set() for post in posts: categories.update(post.categories) for category in categories: category_posts = [post for post in posts \ if category in post.categories] #Write category RSS feed self.__write_feed(category_posts,os.path.join( config.blog_path,config.blog_category_dir, category.url_name,"feed"),"rss.mako") self.__write_feed(category_posts,os.path.join( config.blog_path,config.blog_category_dir, category.url_name,"feed","atom"),"rss.mako") page_num = 1 while True: path = os.path.join(root,category.url_name, str(page_num),"index.html") try: util.mkdir(os.path.split(path)[0]) except OSError: pass f = open(path, "w") page_posts = category_posts[:config.blog_posts_per_page] category_posts = category_posts[config.blog_posts_per_page:] #Forward and back links if page_num > 1: prev_link = util.blog_path_helper( (config.blog_category_dir, category.url_name, str(page_num -1))) else: prev_link = None if len(category_posts) > 0: next_link = util.blog_path_helper( (config.blog_category_dir, category.url_name, str(page_num + 1))) else: next_link = None html = self.__template_render( chron_template, { "posts": page_posts, "prev_link": prev_link, "next_link": next_link }) f.write(html) f.close() #Copy category/1 to category/index.html if page_num == 1: shutil.copyfile(path,os.path.join( root,category.url_name, "index.html")) #Prepare next iteration page_num += 1 if len(category_posts) == 0: break
def __get_archive_links(self, posts): """Return a list of monthly archive links and nice name: """ d = {} #(link, name) -> number that month for post in posts: link = post.date.strftime(util.blog_path_helper("archive/%Y/%m/1")) name = post.date.strftime("%B %Y") try: d[(link, name)] += 1 except KeyError: d[(link, name)] = 1 l = [key+(value,) for key, value in d.items()] l = sorted(l, key=operator.itemgetter(0), reverse=True) return l
def __write_blog_chron(self, posts, root="page"): """Write all the blog posts in reverse chronological order Writes the first num_per_page posts to /root/1 Writes the second num_per_page posts to /root/2 etc """ root = root.lstrip("/") chron_template = self.template_lookup.get_template("chronological.mako") chron_template.output_encoding = "utf-8" page_num = 1 post_num = 0 html = [] while len(posts) > post_num: #Write the pages, num_per_page posts per page: page_posts = posts[post_num:post_num+config.blog_posts_per_page] post_num += config.blog_posts_per_page if page_num > 1: prev_link = util.blog_path_helper('%s/%s' % (root, page_num - 1)) else: prev_link = None if len(posts) > post_num: next_link = util.blog_path_helper('%s/%s' % (root, page_num + 1)) else: next_link = None page_dir = os.path.join(self.blog_dir,root,str(page_num)) util.mkdir(page_dir) fn = os.path.join(page_dir,"index.html") f = open(fn,"w") html = self.__template_render( chron_template, { "posts":page_posts, "next_link":next_link, "prev_link":prev_link }) f.write(html) f.close() page_num += 1
def __write_blog_first_page(self, posts): if not config.blog_custom_index: chron_template = self.template_lookup.get_template("chronological.mako") chron_template.output_encoding = "utf-8" page_posts = posts[:config.blog_posts_per_page] path = os.path.join(self.blog_dir,"index.html") logger.info("Writing blog index page: "+path) f = open(path,"w") if len(posts) > config.blog_posts_per_page: next_link = util.blog_path_helper(config.blog_pagination_dir+"/2") else: next_link = None html = self.__template_render( chron_template, { "posts": page_posts, "next_link": next_link, "prev_link": None }) f.write(html) f.close()