def get_post_by_url(url, app): ''' Pass in a post url (/code/apost) and get back the contents ''' # Get all the data needed for the rss feed. metaAbsPath = os.path.join(BLOG_SYS_PATH, "posts", url, 'meta.txt') bodyTemplatePath = os.path.join("posts", url, 'body.html') parser = ConfigParser.ConfigParser() parser.read(metaAbsPath) # Each meta file should begin with a [post] section metaData = dict(parser.items("post")) # Get rid of newlines in multi-line descriptions. metaData['description'] = metaData['description'].replace("\n", " ") description = markdown.markdown(metaData['description']) postTime = time.strptime(metaData['time'], "%Y-%m-%d %a %H:%M %p") postDict = { 'title' : metaData['title'], 'description' : description, 'url': url, 'pubDate': time.strptime(metaData['time'], "%Y-%m-%d %a %H:%M %p") } postDict['comments'] = postDict['url'] + "#comments" with app.test_request_context(): # Get the blog post body joequery.before_request() content = render_template(bodyTemplatePath, post=postDict) jQuery = PyQuery(content) body = jQuery("#blogPost .entry").eq(0).html() postDict['body'] = body return postDict
def write_index_pages(postsPerPage): for category in BLOG_CATEGORIES: i=1 posts = get_posts_by_category(app, postsPerPage, category=category) categoryPath = os.path.join(BLOG_SYS_PATH, "pages", category) try: os.makedirs(categoryPath) except OSError as exception: if exception.errno != errno.EEXIST: raise while posts: for post in posts: post['pubDate'] = time.strftime("%B %d, %Y", post['pubDate']) # needed for blog index pages to avoid broken links post['url'] = os.path.join("/", post['url']) pagePath = os.path.join(categoryPath, "page%d.static" % i) with app.test_request_context(): before_request() start = postsPerPage * i newposts = get_posts_by_category(app, postsPerPage, category, start) # Determine if we should display prev/next buttons prevPage = False nextPage = False if i>1: prevPage = i-1 if len(newposts) > 0: nextPage = i+1 # Determine the appropriate title tag to use. if i == 1: title = "Programming blog" else: title = "Programming blog | Page %d" % i blogIndexHTML = render_template("templates/blog_index_bodygen.html", posts=posts, prevPage=prevPage, nextPage=nextPage, title=title, category=category) # This keeps things "relatively" static while allowing for dynamic messages # in the header for things like ScreenX TV blogIndexTemplate = os.path.join(currentDir, "joequery", "blog", "templates", "blog_index.html") f = open(blogIndexTemplate, 'r') template = f.read() f.close() html = template.replace("REPLACEME", blogIndexHTML) f = open(pagePath, 'w') f.write(html) f.close() i += 1 posts = newposts print("Generated static blog pages")
def gen_rss_feed(app, postList): posts = copy.deepcopy(postList) # Get current time into RFC822 format for the last build date. lastBuild = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) # Alter the contents of the posts to satisfy XML/RSS requirements. for post in posts: post['title'] = post['title'] post['description'] = post['description'] post['body'] = post['body'] post['url'] = "http://joequery.me/%s" % post['url'] # RFC822 specifications. post['pubDate']=time.strftime("%a, %d %b %Y %H:%M:%S +0000",post['pubDate']) with app.test_request_context(): joequery.before_request() rss = render_template("templates/rssfeed.html", lastBuild=lastBuild, posts=posts) return rss