我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用markdown2.Markdown()。
def save(self, commit=True): author = self.cleaned_data['author'] title = self.cleaned_data['title'] content = self.cleaned_data['content'] markdown = Markdown() html = markdown.convert(content) post = Post(author=author, title=title, content=content, html=html) post.save() tags_text = self.cleaned_data['tags'] tag_names = split_tags(tags_text) for tag_name in tag_names: tag_name.strip() if 0 < len(tag_name) < 16: tag = self.create_and_get(tag_name) post.tags.add(tag) return self.instance
def getMessage(self): msgAsMarkdown = "" subject = "" # for each line in file # set the first line to message # put the rest into a long string f = open(self.templatePath,"r") lines = f.readlines() firstLineOfMessage = 0 for line in lines: firstLineOfMessage = firstLineOfMessage + 1 if line == '\n': break for i in range(firstLineOfMessage,len(lines)): msgAsMarkdown = msgAsMarkdown + lines[i] markdowner = Markdown() message = markdowner.convert(msgAsMarkdown) f.close() return message
def convert_markdown(request): markdown = Markdown() converted_string = markdown.convert(request.POST['text']) return HttpResponse(converted_string)
def get_markdown(): """ Find and set a valid markdown parser Checks for one of these parsers: - pandoc - markdown2 """ markdown = None if shutil.which('pandoc'): markdown = lambda x: subprocess.run( ["pandoc", "-f", "markdown", "-t", "html"], input=x.encode("utf-8"), stdout=subprocess.PIPE).stdout.decode("utf-8") else: try: import markdown2 _md_extras = [ "code-friendly", "fenced-code-blocks", "footnotes", "header-ids", ] markdown = markdown2.Markdown(extras=_md_extras).convert except ImportError: pass if markdown is None: print("No markdown parser found. Will serve as plain text.") return markdown