When you create a new blog post, you get the opportunity to use HTML in your post and edit blog posts.
so enter http://blogger.com , then log in, then by navigating> Edit Messages> Edit then paste this at the top:
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script> <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/lang-css.min.js"></script> <script type="text/javascript"> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(function() { prettyPrint(); }); </script> <style type="text/css"> .str { color: #080; } .kwd { color: #008; } .com { color: #800; } .typ { color: #606; } .lit { color: #066; } .pun { color: #660; } .pln { color: #000; } .tag { color: #008; } .atn { color: #606; } .atv { color: #080; } .dec { color: #606; } pre.prettyprint { padding: 2px; border: 1px solid #888; } @media print { .str { color: #060; } .kwd { color: #006; font-weight: bold; } .com { color: #600; font-style: italic; } .typ { color: #404; font-weight: bold; } .lit { color: #044; } .pun { color: #440; } .pln { color: #000; } .tag { color: #006; font-weight: bold; } .atn { color: #404; } .atv { color: #060; } } </style>
Note that you should not use prettyPrint
directly as an event handler, it confuses it (see readme for more details). This is why we pass addLoadEvent
function, which then rotates and calls prettyPrint
.
In this case, since the blogger does not allow us to refer to the stylesheet, we simply insert the contents of prettify.css.
then add the <code></code>
tag or the <pre></pre>
with the class name "prettyprint"
, you can even specify a language like this "prettyprint lang-html"
so that it looks like this:
<pre class="prettyprint lang-html"> </pre>
or how is it
<code class="prettyprint lang-html"> </code>
the code you paste should have your HTML cleared of <and> for this just paste the code here: http://www.simplebits.com/cgi-bin/simplecode.pl
you can put the top code in your HTML layout so that it is included for all pages by default, if you want.
Update Now you can link the CSS files in the blogger, so adding this to the <head>
should be enough
<link href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script> <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/lang-css.min.js"></script> <script type="text/javascript"> document.addEventListener('DOMContentLoaded',function() { prettyPrint(); }); </script>
I decided not to specifically replace the onload event for the body, instead I use the new DOMContentLoaded event, which older browsers do not support, if you need support for the old browser, you can use any other loading event to trigger prettyPrint, for example jQuery:
jQuery(function($){ prettyPrint(); });
or supposedly the smallest domready ever
and done :)
Edit:
as Lim H pointed out in the comments, if you are using dynamic blogger views (ajax templates), then you need to use the method described here to bind custom javascript: prettyPrint () does not receive a call when the page loads
Update 2017-06-04
Use the guide here https://github.com/google/code-prettify
Basically just use this :)
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script> <pre class="prettyprint"><code class="language-css">...</code></pre>