Add Syntax Highlighter to Blogger There are quite few good Syntax Highlighters out there, one of them is “Prettify” which is simple and lightweight. Prettify states that it can automatically guess and highlight snippets for many languages (best on C-like and HTML-like) and also has many language extensions, if you need one. So I setup a simple demo to test it and the result was quite good.
DemoSimple setupAdding Prettify Syntax Highlighter to any CMS or webpage is very sy. For Blogger you just need to go to your blog’s Template tab, click “Edit HTML” button and add the below script before </body> tag.
<script src="https://google--prettify.google.com/svn/loader/run_prettify.js"></script>As you can see that the project is hosted on Google . This is an auto-loader which will automatically load the required JavaScript API and the CSS file from the subfolders of SVN. So the last thing that you are left is to mention where it should highlight the by adding class="prettyprint" to pre and/or tags within Blogger Post Editor, HTML view. Here is an example:
<pre class="prettyprint">
$(function() {
alert('I am using Prettify as Syntax Highlighter');
});
</pre>There are also some styles available that you can call and load the stylesheet in one URL, here is an example:
<script src="https://google--prettify.google.com/svn/loader/run_prettify.js?skin=desert"></script>Own hostingWell, the above tutorial was the simple yet effective setup but what if you want to host Prettify on your own server? Blogger don’t allow us to host files directly, so if you have got a hosting service then it’s good, otherwise you can use Google to upload the syntax highlighter files.
Download the file form this page, extract it using archive software like WinRAR or 7-Zip and upload “prettify.css” and “prettify.js” files by crting a project. Now same as above add the CSS file before </hd> and the script before </body> tag respectively. To execute it you need to add prettyPrint(); before closing body tag as shown below:
<html>
<hd>
{All hd contents}
<b:skin><![ATA[
{Inline CSS}
]]></b:skin>
<link href="https://example.google.com/.../svn/prettify.css" type="text/css" rel="stylesheet" />
{Other JavaScript}
</hd>
<body>
{Hder, Footer, DIVs}
<script src="https://example.google.com/.../svn/prettify.js"/>
<script>prettyPrint();</script>
</body>
</html>It is not mandatory to add the external CSS file but instd you can add inline CSS before ]]><b:skin> to highlight the syntaxes with some style.
by Cool Text: Free Logos and Buttons - Crte An Just Like This
No comments:
Post a Comment