I'm using blogger.com to host some texts on programming, and I'd like to use prettify (same as stackoverflow) to nicely colour the code samples.
How do I install the prettify scripts into the blog domain?
Would it be better (if indeed its possible) to link to a shared copy somewhere?
I have webspace on a different domain. Would that help?
Many thanks.
It's very simple to add google code prettifier in your blogger.
just include the below javascript library in your blogger just before tag.
just like in the below picture...
Now you successfully added google code prettifier in your blogger.
Now if you want to insert code in your blogger post then add code (html, css, php and etc.), then insert that code between .... tags.
or
Demo of the google Prettify on Blogger
Also please refer this documentation for adding this Google prettifier to blogger in the following link.
how to add Syntax Highlighter For Blogger Using Google Prettify
Here is the solution that works for me. Put in the
<head>...</head>
of dynamic blogger HTML:When you make a new entry in blogger, you get the option to use HTML in your entry and to edit your blog entries.
so type http://blogger.com , then login, then Posting>Edit Posts>Edit then in there put this at the top:
Note that you shouldn't use
prettyPrint
directly as an event handler, it confuses it (see the readme for details). Which is why we're passingaddLoadEvent
a function that then turns around and callsprettyPrint
.In this case because blogger does not allow us to link to the stylesheet, we just embed the prettify.css contents.
then add a
<code></code>
tag or a<pre></pre>
tag with the class name of"prettyprint"
, you can even specify the language like this"prettyprint lang-html"
so it can look like this
or like this
the code that you put in needs to have its HTML cleaned from < and > to do this just paste your code in here: http://www.simplebits.com/cgi-bin/simplecode.pl
you can put the top code in your HTML layout so that its included for all pages by default if you like.
update Now you can link CSS files in blogger, so adding this to the
<head>
should be enoughI chose not to replace the body onload event on purpose, instead I'm using the new DOMContentLoaded event that the old browsers don't support, if you need old browser support, you can use any other load event to initiate prettyPrint, for example jQuery:
or the supposedly smallest domready ever
and your done :)
Edit:
as Lim H pointed out in the comments, in case where you use the blogger dynamic views (ajax templates) then you need to use the method described here to bind custom javascript: prettyPrint() doesn't get called on page load
Update 2017-06-04
Use the guide here https://github.com/google/code-prettify
Basically just use this :)
The following worked for me immediately.
<head>
in the "Edit template" field:snippet:
</head>
replace<body>
with<body onload='prettyPrint()'>
<pre class="prettyprint">int foo=0; NSLog(@"%i", foo); </pre>