I have a Tumblr-managed blog, on which I post a lot of Python-related posts. How do I make the Python snippets in my posts show color-coded?
3 Answers
If you have too many, I would say go for google-code-prettify.
All you have to do is to include some javascript, css and then you're done.
Check some examples and here is the setup:
- Download a distribution
- Include the script and stylesheets in your document (you will need to make sure the css and js file are on your server, and adjust the paths in the script and link tag)
<link href="prettify.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="prettify.js"></script>
- Add onload="prettyPrint()" to your document's body tag.
- Modify the stylesheet to get the coloring you prefer
-
The only problem I had with this method is that it expect you to mark your
code
tags with a specificclass
, which is not nice if you are using Markdown syntax. Luckily, it's not a problem to do it using JS on page load. Commented Dec 12, 2010 at 7:48
You can use hilite.me
hilite.me converts your code snippets into pretty-printed HTML format, easily embeddable into blog posts, emails and websites.
Just copy the source code to the left pane, select the language and the color scheme, and click "Highlight!". The HTML from the right pane can now be pasted to your blog or email, no external CSS or Javascript files are required.
I use Github's gist to do this. Paste in and create the gist and you can then embed them in any web page including tumblr.
-
There's a problem here, it needs JS so if a user doesn't have it activated, or if a user is a search bot, your code stays invisible. Commented Dec 12, 2010 at 7:52
-
I wish I could upvote this more than once. @MladenJablanović You can't use the majority of the web without JavaScript nowadays. I think that is not really an issue. Commented Jan 21, 2012 at 21:40
-
Can you give more detail on how to do this? I just tried it and gist renders the embed as
script
elements - Tumblr escapes these, even if you use HTML mode. Any ideas?– cottsakCommented Aug 16, 2012 at 8:38