Python Markdown Syntax

Posted on  by 



  1. Python Markdown Syntax Tutorial
  2. Python Markdown Syntax Interview
  3. Python In Markdown

In this little tutorial, I want to show you in 5 simple steps how easyit is to add code syntax highlighting to your blog articles.

Python

Markdown is great because of its support of code blocks. We've tied this in with Codebase's powerful syntax highlighting to provide language specific code blocks To use the syntax highlighting, you'll need to specify the language that you're using.

There are more sophisticated approaches using static site generators,e.g., nikola, but the focus hereis to give you the brief introduction of how it generally works.

All the files I will be using as examples in this tutorial can bedownload from the GitHub repository/rasbt/python_reference/tutorials/markdown_syntax_highlighting

  • The code highlighting syntax uses CodeHilite and is colored with Pygments. It follows the same syntax as regular Markdown code blocks, with ways to tell the highlighter what language to use for the code block. The language will be detected automatically, if possible. Or you can specify it on the first line with 3 colons and the language name.
  • How to Use Markdown Syntax with Python For most things you can think of, there is a python library available for it. So is naturally also the case for Markdown. There is a library called Python-Markdown that can easily be installed with pip using the following command.
  • Markdown is a lightweight Markup language with a plain text syntax. John Gruber developed the Markdown language in 2004 in a collaborative effort with Aaron Swartz, intending to enable people to.
  • Markdown is a lightweight Markup language with a plain text syntax. John Gruber developed the Markdown language in 2004 in a collaborative effort with Aaron Swartz, intending to enable people to.

Update:

The people atWebucator (aprovider of Python training classes) created a nice video tutorial fromthis blog post that nicely explains all the essential steps in less than4 minutes! Check it out on YouTube:

Python in markdown

1 - Installing packages

The two packages that we will use are

Just as the name suggests, Python-Markdown is the Python package that wewill use for the Markdown to HTML conversion. The second library,Pygments, will be used to add the syntax highlighting to the codeblocks.
Conveniently, both libraries can be installed via pip:

and

(For alternative ways to install the Python-Markdown package, please seethe documentation)

2 - Writing a Markdown document

Now, let us compose a simple Markdown document including some Pythoncode blocks in any/our favorite Markdown editor.

Note that the syntax highlighting does not only work for Python, butother programming languages.

Python Markdown Syntax Tutorial

So in the case of C++, for example:

Since the CodeHilite extension in Python-Markdown uses Pygments, everyprogramming language that is listedhere currently has support for syntaxhighlighting.

3 - Converting the Markdown document to HTML

After we created our Markdown document, we are going to usePython-Markdown directly from the command line to convert it into anHTML document.

Note that we can also import Python-Markdown as a module in our Pythonscripts, and it comes with a rich repertory of different functions,which are listed in the libraryreference.

The basic command line usage to convert a Markdown document into HTMLwould be:

However, since we want to have syntax highlighting for our Python code,we will use Python-Markdown’s CodeHiliteextensionby providing an additional -x codehilite argument on the command line:

Python

This will create the HTML body with our Markdown code converted to HTMLwith the Python code blocks annotated for the syntax highlighting.

4 - Generating the CSS

If we open thebody.htmlfile now, which we have created in the previous section, we will noticethat it doesn’t have the Python code colored yet.

What is missing is the CSS code for adding the colors to our annotatedPython code block. But we can simply create such a CSS file viaPygments from the command line.

Note that we usually only need to create thecodehilite.cssfile once and insert a link in all our HTML files that we created viaPython-Markdown to get the syntax coloring

5 - Insert into your HTML body

In order to include a link to thecodehilite.cssfile for syntax coloring in our converted HTML file, we have to add thefollowing line to the header section.

<link type='text/css' href='./codehilite.css'>

Python Markdown Syntax Interview

Now, we can insert the HTML body(body.html),which was created from our Markdown document, directly into our finalHTML file (e.g., our blog article template).

If we open ourfinal.htmlfile in our web browser now, we can the pretty Python syntaxhighlighting.

Useful links:

Python In Markdown

  • languages supported by Pygments





Coments are closed