Skip to main content

Create your own syntax highlighter/code beautifier with python and pygments

Now a days I have seen that there are lots and lots of people who want to share their code to this world and help people who are in need of that code(or for your personal satisfaction 😜 ). Even though sharing code is easy just by using a <pre> tag or a <code> tag, but sometimes many people including me feel comfortable when the code is neatly highlighted. 

I will show you a comparison between a neatly highlighted code and un-highlighted code.
Python Code not highlighted
Not highlighted code
Python Code highlighted with pygments
Highlighted Code
Okay don't worry if you don't have a code highlighter, you can create your own code beautifier/syntax highlighter and present your code in a neat and tidy format. To do this you just need to know basics of python and I will teach you the rest in this post.

Okay let's get started.

 To do syntax highlighting with python we will use a library called pygments. We are using this library because:
 We will start with installation of pygments:

Installing with pip

$ pip install pygments

Installing with easy_install

$ easy_install pygments

Installing manually

First download the package from pypi and then cd to the directory. Now when you are sure that you are in the pygments directory then you can type the following command:
$ python setup.py install

About Pygments

Pygments has four important components which work together to high light given code. They are as follows:
  •  Lexers: We all know that every programming language has its own reserved keywords. A lexer is the one which will identify these keywords in your program and then split the code depending on the programming language you have given.
  • Filter: After the lexer has split the keywords and other stuff then the code is sent to filter where it is filtered and processed in the background which is really not necessary for us to know at this point of time. 
  • Formatter: A formatter will take the tokens that are supplied by the filter and then it will convert it to HTML/LaTeX/RTF/ASCII sequence depending on your requirement.
  • Style: After the code is converted to required sequence, we also need style or CSS for HTML to make it look beautiful. That part of the work is taken care here.

Creating first code Beautifier

As you have now understood the basics of what is happening behind the scenes of pygments you can now create your own syntax highlighter within no time.
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments import highlight

programming_language = get_lexer_by_name('python')

raw_code = 'print("Programming is fun!")'

highlighted_code = highlight(raw_code, programming_language, HtmlFormatter())

print highlighted_code
I have saved above program as tutorial.py. If you will run above program either on command line or on IDLE you will get a result something like this:
python pygments from command line
pygments from command line
Okay lets first understand what happened before looking at the output.
In the first line I have imported get_lexer_by_name, a function which, when called with a programming language as a parameter will act like a lexer for the programming language. You can also directly import python lexer just by typing from pygments.lexers import PythonLexer which is equivalent to get_lexer_by_name('python'). If you want to import some other lexer then you can visit this page and find out your lexer: Available Lexers - Pygments
Next from pygments.formatters we have imported HtmlFormatter a formatter, which will format the given code and output it to a HTML Format.
Next we have imported the highlight function.
We have created a variable programming_language for the lexer.
We have saved our code that we need to highlight in a variable named raw_code.
In the next line we have highlighted our code calling the highlight function with raw_code, lexer and formatter as parameters.
In the last line we have printed the HTML Code we have generated.    
As we have used HtmlFormatter we have got the code ouput in HTML Format. If you were to get code in some other format other than HTML then you can see Pygments Formatters,  and import the required formatter.
Coming back, the code generated by running tutorial.py looks as follows:
<div class="highlight">
 <pre>
  <span></span>
  <span class="k">print</span>
  <span class="p">(</span>
  <span class="s2">&quot;Programming is fun!&quot;</span>
  <span class="p">)</span>
 </pre>
</div>
If you will save above code as .html file, open it in a browser then you will see something like this:
Screen shot of pygments without CSS
Screen Shot of pygments without CSS

 By now you should have understood on why the code is still black and white and not in a colorful way. 

Yes you have guessed it right!

The reason is because even though we have generated our HTML code we have not yet added the CSS(Style) information. To get the css information you can do as follows:

print HtmlFormatter().get_style_defs('.highlight')
 This will generate CSS styling information for the .highlight class.

Digging Deep

Till now we have seen the basics of Pygments, but we will dig a little deep and finish of this post.

Now see the following code and try to understand it:
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from pygments import highlight

programming_language = PythonLexer()

raw_code = 'print("Programming is fun!")'

formatter = HtmlFormatter(cssclass='radiusofcircle',linenos=True)

highlighted_code = highlight(raw_code,programming_language,formatter)

print(highlighted_code)
Lets first understand the above program before we will see the output:
In the first three lines we have imported the lexer, formatter, highlightner.
In the next line we have assigned our PythonLexer to variable programming_language.
As in the previous code in the next line we have written code and saved it to variable raw_code
 Next we have called HtmlFormatter with parameters cssclass = radiusofcircle which means that the output HTML should have class=radiusofcircle instead of default .highlight, the one we have got in the previous code. linenos=True which means that the code should also have line numbers.
All the remaining code is same as the previous code.
 Now run the above program and you will see code generated like this:
  <table class="radiusofcircletable">
    <tr>
      <td class="linenos">
        <div class="linenodiv">
          <pre>
1
</pre>
        </div>
      </td>

      <td class="code">
        <div class="radiusofcircle">
          <pre>
<span class="k">print</span><span class="p">(</span><span class=
"s2">"Programming is fun!"</span><span class="p">)</span>
</pre>
        </div>
      </td>
    </tr>
  </table>
I am assuming that you have observed the difference.

Summary

We have come to the end of the post and I think I have covered all the topics which are required for you to highlight your code. If you want to delve deep then you can visit the official documentation from here: Introduction and QuickStart - Pygments
  
As always I have tried to write this post in such a way that it is easy even for the beginners. If you have not understood anything or have any doubt then please do comment in the comment box below and I will be glad to help you. You can also contact me from here: Contact me.

Also please do comment on how I can improve this post so that it be easy for everyone to understand. Please don't hesitate to comment/contact me if I have made any typo or mistake in this post. Please do comment if you want me to add any topic to this post.

Thank you. Have a nice day😄

Popular posts from this blog

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed tag Us