Use Python to develop instance sharing for Markdown script extensions

Source: Internet
Author: User
about Markdown

As mentioned in the introductory note, Markdown is a lightweight "markup language" for writing, which replaces typography with concise syntax, rather than the word-processing software we use. Word or Pages has a lot of typography and font settings. It allows us to focus on the code word and use the "tag" syntax instead of the common typesetting format. For example, from the content to the format, and even illustrations, the keyboard can be all done. Currently, there are many editors that support Markdown syntax, including many websites (such as Pinterest) that support Markdown text entry. Markdown from writing to completion, export format as you like, you can export HTML format files for the site publishing, can also be very convenient to export PDF format, this format of the resume can be more favorable to HR. You can even use the CloudApp cloud Service tool to upload directly to a Web page to share your article, the world's largest lightweight blogging platform TUMBLR, and support the use of Markdown tools such as the Mou to edit and upload directly.

Python's markdown Extended development
Recently, using Python's markdown to write a document applet, because of the need to use some additional features, so the markdown has a few simple extensions, and then record the written template. Direct Sticker Code:

#encoding =utf-8 # #预处理器 from markdown.preprocessors Import preprocessor class Codepreprocessor (preprocessor): def run (SE                 LF, lines): New_lines = [] flag_in = False block = [] for line in lines:if line[:3]== '!!! ': flag_in = True block.append ('
'% Line[3:].strip ())       elif flag_in:         if Line.strip () and line[0]== '! ':           block.append (line[1:])         Else:           flag_in = False           block.append ('
') Block.append (line) new_lines.extend (block) block = [] Else:new_lines.appen D (line) if is new_lines and Block:new_lines = block return New_lines # #后置处理器 from Markdown.postprocessors Import Postprocessor class Codepostprocessor (postprocessor): def run (self, text): T_list = [] is in text. Split (' \ n '): if line[:5]== '

!< ': line = Line.lstrip ('

'). Replace ('

', ') [1:] T_list.append (line) return ' \ n '. Join (t_list) # #扩展主体类 from markdown.extensions import Extens Ion from Markdown.util Import etree class Codeextension (Extension): Def __init__ (self, configs={}): Self.config = Co Nfigs def Extendmarkdown (self, MD, md_globals): # #注册扩展, for Markdown.reset when extended while Reset md.registerextension (self) # #设置Preprocessor codepreprocessor = codepreprocessor () #print Md.preprocessors.keys () Md.preproce Ssors.add (' Codepreprocessor ', Codepreprocessor, ' unescape ') # #print md_globals # #markdown全局变量

For an in-depth look at the markdown extension, you can view official documents, but there are no examples, just manuals. But in general can know what composition, in the combination of the above file structure can be written out. Here's the code for the call:

#encoding =utf-8 Import markdown Import markdowncode  text = ""!!! Python!!def foo ():  # # #title "'  configs = {}  

The main extensions are 2 features:
One is to shape it like this:

Converted into:

  def foo ():  

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.