Monday, September 7, 2015

How to highlight source code for displaying in HTML

In this post I will show you a useful web tool created by Alexander Kojevnikov that allows you to quickly and conveniently convert your source code to highlighted HTML so that you can place it on your website or blog.


Lets say you have some Python code snippet:

def convert(x):
    return(x*2.54)
## list of values to convert
vals = [31,24,15,6.3]
for each in vals:
    print convert(each)
   

What you really want is HTML that displays your source code as it appears in your development environment. For example with highlighted keywords like:


def convert(x):
    return(x*2.54)
## list of values to convert
vals = [31,24,15,6.3]
for each in vals:
    print convert(each) 

Try it

Now try it yourself, just go the the website http://hilite.me/ and type your source code in the box on the left, select the language of the source code and the output style, then just hit "Highlight!". You will get output HTML for displaying your source code for many different languages. For example the screen shot below shows how to create pretty HTML for the Python code snippet above as it would be displayed in the vim text editor using vim's default color scheme. You even have the option to display line numbers in the HTML!  Example screen shot:



 
 

Hilite.me is a useful web tool that you can easily use in a quick fashion to highlight and style source code of varying type to output in HTML, markdown, LateX, and others. With a bit of research you will find that everything done on the tool can easily be done on your system using the Pygments Python module. This may be useful especially in a workflow or if you do not have internet access. If there is interest in how to use Pygments in a workflow it will be a future post. Cheers

8 comments:

  1. Your blogs and its stuff magnetize me to return again n again.
    article source

    ReplyDelete
  2. This blog was extremely helpful. I really appreciate your kindness in sharing this with me and everyone else! vpn

    ReplyDelete
  3. I just want to let you know that I just check out your site and I find it very interesting and informative.. https://internetprivatsphare.de/prosieben-live-im-ausland-empfangen/

    ReplyDelete
  4. I really love this post I will visit again to read your post in a very short time and I hope you will make more posts like this.  https://www.debestevpn.nl

    ReplyDelete
  5. Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this. https://vpnveteran.com/

    ReplyDelete
  6. Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this. privatsphare

    ReplyDelete
  7. Very cozy looking rooms. Let me know if your going to Mexico. Oh and btw. you should read our Tipping in Mexico guide if you do. It will save you a lot of awkward moments. lemigliori vpn

    ReplyDelete