Learn how to use google code prettify to add code highlighting or code snippets to blog or website. Get Dark custom google code prettify theme of Pc-Hub.
Hey guys..checkout my new tutorial on how to add code snippets using Google Code Prettify. You can directly visit my github repo to checkout the Dark Theme for your code snippet or you can follow the tutorial to learn in details. And if you face any problem at all or if you have any suggestions then please leave a comment down below ^_^
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality✓ Free Actions
Free to watch • No registration required • HD streaming
The first thing I’ve been looking into after creating this Tumblr was:
How can I get syntax highlighting to work?
I remembered a great JavaScript (remember, I said I’d be looking into it) module by Google called Google code prettify. From that I tried to integrate it into Tumblr which seems to be working as expected.
For those who do not know this library, it actually detect the type of code and color it automatically.
How to achieve that?
Including the library and CSS
In order for this to work we will need to include the JavaScript library so as the CSS with all the different code styles.
Running the script on page load
What we want know is to run this JavaScript whenever we load our page, the solution I picked was to use the body tag “onload" event as shown below:
<body onload="prettyPrint()">
Defining the tag and class
Now the script is running but we need to define a little style so the code won’t be running out of your article, you wouldn’t want that would you?
For that you can add anywhere in your style tag:
pre.prettyprint {
overflow-x: auto; margin: 5px 20px 20px;
}
Using it
<pre class="prettyprint"> Your code here </pre>
Here it is, working well. It’s not the only solution, just one that fit my needs :)
Although syntax highlighting may be superfluous to effective coding, I think it makes code easier to read and understand - so I needed a way to do it in Tumblr. A quick Google search yielded a post at Snippets of Code to enable syntax highlighting using Google’s `prettify.js`. I modified the jquery function to support a second class of `` tag (so that I could style DOS or Unix-like shells separately), and to only add the "prettyprint" class to paired `
<!-- For Syntax Highlighting --> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <link rel="stylesheet" type="text/css" href="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css"></link> <script src="http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js"></script> <script src="http://google-code-prettify.googlecode.com/svn/trunk/src/lang-css.js"></script> <script> function styleCode() { if (typeof disableStyleCode != "undefined") { return; } var makePretty = false; $("pre").children("code").each(function() { if (!$(this).hasClass("shell")){ if (!$(this).hasClass("prettyprint") ) { $(this).addClass("prettyprint"); makePretty = true; } } }); if (makePretty) { prettyPrint(); } //prettyPrint() is externally defined } $(function() { styleCode(); }); </script>
This snippet links the `jquery-latest.min.js`, `prettify.css`, `prettify.js`, and the css extension `lang-css.js` into your page, then looks for all `` tags and adds the "prettyprint" class if the code isn't the "shell" class (which we'll define and use later). A good reference of supported languages and usage can be found in the prettify [README](http://google-code-prettify.googlecode.com/svn/trunk/README.html). Next, the default Tumblr CSS definitions need to be extended to display blocks of code differently from inline code (which is convenient if you use markdown in Tumblr). Adding the following definitions will allow you to use `
` around any `` block (whether it's to be used for source code or a shell). Keep in mind that the `code.shell` class overlaps block settings from `pre.block`, which is a bit hack-ish (read: may break on some browsers - I haven't tested it much). `CSS`
As you can see (hopefully), it works pretty well. I hope this helps anyone else trying to get syntax highlighting working. #### Edit: Modifying the default theme CSS for prettify (found [here](http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css)) by removing `list-style-type: none` (source: [StackOverflow](http://stackoverflow.com/questions/8399547/how-to-add-line-numbers-to-all-lines-in-google-prettify), eliminating the `background:#F0F0F0;` line and setting `white-space:pre-wrap` in the modified `prettyprint` CSS on tumblr, and changing the javascript function as follows will enable line numbers with banded rows. `javascript`
function styleCode() { if (typeof disableStyleCode != "undefined") { return; } var makePretty = false; $("pre").children("code").each(function() { if (!$(this).hasClass("shell")){ // Add prettyprint to all pre blocks with "code" tag children if (!$(this).hasClass("prettyprint")) { $(this).addClass("prettyprint"); makePretty = true; // Add linenums if (!$(this).hasClass("linenums")) { $(this).addClass("linenums"); } } } }); if (makePretty) { prettyPrint(); } //prettyPrint() is externally defined } $(function() { styleCode(); });
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality✓ Free Actions
Free to watch • No registration required • HD streaming
Top Posts Tagged with #google code prettify | Tumlook