def save_post(file, category=''): class HighlighterRenderer(HtmlRenderer): def blockcode(self, text, lang): if not lang: return '\n<pre><code>{}</code></pre>\n'.format( houdini.escape_html(text.strip())) lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(text, lexer, formatter) if category == '': print('No tag! Abort.') return rndr = HighlighterRenderer() md = Markdown(rndr, MARKDOWN_EXTENSIONS) with open(file, 'r', encoding='utf-8') as f: title = '' for line in f: title = line[1:] break content_html = (md(f.read())) blog_id = PurePosixPath(file).stem # use the filename without the extension as the blog_id new_blog = BlogPost(title, category, content_html, blog_id) old = BlogPost.query_by_id(blog_id) if isinstance(old, BlogPost): print("The same blog id already exists. \nOverwriting......") old.delete() else: print("New blog. Posting...") new_blog.save() print("Successfully posted.") return
def delete_post(blog_id=''): if blog_id == '': blog_posts = BlogPost.get_all() last_blog = blog_posts[0] blog_id = last_blog.blog_id try: print('id: ', blog_id) BlogPost.delete_by_id(blog_id) except: print("Error when deleting!") return
def list_by_tag(tag): blog_posts = BlogPost.query_by_tag(tag) if len(blog_posts) == 0: print('No file was found by tag: %s!' % tag) for blog in blog_posts: print(blog_posts.index(blog)) blog.print()
def list_all(): try: blog_posts = BlogPost.get_all() print('Total: ', len(blog_posts)) for blog in blog_posts: print(blog_posts.index(blog)) blog.print() except: print('Error when list all files!\n') raise
def list_all_by_tag(tag, page='1'): all_blog_posts = BlogPost.query_by_tag(tag) current_page_info = Page_Info.create(page, all_blog_posts) template = TEMPLATE_ENV.get_template('home.html') return template.render(post_list=current_page_info.data, page_info=current_page_info)
def blogpost(blog_id): blog_post = BlogPost.query_by_id(blog_id) template = TEMPLATE_ENV.get_template('post.html') return template.render(post=blog_post)