Showing posts with label Blogging. Show all posts
Showing posts with label Blogging. Show all posts

Wednesday, June 17, 2020

Syntax Highlighting Code in Blogger

I was using Syntax Highlighter created by Alex Gorbatchev to highlight code snippets on this blog, but of late, it was not working or taking too long to load up. 
So it was time again to figure out a new way to do this and there were a few options that work in a similar fashion:
The other option was to pre-format code and then paste it into your post:

I found Code Prettifier to be the simplest option that can get me closest to Syntax Highlighter and the setup is very easy - just need to add these lines to the theme HTML code:

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<style>
li.L0, li.L1, li.L2, li.L3,
li.L5, li.L6, li.L7, li.L8 {
  list-style-type: decimal !important;
}
</style>

Once this is done, all you need to do is to put encoded code snippet in a pre block with the class set to prettyprint and viola!
Note that the above code has a style override which makes line numbers show up on all lines of code. Without that block of style code, only every fifth line of could would have a line number.

Here is an example of the pre block that you need to use to syntax highlight your code:
 <pre class="prettyprint linenums lang-sh"> ... your code ... </pre>

You don't need to specify the language since prettyPrint will guess it, however you can specify a language by specifying the language extension along with the prettyprint class. Languages supported out of the box are:
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
"js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml","xsl"
You may also use the HTML 5 convention of embedding a <code> element inside the <pre> and using language-java style classes:
  <pre class="prettyprint"><code class="language-java">...</code></pre>
Note: Remember to escape your code using HTML entities :)

Wednesday, December 26, 2012

Blogger keeps getting better

Blogger is getting better and more integrated with Google & Plus with the following new & useful features:

Good to see so many good features being added to blogger towards the end of 2012.
Hope 2013 makes it even better.

Sunday, September 11, 2011

Better Blogger Interface

Blogger is working on a vastly improved interface and I got a preview of it recently.
The new interface is really nice and am hoping it will become the standard interface soon.

Wednesday, September 22, 2010

How to use Syntax Highlighter 3 in Blogger

Syntax Highlighter version 3.0.83 has been released and there are quite a few changes in the way things work now. I realized this when the rendering on this site broke after I modified it to use the latest release. So, here is the steps required to setup the "hosted" version of the latest Syntax Highlighter (version 3.0.83) and integrate with your Blogger/Blogspot blog.

Installation

  1. Navigate to Dashboard > Design > Edit HTML
  2. Backup the current template by clicking on the link Download Full Template
  3. In the textarea, press CTRL+F to find the code </head>
  4. Copy the below code and paste it just above
    <!-- Syntax Highlighter Additions START -->
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
    <link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
    
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
    <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
    
    <script language='javascript' type='text/javascript'>
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
    </script>
    <!-- Syntax Highlighter Additions END -->
    
  5. Preview your changes and make modifications as required
  6. Save the template

In the above code, I have put in brushes for the languages that I use frequently. Modify the brushes according to your needs - check the full list of supported brushes here and use whichever you need.

Configuration

Apart from choosing the brushes that you need, there are other configuration parameters that you can setup as well. Check out the details here. More useful ones are the ones that allow you to change the starting line number, highlight specific lines, disable auto-linking of URLs and html-script option to highlight mixture of HTML/XML code.

Apart from these configuration changes, you can change the theme as well. SyntaxHighlighter comes bundled with 7 themes and I personally like the default one. However, you can choose the theme that you fancy the most by simply replacing the name of the theme in this line:
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>

So you can replace shThemeDefault in the above line with any one of the following themes:
  1. Django - shThemeDjango
  2. Eclipse - shThemeEclipse
  3. Emacs - shThemeEmacs
  4. Fade to Grey - shThemeFadeToGrey
  5. Midnight - shThemeMidnight
  6. RDark - shThemeRDark

Usage

To use it in your blog post, there are a couple of things that need to be done.
  1. Escape all code by replacing any occurrences of:
    1. < with &lt;
    2. > with &gt;
  2. Enclose the escaped code between <pre class="brush:[brush_name];[optional_params]"> and </pre>
  3. Setup the proper brush name (based on the code being highlighted) in the opening pre tag. Using the correct brush will ensure proper highlighting of the code

New Features in 3.0.83

The new version brings with it quite a few new features - here's an overview:
  • Auto-Loading of Brushes

    Now all the brushes need not be pre-loaded on every page. You can load only the required brushes on the pages that need those brushes. Use the shAutoloader.js and then load the brushes as required
    For more details on using this feature, check out the instructions here. Note that this might not be so useful for a blogger account as we do not modify the javascript for each page/post.
  • No usage of Flash

    Earlier, Flash was being used to copy the code to clipboard. This has now been removed and a simpler & better way to do this has been implemented. Also, the user can now simply double-click anywhere in the code to select it and then use the standard CTRL+C to copy it.
  • Code copy without line numbers

    Now the copied code excludes line numbers and the extra leading tabs/spaces to make it usable directly - very convenient!
  • Add Title

    Now you can add a title to the code block by title="[title_name]"
parameter to the pre tag.

  • More under the hood

    More improvements and changes under the hood like better CSS support, easier integration with CommonJS & node.js, etc.
  • Wednesday, June 16, 2010

    Make your Blog Post Title SEO Friendly

    By default, the Blog Post Title that Blogger creates has the Blog Name followed by the Post title. This is not so SEO Friendly and it determines how you your blog title is displayed in search results. Making it more relevant increases the chances of people clicking through your link and besides, doing this is really very simple!

    Change Blog Post Title
    1. Go to Design > Edit HTML in your Dashboard
    2. Search for:
      <title><data:blog.pageTitle/></title>
    3. Replace it with:
      <b:if cond='data:blog.pageType == "index"'> 
        <title><data:blog.title/></title>
      <b:else/> 
        <title><data:blog.pageName/> | <data:blog.title/></title> 
      </b:if>
      
    4. Save the Template

    Note: The changes on your Blog will be immediate, but the results to show up on the search engines may take up time depending on how they spider and change their databases.

    Summer is here (almost)!

    Good to see that warmth is returning to the air and its summer time again!
    And along with the arrival of summer, it is good see new, fresh and very good templates/themes being made available from blogger!
    My previous blog template was good but was breaking and it was time to change it - what a timing from blogger in releasing the new templates!
    So to reflect the joy in the air, I have changed the template on my blog to be more cheerful!
    If you have a blogger/blogspot blog, do check out the new templates!

    With the new template, the Tag Cloud widget stopped working so I had to re-install it. If you face a similar problem, then check out the creator phydeaux3 blog page on this here. Only thing that I did not like about this widget is that it shows the current post also in the list of related posts. I feel filtering it should not be such a big task - I hope the creator can take note of this and implement it at the earliest.

    The code was not showing up properly after installing new template - this indicated that the Syntax Highlighter was not working. To re-install it, I referred to the detailed post here.

    Apparently, adding the new template broke the Related Posts widget as well, so I had to re-install that as well. Check out the instructions at the the creators' site here.

    Note: Please backup your current template locally before doing any such changes. Also you may need to re-install 3rd party widgets as they may not be cleanly supported by these new templates!

    Wednesday, April 28, 2010

    Adding Google Custom Search to your Blog

    Adding Custom Search to your Blogspot Blog enhances its value and allows visitors to sift through your posts with ease. Adding Google Custom Search is not simple or for the fain at heart. It requires tons of patience, a keen eye and a good comfort level at playing with code. For a live demo, I have added the same to my blog as well, so feel free to try it out! Adding Google Custom Search is not simple, here are the steps that are required:
    1. Create a static page on Blogger for displaying Search Results
    2. Sign Up for Google AdSense - if you want to earn money
    3. Sign Up for Google Custom Search
    4. Update the static page with Google Custom Search Code
    5. Remove side bar & meta-data displayed on the static page
    6. Add search box on side bar
    So here goes, get ready to get down & dirty with the details ...

    1. Create a static page on Blogger

    Create a static page on Blogger for displaying Search Results. Go to Posting > Edit Pages and click on the New Page button to create a new static page. Set the title as Search Results or anything appropriate and for now, enter placeholder text Search Results in the page. View this page and note down the URL of this page - this will be used in the later steps when configuring Google Custom Search.

    2. Sign Up for Google AdSense

    If you want to earn some money through the searches that are done on your site, you will need to sign-up for an adsense account. Here I am assuming that you already have one - if not, then just go ahead and sign up for one.

    3. Sign Up for Google Custom Search

    From inside the AdSense account, click on the link AdSense for Search and then follow the steps below:

    • Click on Single Page link at the top to view all the options in one page
    • Search Type: Select Only sites I select
    • Selected sites: If you have more than one site, enter one site per line for each of the websites you want to include in the search.
    • Optional Keywords: Enter keywords separated by space if you want to give hints to the search engine.
    • Opening of search results page: Select Open results within my own site and in the box below for Enter URL where search results will be displayed, enter the URL of the static page created in Step 1 above.
    • Ad Location: You can choose whichever option you like, but I personally prefer to go for Top and Bottom
    • Name Your Search Engine: give a name for your custom search engine
    • Fill in all the other remaining fields as per your requirement
    • Press the Submit and Get Code button to get the Javascript code that you need to put into the static page

    4. Update static page on Blogger

    Go to Posting > Edit Pages and click on the Edit link for the static page created above. Then:

    1. Copy code from Search box code and paste it here
    2. Enter a break tag <br>
    3. Copy code from Search results code and paste it here

    5. Remove side bar & meta-data displayed on the static page

    Now this is the tricky part which requires you to change the template. You can skip this step if you are OK with having the widgets display on the right column. Note: take a backup of your template before proceeding!
    Go to Layout > Edit HTML and check the box Expand Widget Templates. Then add the line: to the following 3 places:
    To remove the Post Metadata (Author, Tags, Time) from Static page
    Lines 1 and 19 in this code snippet

           <b:if cond="data:blog.pageType != "static_page"">
       <div class="postmeta">
       <b:if cond="data:top.showAuthor">
          Posted by
          <span class="fn"><data:post.author/></span>
          </b:if> |
       <b:if cond="data:post.labels">
       Posted in
            <b:loop values="data:post.labels" var="label">
              <a expr:href="data:label.url" rel="tag"><data:label.name/></a><b:if cond="data:label.isLast != "true"">,</b:if>
            </b:loop>
          </b:if> |
       <b:if cond="data:top.showTimestamp">
          <b:if cond="data:post.url">
          Posted on <data:post.timestamp/> 
          </b:if>
          </b:if>
       </div>
          </b:if>
     </div>
    

    To remove the "Continue Reading" or similar link from Static page
    Lines 2 and 7 in this code snippet
     <b:if cond="data:blog.pageType != "item"">
            <b:if cond="data:blog.pageType != "static_page""><br/>
     <div class="postmetadata">
        <p><a expr:href="data:post.url">Continue Reading</a></p>
     </div>
        </b:if>
        </b:if>
    

    To remove the right Column widgets from Static page
    Lines 2 and 14 in this code snippet
     <!-- Body_right -->
    <b:if cond="data:blog.pageType != "static_page"">
        <div id="body_right">
       <!-- Sidebar Ads -->
    ...
    ...
       <!-- /Sidebar Ads -->
       
       <!-- Sidebar -->
       <div id="sidebars">
    ...
    ...
          </div>
    </b:if>
       <!-- /Body_right -->
    

    6. Add search box on side bar

    To add search box on side bar, go to Layout > Page Elements and click on the Add a Gadget link on the bottom right. Add a HTML/JavaScript widget and in the window that opens up, enter the following:

    • Title: Search or something similar
    • Content: Here paste the code from Google Custom Search / AdSense for Content from the box Search box code
    And after these long & complicated steps, you should have your Google Custom Search with AdSense on your Blogger Blog ready!!!

    Thursday, April 15, 2010

    Syntax Highlighter now available for Blogger

    I had been putting off using Syntax Highlighter for quite some time as it required me to host the files somewhere and then use them. Also, using it for a Blogger Blog did not seem to be so straight-forward and did not produce clean results. This was the status when I had last checked it out quite some time back.

    So when reviewed a post I wrote with a lot of code in it, I was saddened that I had not add Syntax Highlighter to my blog. So I decided to take a second look and was pleasantly surprised to see both my issues resolved! Now there was an option for using the hosted version of the files and also there was a very neat blogger mode!

    So now finally, you don't need to look around for someone to host the Syntax Highlighter files for you - you can use the hosted version of the Syntax Highlighter files. And there is a very good bloggerMode! which helps to integrates very cleanly with Blogger.

    Monday, March 22, 2010

    Install WordPress on IIS7

    A friend of mine wanted me help him install WordPress on his website - no big deal as WP is famous for it's 5 minute installs and so I figured this should be a cake walk for me. Little did I know that my friend's web-server was IIS7 on Windows Server 2008 and this would translate to a 5 hour install :(

    Google, pointed me to a very good site that helped me out - trainsignaltraining.com.

    Here are the components that you need to download:

    Here are the Installation steps:
    1. Install & configure PHP
    2. Install & configure MySQL
    3. Install & configure PHPMyAdmin
    4. Install & configure URL Rewrite Extension
    5. Install & configure WordPress
    6. Configure Search Engine Friendly URLs on WordPress

    However, here are a few "gotchas" that need to be taken care of:
    • For PHPMyAdmin to work, you may need to change in config.inc.php
      $cfg[$i]['host'] = ‘localhost’; to
      $cfg[$i]['host'] = ‘127.0.01’; or
      $cfg['Servers'][$i]['host'] = ‘127.0.0.1′
    • For PHPMyAdmin to work, you may need to change in config.inc.php
      controluser = ‘pma’ to
      controluser = ‘root’
    • For PHPMyAdmin to work, you may need to change windows\system32\drivers\etc\hosts file:
      Remove the comment (#) in front of the line 127.0.0.1
      Comment out the line ::1 localhost
    • Read the comments in all the pages as they have solutions to a lot of common problems

    Note: I am assuming that IIS7 is already installed. If not, then there's a good tutorial for that as well here!

    Friday, January 22, 2010

    Good to be back!

    Feels really good to be back fresh & fully charged up after a 3 week vacation covering 4 cities in India and a short stopover in Dubai (courtesy Emirates Airlines)!

    Keeping my New Year Resolution, I have started blogging in earnest! Lets' see how far I can keep up with my resolution ;)

    Wednesday, December 9, 2009

    Adding Poll to a Blog Post

    Was discussing about the most frequently used / favorite *nix and I thought of adding a Poll on this in a Blog post. Here's how to do this ...


    I played around with the blogger menu's & settings, but there was no way I could add a poll to a blog post.

    So I went the round about way of creating a Poll on the side menu and then putting that code into my post. Voila ! and it worked like a charm! Here are the steps in detail:

    1. In Blogger/Blogspot, Go to the Layout Tab
    2. Click on the "Add a Gadget" link in the right panel
    3. Click on the "Poll" gadget from the "Basic" list - this pops open a new window
    4. Enter the Poll Question and list of answers you expect people to choose from
    5. There are four options - you can remove as many as you don't want or add more too
    6. Select whether users can choose multiple answers - based on how you want your poll to be
    7. Select the end date & time for your poll
    8. Save the poll
    9. Click on the "View Blog" link at the top and then view the source
    10. Search for the question in your poll in the source - you will see an <iframe> tag near it
    11. Copy the full tag from <iframe> to </iframe>
    12. Now create the new post to which you want to add this poll to
    13. Go to the "Edit HTML" tab if not already in it
    14. Paste the copied tag inside the post in the location where you want this poll to appear
    15. Save & Publish this post
    16. Go back to Layout and remove this gadget

    After this, your post & the poll inside it should still work perfectly!

    Here are a few things that you can do with the iframe properties:
    • Adjust the value of the "height" property
    • Adjust the value of the "width" property of the "style" property
    • Set the value of the "scroll" property to "no" to remove scrollbar
    • Add "overflow:hidden" to the "style" property to remove the scrollbar

    Tuesday, November 24, 2009

    Technorati Blog Claiming process

    49W8WE68F9S4
    This is the code sent to me by Technorati to verify my ownership of this blog!

    Monday, November 23, 2009

    Half Century: Finally Crossed the 50 Posts Mark!!!

    On a side note, it's good to just realize that I finally completed my Half Century! Took me about 7 months to reach & cross the 50 posts mark! Hope to accelerate the speed of posting next year - that's going to be my New Year Resolution for 2010 ;)

    Tuesday, October 13, 2009

    New Widgets added - Tag Cloud & Related Posts

    I found out and added two new widgets to this blog: Tag Cloud & Related Posts - thanks to the creators for providing these.


    Here are the details of the widgets that I have added.

    Tag Cloud:
    I searched for and found quite a few free Blogger Tag Cloud widgets but liked this one provided by phydeaux3. It is very simple, flexible, highly configurable and suits my needs perfectly!

    Related Posts:
    I was looking around for cool widgets for this blog and found one which shows related posts below each post. This was awesome and quite simple to implement. You can check it out here. Check out the blog for a lot of good widgets, but some like the Search does not have proper install instructions.

    Friday, March 6, 2009

    Source Code Encoding tool for posting to a Blog

    I faced a strange dilemma while trying to publish a piece of code to my blog post. The JavaScript code that I was trying to post was getting truncated and not showing up properly due to the embedded DHTML code in it. I found out a good tool to encode the markup to make it suitable for publishing in a Blog post:

    SimpleBits Converter

    Instructions from the website:
    Enter normal (X)HTML in the markup box below.
    Press "Process" and it will spit out entity-encoded markup suitable for <code> examples.
    Use spaces in increments of two for nesting indents.


    Now looking for a converter / tool that can beautifully format, color & print out Java Code for publishing in a post !

    Finally started blogging...

    After what seems like ages of procrastination, I finally got around to creating a blog!
    This will be a journal of my rants, views, reviews, comments, experiences, experiments with Technology, cooking and life in general.
    I coined "Common IT Man" as a class that extends RK Laxman's "Common Man" class and adds features specific to IT professionals of today's world!

    LinkWithin

    Related Posts Plugin for WordPress, Blogger...