57

I'm using blogger.com to host some texts on programming, and I'd like to use Prettify (same as Stack Overflow) to nicely colour the code samples.

How do I install the Prettify scripts into the blog domain? Would it be better (if indeed it's possible) to link to a shared copy somewhere? I have webspace on a different domain. Would that help?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
billpg
  • 3,195
  • 3
  • 30
  • 57
  • Off-topic: You should see this **[Social Content Locker](http://stackoverflow.com/questions/27619171/social-content-locker-for-blogger-com-blogs-is-it-possible)** for blogger, it's just amazing-- – craig lerr Jan 03 '15 at 09:08

11 Answers11

61

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, log in, and navigate to PostingEdit PostsEdit. In there put 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">
/* Pretty printing styles. Used with prettify.js. */

.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 shouldn't use prettyPrint directly as an event handler. It confuses it (see the readme for details). Which is why we're passing addLoadEvent a function that then turns around and calls prettyPrint.

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 "prettyprint lang-html".

So it can look like this:

<pre class="prettyprint lang-html">
<!-- your code here-->
</pre>

Or like this:

<code class="prettyprint lang-html">
<!-- your code here-->
</code>

The code that you put in needs to have its HTML cleaned from < and >. To do this, just paste your code in here: https://www.freeformatter.com/html-escape.html

You can put the top code in your HTML layout, so that it’s included for all pages by default if you like.

As of 2012, you can link CSS files in 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 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:

jQuery(function($){
    prettyPrint();
});

Or the supposedly smallest domready ever

And you're done :)

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 code: prettyPrint() doesn't get called on page load

Use the guide at GitHub: 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>
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
  • 2
    I've not actually tried it, but it just looks so darn comprehensive I just had to accept it. – billpg Dec 16 '09 at 19:22
  • @timo can i insert it into the blog template rather than amending it in every post. – Unni Kris Jun 20 '12 at 05:34
  • @UnniKris yes, you should do that – Timo Huovinen Jun 20 '12 at 08:12
  • @UnniKris you do it for every post only when a few of your posts contain code that needs to be highlighted. – Timo Huovinen Jun 20 '12 at 14:15
  • Hi, I couldn't get prettyPrint() to execute on page load regardless of any domready method I use. Please see the question here: http://stackoverflow.com/questions/14540009/prettyprint-doesnt-get-called-on-page-load – Lim H. Jan 26 '13 at 19:11
  • The URLs in the – Anachronist May 30 '17 at 07:02
  • @Anachronist Thanks for catching that, I will try and find the new urls. – Timo Huovinen Jun 04 '17 at 07:35
  • @TimoHuovinen - the source URL seems to have changed AGAIN: As per the GitHub page, it's now `https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js` – millhouse Jul 19 '17 at 06:40
  • @millhouse I've try add the `` and the `https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js` to my `` but nothing work. I've tried add these 2 script to 1 post also, nothing happen too. [Here is image for my template HTML](https://i.imgur.com/1W5ORW2.png) – Luke Jun 20 '18 at 01:33
  • @millhouse you need to add the css too, see the answers edit – Timo Huovinen Jul 11 '18 at 10:25
  • The www.simplebits.com link is broken: *"404 Page Not Found. The page you requested does not exist."* – Peter Mortensen Aug 14 '21 at 11:15
41

The following worked for me immediately.

  • Go to Blogger > Layout > Edit HTML
  • Copy the following snippet and paste it immediately after <head> in the "Edit template" field:

snippet:

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' rel='stylesheet' type='text/css'/>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'></script>
  • After </head> replace <body> with <body onload='prettyPrint()'>
  • Click "SAVE TEMPLATE"
  • Go to Blogger > Posting > New Post
  • Make sure you're editing the HTML by clicking on "Edit HTML." In the empty field try:

<pre class="prettyprint">int foo=0; NSLog(@"%i", foo); </pre>

  • Notice if you click "Preview" now you'll see this code in black only. Don't worry (yet).
  • Click "PUBLISH POST" and then "VIEW BLOG". Your code should be prettified.
gMale
  • 17,147
  • 17
  • 91
  • 116
Ken
  • 30,811
  • 34
  • 116
  • 155
  • 1
    As of now, this is now the best answer since we can now link to CSS in blogger. – Eric Fortin Jan 29 '12 at 19:41
  • You can make `Preview` work by clicking it, then in the newly opened preview tab, you replace `https` by `http` (without "s") allowing the script to load. – heltonbiker Jun 17 '14 at 02:55
15

Nowadays, Google Code Prettify has an auto-loader script. You can load the JavaScript and CSS for prettify via one URL.

Add the script to the <head> section of your Blogger template and it will work on all your posts:

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

More detail is on Getting Started.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Badaro
  • 576
  • 3
  • 12
  • Yes! This is the preferred way of doing it now. If you need more languages than the default ones, you can add params (`?lang=css&lang=ml`) at the end of the url to attach extra [language handlers](https://code.google.com/p/google-code-prettify/source/browse/trunk/src) – KyleMit Jan 29 '14 at 20:56
6

It's very simple to add the Google Code prettifier in your Blogger account.

Just include the below JavaScript library in your Blogger account just before tag.

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

Just like in the below picture...

Enter image description here

Now you have successfully added the Google Code prettifier to your Blogger account.

Now if you want to insert code in your Blogger post, add code (HTML, CSS, PHP and etc.), and insert that code between .... tags.

 <pre class="prettyprint">...</pre>

or

<code class="prettyprint">...</code>

Demo of the Google Prettify on Blogger

Also please refer to this documentation for adding this Google prettifier to Blogger in the following link.

Syntax Highlighter For Blogger Using Google Prettify

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
muni
  • 1,362
  • 2
  • 19
  • 21
3

Have a look at SyntaxHightlighter.

On that site you can also find instructions on how to use it at blogger.com and the site offers a hosted version of the required scripts, so you don't need to host files somewhere yourself.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeroen-bart Engelen
  • 1,157
  • 2
  • 12
  • 19
2

Another solution is to use the syntaxhighlighter 2.0 JavaScript library. I've used it on my blog and it seems to work quite well.

Here's a blog post about it:

Syntax Highlighting with Blogger Engine

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CraftyFella
  • 7,520
  • 7
  • 47
  • 61
  • The first link ***seems*** to be (effectively) broken. It redirects to *[Alex Gorbatchev And gogopdf Turn Raw Javascript To A PDF Service](https://gogopdf.com/blog/Alex-Gorbatchev-And-gogopdf-Turn-Raw-Javascript-To-A-PDF-Service)* (on a different domain). It is not clear at all if that is the original thing (it mentions syntax highlighting, but it is not clear if that is the original intent). – Peter Mortensen Aug 14 '21 at 11:08
1

It is not a direct answer to your question, but it is worth considering GitHub. You can get a free account and get syntax colored "gists" which you can share and host on your web page.

The downside is that the copy is hosted on GitHub's site and if that's down, then it's down for you too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
1

cdnjs is providing the library "SyntaxHighlighter".

Go to BloggerTemplateEdit template. Add the below code just before the ending of the body tag and save the template.

I have given a example for Python.

You can link the other language script files from cdnjs.

Syntax highlight code

<pre class="brush: py">
    print("Hello, World!")
</pre>

For other languages, go and copy the script: https://cdnjs.com/libraries/SyntaxHighlighter

Enter image description here

<!-- Syntax highlighter-->
<link href='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeDefault.css' rel='stylesheet'/>
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.min.js'/>
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shAutoloader.min.js'/>

<!-- For Python -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushPython.min.js'/>

<!-- Include other languages, like JavaScript and PHP -->
<script language='javascript'>
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
</script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
0

Here is the solution that works for me. Add in the <head>...</head> of the dynamic Blogger HTML:

<script>
    $(window.blogger.ui()).on('viewitem', function (event, post, element) {
        prettyPrint();
    });
</script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Artur A
  • 7,115
  • 57
  • 60
0

Go to the Blogger theme section and click on edit HTML.. Then add a reference to the Google Prettify CDN to the head tag of the HTML.

Then include a theme for code snippet below this script...I included the Desert theme.
<!--Desert theme-->
<style type='text/css'>pre .atn,pre .kwd,pre .tag{font-weight:700}pre.prettyprint{display:block;background-color:#333}pre .nocode{background-color:none;color:#000}pre .str{color:#ffa0a0}pre .kwd{color:khaki}pre .com{color:#87ceeb}pre .typ{color:#98fb98}pre .lit{color:#cd5c5c}pre .pln,pre .pun{color:#fff}pre .tag{color:khaki}pre .atn{color:#bdb76b}pre .atv{color:#ffa0a0}pre .dec{color:#98fb98}ol.linenums{margin-top:0;margin-bottom:0;color:#AEAEAE}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}@media print{pre.prettyprint{background-color:none}code .str,pre .str{color:#060}code .kwd,pre .kwd{color:#006;font-weight:700}code .com,pre .com{color:#600;font-style:italic}code .typ,pre .typ{color:#404;font-weight:700}code .lit,pre .lit{color:#044}code .pun,pre .pun{color:#440}code .pln,pre .pln{color:#000}code .tag,pre .tag{color:#006;font-weight:700}code .atn,pre .atn{color:#404}code .atv,pre .atv{color:#060}}</style>
For more themes, visit here.. [Prettify themes][1] When you create a post, change the edit mode from visual to **HTML** and go to the place where you are going to add the code snippet. Then include the code like this.
<pre class="prettyprint">
  <code class="language-html">
      <!-- your code snippet -->
  </code>
</pre>
You can change the code style by selecting relevant languages HTML, CSS, PHP, JavaScript, etc. Here I used an **HTML** code snippet. [1]: http://www.compromath.com/2017/02/adding-specific-code-syntax-highlighter.html
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

My way is simple and has mobility.

Go to your Blogger account click ThemeCustomizeAdvancedAdd CSS.

Then paste the below CSS code.

code {
    font-family: Courier, monospace;
    color: #000;
    background-color: #f8f9fa;
    border: 1px solid #eaecf0;
    border-radius: 2px;
    padding: 1px 4px;
}
pre, .mw-code {
    padding: 5px;
    font-family: Courier, monospace;
    font-size: inherit;
    line-height: 1rem;
    color: #000;
    background-color: #f8f9fa;
    border: 1px solid #eaecf0;
    padding: 1em;
    white-space: pre-wrap;
    overflow-x: hidden;
    word-wrap: break-word;
}

To make things work, for example:

To check the MX record of a domain <code> nslookup </code> (in Windows):<br />

<pre>temp = foo
foo = bar
bar = temp
</pre>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Junayed
  • 91
  • 6