|
| 1 | +[Sebastian Raschka](http://sebastianraschka.com) |
| 2 | + |
| 3 | +last updated: 05/28/2014 |
| 4 | + |
| 5 | +<hr> |
| 6 | +I would be happy to hear your comments and suggestions. |
| 7 | +Please feel free to drop me a note via |
| 8 | +[twitter ](https://twitter.com/rasbt), [email ](mailto:[email protected]), or [google+ ](https://plus.google.com/118404394130788869227). |
| 9 | +<hr> |
| 10 | + |
| 11 | + |
| 12 | +# 5 simple steps for converting Markdown documents into HTML and adding Python syntax highlighting |
| 13 | + |
| 14 | +<br> |
| 15 | +<br> |
| 16 | + |
| 17 | +In this little tutorial, I want to show you in 5 simple steps how easy it is to add code syntax highlighting to your blog articles. |
| 18 | + |
| 19 | +There are more sophisticated approaches using static site generators, e.g., [nikola](https://github.com/getnikola/nikola), but the focus here is to give you the brief introduction of how it generally works. |
| 20 | + |
| 21 | + |
| 22 | +All the files I will be using as examples in this tutorial can be download from the GitHub repository [/rasbt/python_reference/tutorials/markdown_syntax_highlighting](https://github.com/rasbt/python_reference/tutorials/markdown_syntax_highlighting) |
| 23 | + |
| 24 | +<br> |
| 25 | +<br> |
| 26 | + |
| 27 | +##1 - Installing packages |
| 28 | + |
| 29 | +The two packages that we will use are |
| 30 | + |
| 31 | +- [Python-Markdown](http://pythonhosted.org/Markdown/) |
| 32 | + |
| 33 | +- [Pygments](http://pygments.org) |
| 34 | + |
| 35 | +Just as the name suggests, Python-Markdown is the Python package that we will use for the Markdown to HTML conversion. |
| 36 | +The second library, Pygments, will be used to add the syntax highlighting to the code blocks. |
| 37 | +Conveniently, both libraries can be installed via `pip`: |
| 38 | + |
| 39 | + |
| 40 | + pip install markdown |
| 41 | + |
| 42 | +and |
| 43 | + |
| 44 | + pip install Pygments |
| 45 | + |
| 46 | + |
| 47 | +(For alternative ways to install the Python-Markdown package, please see [the |
| 48 | +documentation](http://pythonhosted.org/Markdown/install.html)) |
| 49 | + |
| 50 | + |
| 51 | +<br> |
| 52 | +<br> |
| 53 | + |
| 54 | +##2 - Writing a Markdown document |
| 55 | + |
| 56 | +Now, let us compose a simple Markdown document including some Python code blocks in any/our favorite Markdown editor. |
| 57 | + |
| 58 | + |
| 59 | +--> [**some_markdown.md**](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/some_markdown.md) |
| 60 | + |
| 61 | +<div style="padding:6px; color: grey; background-color: white; border: black 1px solid"> |
| 62 | +<pre> |
| 63 | + |
| 64 | +##This is a test |
| 65 | + |
| 66 | +Code blocks must be indented by 4 whitespaces. |
| 67 | +Python-Markdown has a auto-guess function which works |
| 68 | +pretty well: |
| 69 | + |
| 70 | + print("Hello, World") |
| 71 | + # some comment |
| 72 | + for letter in "this is a test": |
| 73 | + print(letter) |
| 74 | + |
| 75 | +In cases where Python-Markdown has problems figuring out which |
| 76 | +programming language we use, we can also add the language-tag |
| 77 | +explicitly. One way to do this would be: |
| 78 | + |
| 79 | + :::python |
| 80 | + print("Hello, World") |
| 81 | + |
| 82 | +or we can highlight certain lines to |
| 83 | +draw the reader's attention: |
| 84 | + |
| 85 | + :::python hl_lines="1 5" |
| 86 | + print("highlight me!") |
| 87 | + # but not me! |
| 88 | + for letter in "this is a test": |
| 89 | + print(letter) |
| 90 | + # I want to be highlighted, too! |
| 91 | + |
| 92 | +</pre> |
| 93 | +</div> |
| 94 | + |
| 95 | + |
| 96 | +<br> |
| 97 | +<br> |
| 98 | + |
| 99 | +## 3 - Converting the Markdown document to HTML |
| 100 | + |
| 101 | + |
| 102 | +After we created our Markdown document, we are going to use Python-Markdown directly from the command line to convert it into an HTML document. |
| 103 | + |
| 104 | +Note that we can also import Python-Markdown as a module in our Python scripts, and it comes with a rich repertory of different functions, which are [listed in the library reference](https://pythonhosted.org/Markdown/reference.html). |
| 105 | + |
| 106 | +The basic command line usage to convert a Markdown document into HTML would be: |
| 107 | + |
| 108 | + python -m markdown input.md > output.html |
| 109 | + |
| 110 | +However, since we want to have syntax highlighting for our Python code, we will use Python-Markdown's [CodeHilite extension](http://pythonhosted.org/Markdown/extensions/code_hilite.html) by providing an additional `-x codehilite` argument on the command line: |
| 111 | + |
| 112 | + |
| 113 | + python -m markdown -x codehilite some_markdown.md > body.html |
| 114 | + |
| 115 | +This will create the HTML body with our Markdown code converted to HTML with the Python code blocks annotated for the syntax highlighting. |
| 116 | + |
| 117 | + |
| 118 | +<br> |
| 119 | +<br> |
| 120 | + |
| 121 | +##4 - Generating the CSS |
| 122 | + |
| 123 | +If we open the [**body.html**](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/body.html) file now, which we have created in the previous section, we will notice that it doesn't have the Python code colored yet. |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +What is missing is the CSS code for adding the colors to our annotated Python code block. But we can simply create such a CSS file via `Pygments` from the command line. |
| 128 | + |
| 129 | + pygmentize -S default -f html > codehilite.css |
| 130 | + |
| 131 | +Note that we usually only need to create the [**codehilite.css**](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/codehilite.css) file once and insert a link in all our HTML files that we created via Python-Markdown to get the syntax coloring |
| 132 | + |
| 133 | + |
| 134 | +<br> |
| 135 | +<br> |
| 136 | + |
| 137 | + |
| 138 | +## 5 - Insert into your HTML body |
| 139 | + |
| 140 | + |
| 141 | +In order to include a link to the [codehilite.css](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/codehilite.css) file for syntax coloring in our converted HTML file, we have to add the following line to the header section. |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +`<link rel="stylesheet" type="text/css" href="./codehilite.css">` |
| 146 | + |
| 147 | + |
| 148 | +Now, we can insert the HTML body ([body.html](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/body.html)), which was created from our Markdown document, directly into our final HTML file (e.g., our blog article template). |
| 149 | + |
| 150 | + |
| 151 | +[**template.html**](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/template.html): |
| 152 | + |
| 153 | +<code> |
| 154 | + |
| 155 | + <!DOCTYPE html> |
| 156 | + <html lang="en"> |
| 157 | + |
| 158 | + <head> |
| 159 | + <meta charset="utf-8"> |
| 160 | + <link rel="stylesheet" type="text/css" href="./codehilite.css"> |
| 161 | + </head> |
| 162 | + |
| 163 | + <body> |
| 164 | + |
| 165 | + <-- converted HTML contents go here |
| 166 | + |
| 167 | + </body> |
| 168 | + </html> |
| 169 | + |
| 170 | +</code> |
| 171 | + |
| 172 | +If we open our [**final.html**](https://github.com/rasbt/python_reference/blob/master/tutorials/markdown_syntax_highlighting/template.html) file in our web browser now, we can the pretty Python syntax highlighting. |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
0 commit comments