Some tutorials encourage the modification of your template's HTML code. I don't like this method, because your HTML code changes are reverted as soon as you switch to another theme or modify the CSS properties.
Here is a more elegant way:
- In the Blogger.com Blog Administration, click "Layout" in the left navigation bar.
- Click "Add Gadget" somewhere in your layout, preferably somewhere at the bottom of the page.
- Choose "HTML/JavaScript" from the "Basics" tab.
- Give your gadget a name (i.e. "PrettyPrint") and paste the code from below into the "Content" text area.
- To embed source code, just place it inside of a <pre class="prettyprint">...</pre> tag.
<script type="text/javascript">
var checkJsTimer;
function checkJs() {
if (typeof prettyPrint == 'function') {
window.clearInterval(checkJsTimer);
prettyPrint();
}
}
function downloadJSAtOnload() {
var element = document.createElement("script");
element.type = 'text/javascript';
element.src = "http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js";
document.body.appendChild(element);
checkJsTimer = window.setInterval(checkJs, 100);
element = document.createElement("link");
element.rel = 'stylesheet';
element.type = 'text/css';
element.href = "http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css";
document.getElementsByTagName('head')[0].appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>

Heiner,
ReplyDeletegreat post. Not having to touch the html layout and not having to upload the .js and .css files is a great plus.
Good work !
Hi,
ReplyDeleteI used what is said in your post to enable prettyprint.
Thank's a lot, it work's for me.
But I have to be on a static layout.
I I choose a dynamic layout (the one I prefere) it doesn't work.
Do you any idea?
Thank's
Nice post..
ReplyDeleteAlso found a nice eclipse plugin to directly copy code as HTML from eclipse IDE.
http://java-sample-program.blogspot.com/2012/12/copy-as-html-eclipse-plugin.html
This is not a easy task but you guide such a way its look easy and i can try to format source code by implement the new technology news PrettyPrint. Thank you for guidance.
ReplyDeleteThanks for the share.. Still works like a charm.
ReplyDeleteI was using the link script and body onload function() here at irsah blog, but somehow interfered with my blogger responsive theme (possible jquery/js conflicts?).
Using your script either thru widget or direct embbed to theme works. Thanks again.
Very useful. Thanks
ReplyDeleteI pasted the above code inside pre tag on a HTML / JavaScript gadget. It is showing PrettyPrint at the point where i added it. But what i am not getting is how can i use this to format my code snippet inside my blog post. please help
ReplyDelete