Blog post code block formatting

As part of learning how to code super slick sweet python and maintain a blog about it I had to come up with a way to make the display of the code blocks look professional. My first code block was ugly and I had to go through and insert <br> after every line. This was unacceptable. After much searching around the web I came across the obvious answer. There exists a package in Python called Pygments. Pygments comes with a script entitled “pygmentize.” This script can be used to convert your Python source into succulent looking html code viewable from any browser.

pygmentize -l python -f html -o filename.html filename.py

Pretty simple except for one thing. While it creates tags for all of the different types of text you might find in a Python script it does not create styles for those. So I made the styles myself. By opening the preferences in my Spyder editor and seeing the colored syntax highlighting I was able to make a one for one style for each syntax tag I found in the html file. So when you look at the code blocks you are in essence seeing exactly what I see in my editor.

Leave a comment