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.
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
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:
Problem In a webapp of mine I use markdown with the excellent Python-Markdown package. However, it doesn't support strikethrough by default. Solution The good news is that you can add 3rd-party. Compiler and tools for the Anaconda Python Language. If you want some text to show up exactly as you write it, without Markdown doing anything to it. This is the personal website of a data scientist and machine learning enthusiast with a big passion for Python and open source. Born and raised in Germany, now living in East Lansing, Michigan.
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.
So in the case of C++, for example:
Markdown Python Code
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
Spa saturdayanne 28 online, free games. 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:
Rununblocked games. 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.
Python Markdown Tab
<link type='text/css' href='./codehilite.css'>
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).
Python Markdown Tutorial
If we open ourfinal.htmlfile in our web browser now, we can the pretty Python syntaxhighlighting.
Useful links:
languages supported by Pygments