<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>Fenced Code Block Extension — Python Markdown</title> <link rel="stylesheet" href="../default.css" type="text/css"> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../siteindex.html" title="General Index">index</a></li> <li class="right"> <a href="footnotes.html" title="Footnotes Extension" accesskey="N">next</a> |</li> <li class="right"> <a href="definition_lists.html" title="Definition List Extension" accesskey="P">previous</a> |</li> <li><img src="../py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python Markdown v2.3.1 documentation</a> »</li> <li><a href="index.html">Extensions</a> »</li> <li><a href="fenced_code_blocks.html">Fenced Code Block Extension</a> »</li> </ul> </div> <!-- .related --> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <h1 id="fenced-code-blocks">Fenced Code Blocks</h1> <h2 id="summary">Summary</h2> <p>This extension adds a secondary way to define code blocks which overcomes a few limitations of the indented code blocks.</p> <p>This extension is included in the standard Markdown library.</p> <h2 id="syntax">Syntax</h2> <p>Fenced Code Blocks are defined using the syntax established in <a href="http://www.michelf.com/projects/php-markdown/extra/#fenced-code-blocks">PHP Markdown Extra</a>.</p> <p>Thus, the following text (taken from the above referenced PHP documentation):</p> <pre><code>This is a paragraph introducing: ~~~~~~~~~~~~~~~~~~~~ a one-line code block ~~~~~~~~~~~~~~~~~~~~ </code></pre> <p>Fenced code blocks can have a blank line as the first and/or last line of a code block and they can also come immediately after a list item without becoming part of the list.</p> <p>In addition to PHP Extra's syntax, you can define the language of the code block for use by syntax highlighters etc. The language will be assigned as a class attribute of the <code><code></code> element in the output. Therefore, you should define the language as you would a css class - <code>.language</code>. For consistency with other markdown syntax, the language can <em>optionally</em> be wrapped in curly brackets:</p> <pre><code>~~~~{.python} # python code ~~~~ ~~~~.html <p>HTML Document</p> ~~~~ </code></pre> <p>The above will output:</p> <pre><code><pre><code class="python"># python code </code></pre> <pre><code class="html">&lt;p&gt;HTML Document&lt;/p&gt; </code></pre> </code></pre> <p><a href="http://github.github.com/github-flavored-markdown/">Github</a>'s tilde (```) syntax is also supported:</p> <pre><code>```python # more python code ``` </code></pre> <h2 id="usage">Usage</h2> <p>From the Python interpreter:</p> <pre><code>>>> html = markdown.markdown(text, ['fenced_code']) </code></pre> <p>If you would like to have your fenced code blocks highlighted with the <a href="code_hilite.html">CodeHilite</a> extension, simply include that extension and the language of your fenced code blocks will be passed in and highlighted appropriately.</p> <pre><code>>>> html = markdown.markdown(text, ['fenced_code', 'codehilite']) </code></pre> </div> <!-- .body --> </div> <!-- .bodywrapper --> </div> <!-- .documentwrapper --> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3>Table Of Contents</h3> <div class="toc"> <ul> <li><a href="#fenced-code-blocks">Fenced Code Blocks</a><ul> <li><a href="#summary">Summary</a></li> <li><a href="#syntax">Syntax</a></li> <li><a href="#usage">Usage</a></li> </ul> </li> </ul> </div> <h4>Previous topic</h4> <p class="topless"><a href="definition_lists.html" title="previous chapter">Definition List Extension</a></p> <h4>Next topic</h4> <p class="topless"><a href="footnotes.html" title="next chapter">Footnotes Extension</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="https://github.com/waylan/Python-Markdown/issues" >Report a Bug</a></li> <li><a href="fenced_code_blocks.txt" rel="nofollow">Show Source</a></li> </ul> </div> <!-- .sphinxsidebarwrapper --> </div> <!-- .sphinxsidebar --> <div class="clearer"></div> </div> <!-- .document --> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../siteindex.html" title="General Index">index</a></li> <li class="right"> <a href="footnotes.html" title="Footnotes Extension" accesskey="N">next</a> |</li> <li class="right"> <a href="definition_lists.html" title="Definition List Extension" accesskey="P">previous</a> |</li> <li><img src="../py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="../index.html">Python Markdown v2.3.1 documentation</a> »</li> <li><a href="index.html">Extensions</a> »</li> <li><a href="fenced_code_blocks.html">Fenced Code Block Extension</a> »</li> </ul> </div> <!-- .related --> <div class="footer">© 2010-2012 Python Markdown Project</div> </body> </html>