Showing posts with label Plugins Blogger. Show all posts
Showing posts with label Plugins Blogger. Show all posts

Wednesday, October 14, 2009

Related post plugin for blogger


Since I started using blogger, I've always wondered why this feature is not present by default. To me having a list of related post is very important, because that's a nice way to put forward more relevant content to you dear reader ;)

So i've search on internet, and found a flurry of so called plugin or widget. The most common one seems to have been written by Hoctro. But none of those never really satisfied me.

As a result I decided to write my own using a bit of Ajax query to make everything smooth and nice. The result is a plugin that display on articles pages (not on the front page for now) a list of post related to the current one. It is relying on the post labels and the google API to display thoses.

And here it is:

<b:if cond='data:blog.pageType == &quot;item&quot;'>

<div class='similiar'>

 <div class='widget-content'>
  <div id='relatedPosts' /> <br/>
  <div expr:id='&quot;relatedPosts_&quot; + data:post.id'/> <br/>

  <script type='text/javascript'>

  var blogURL = &quot;<data:blog.homepageUrl/>&quot;;
  var maxNumberOfLabels = 10;
  var nPost = 0;
  var maxPosts = 5;
  var numLabel = 0;
  var receivedResult = 0;

  var outputDone = false;

  var txt = document.createTextNode(&quot;You may also want to read:&quot;); 
  var ul = document.createElement(&#39;ul&#39;);
  var labelRelatedPost = document.createElement(&#39;h3&#39;);
  labelRelatedPost.appendChild(txt);
  var postsFound = [];

  function listEntries(result, postID) 
  { 

   var json = JSON.parse(result);

   receivedResult = receivedResult+1;

   // calculate the number of remaining post to add to the list
   var RemainingPosts = (maxPosts - nPost);
   var nPostToParse = (json.feed.entry.length &lt;= RemainingPosts) ? json.feed.entry.length : RemainingPosts;

   for (var i = 0; i &lt; RemainingPosts; i++) {
    var entry = json.feed.entry[i];
    if(entry)
    { 
     var postURL;
     var title = entry.title.$t

     // parse the link array to find the post URL
     for (var k = 0; k &lt; entry.link.length; k++) {
      if (entry.link[k].rel == &#39;alternate&#39;) {
       postURL = entry.link[k].href;
       break;
      }
     }

     // check the post is not the same as the current post
     if (postURL != location.href) 
     {
      if (postAlreadyKnown(postURL) == false)
      {
       nPost++;

       //alert("Other post found "+nPost);
       var li = document.createElement(&#39;li&#39;);
       var a = document.createElement(&#39;a&#39;);
       var txt = document.createTextNode(entry.title.$t);
       a.href = postURL; 
       a.appendChild(txt);
       li.appendChild(a);
       ul.appendChild(li);   
      }


     }
    }

   }

   // check if everything's been received
   if (receivedResult == numLabel ) {
    if (outputDone == false &amp;&amp; nPost &gt; 0) 
    {
     outputDone = true;
     var relatedPostSection =  'relatedPosts_'+postID;
     var relatedDiv = document.getElementById('relatedPosts_'+postID);
     if (relatedDiv) {
      relatedDiv.appendChild(labelRelatedPost);
      relatedDiv.appendChild(ul);
      
      //
      var providedBy = document.createElement(&#39;h5&#39;);
      var em = document.createElement(&#39;em&#39;);
      var a = document.createElement(&#39;a&#39;);
      a.href = "http://xiuxixiuxi.blogspot.com/2009/10/related-post-plugin-for-blogger.html"
      var txt = document.createTextNode("Provided by XiuxiXiuxi");
      a.appendChild(txt);
      em.appendChild(a);
      providedBy.appendChild(em);
      relatedDiv.appendChild(providedBy);
     }
    }
   }

  }

  function postAlreadyKnown(postURL) {
   for(var i= 0; i &lt; postsFound.length;i++)
   {
    if(postsFound[i] == postURL) 
    {
     return true;
    }
   }
   postsFound[postsFound.length] = postURL;
   return false;
  }

  function searchQuery(query, label) {

   var script = document.createElement(&#39;script&#39;);


   var xhr = new XMLHttpRequest();
   xhr.onreadystatechange = function() {

    if (xhr.readyState == 4) {
     // Your callback code goes here
     listEntries(xhr.responseText, &quot;<data:post.id/>&quot;);
    }
   }
   xhr.open(&#39;GET&#39;, query+ &#39;feeds/posts/default/-/&#39; + label + &#39;?alt=json&#39; , true);
   xhr.send(null);
  }



  <b:if cond='data:post.labels'>
  <data:postLabelsLabel/>
  <b:loop values='data:post.labels' var='label'>
  var textLabel = &quot;<data:label.name/>&quot;
  //alert(textLabel);
  if (numLabel &lt; maxNumberOfLabels) {
   searchQuery(blogURL, textLabel);
   numLabel++;
  }
  </b:loop>
  </b:if>
  </script>
 </div>
</div>
</b:if>

Now where to put that code. You need to edit your blog layout.To do that, go to your blogger dashboard and click on "Layout", click on "Edit HTML", and click on "Expand Widget Templates". Now search for:

<data:post.body> 

and paste the code above just after that.

And your done.


Sunday, October 11, 2009

Code Syntax Highlingting plugin


If you post any source code on your blog (site) and want to have some syntax highlighting like this on your website then this posts is for you:

alert("Hello World");
 // Example of code


I will concentrate here on how to set it up for blogger but it's basically vadid for any website. More details on that at the end of the post. But let's talk about the plugin a bit.


Supported Languages

This plugin at the time I'm writing this article supports the following languages:
  • ActionScript3
  • Bash/shell
  • C#
  • C++
  • CSS
  • Delphi
  • Diff
  • Groovy
  • JavaScript
  • Java
  • JavaFX
  • Perl
  • PHP
  • Plain
  • PowerShell
  • Python
  • Ruby
  • Scala
  • SQL
  • Visual Basic
  • XML (as well as xslt, html, xhtml)
Not bad heh ;) And there are much much more which haven't made they way to the official package.

Installing the plugin

In order to set this up you will have to edit your template layout. To do that, go to your blogger dashboard and click on "Layout" and then click on "Edit HTML", and search for:

]]></b:skin>

and paste the following code.

<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/shBrushJScript.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/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<!-- add brushes here -->
<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>


Using the plugin

Once you've completed previous step, all you have to do is to start writing a post, and when you have to put some code, switch to the "Edit HTML" tab and use pre tags as follow:
<pre class="brush: js">
 <!-- Your code goes here -->
</pre>

You need to change the language code (in this case js) to match the language you want to highlight.

Make sure to replace all instances of < with &lt;.

Don't worry if your text looks funny in the "Compose" tab, it's going to be fine when you few the post normally.

Where to know more about it: 

This little baby is named SyntaxHighlighter ans it is the fruit of several years of Alex Gorbatchev efforts. You can find more on his website. You will find there the full list of scripts you can include to get syntax highlighting for different languages. There's also different theme css.


I hope this will be useful to you as well.

Sunday, June 1, 2008

How to get your site/blog referenced by Google (and other search engine)

I've seen a log of people asking themselves, why their blog/site is not yet referenced by a search engine. So here's a little little of the things you can do.

Basically what you need to do is to tell the search engine that you exist, because Internet is HUGE, and there's site popping everywhere every seconds, and they have no way to actually know that you exist (and want to be found).

First if you're using a blog, and specifically your Blogger blog, there's a few settings you need to tweak.

  • Go to your Dashboard and then Settings &gt; Basic.
  • Make sure your setting look like mine, especially for "Add your blog to our listings?" as well as "Let search engines find your blog?"

Blogger Settings

Now, the following instruction are true for ANY kind of website, not just a blog.

Getting referenced by Google :

  • Go here and fill the form.
  • It's done... nothing more to do.

Now that we've done Google, which we can say is probably the most used search engine theses days, it doesn't hurt to get also referenced by other search engine.

Getting referenced by Yahoo

  • First thing you can do is check if they already know about your site : Yahoo SiteExplorer
  • Go here, and fill the form just like for Google.
  • Tada... it's done

Getting Referenced by LiveSearch

Now with those three, you've got the most commonly used search engine. Well this is partially true. I should say you have covered the most commonly used search engine in the Western world. If you want to toss some more change to have you site visited by Chinese citizen, use the following :

While you're at it you can get your site referenced by Indian search engine :

Now this time I think we are in good shape, and got most of the world covered. That being said, although those search engine are the most used ones, they are still general purpose search engine. If your site/blog is targeting a very specific audience, then you might want to get referenced in specialized search engine.

Also there's something you need to keep in mind, is that there's no better search engine than 'Word to Mouth' ... so try to have people talk/link to your site.

Finally if your site is a commercial one, or you REALLY want to get some traffic, then you might want to invest into Paid search advertising. There's a good article here about it. Most of the search engine above offer such kind of services.

Alex