Sunday, October 18, 2009

How to get Adsense ads more relevant to your site content

Adsense is a great tool to generate revenue on your pages, and very easy to use. A couple of click and editing into your site or blog (widget installation maybe), and you're good to go.

But you may have noticed that something the ads displayed on your site by Adsense, are not that relevant, it not totally out of it. The reason why this happens, is because, Adsense just like Google (the search engine side of the business ;) ) uses bots to crawl on your site and analyze its content. Up to here nothing that you wouldn't expect. The catch is, everything that is on your site may not always be relevant content, and those bots have no way to identify what is and what's not.

Naturally, the trick would be to be able to "guide" those bots in the right direction and clearly indicate the content they have to consider. This can be done with the help of two specific tags that you need to include into the code of your site. If you have a blog, that's around the body of your article, any part, box, div, etc.. that wraps what you want to be indexed. Those two tags are and .

The explanation that follow are a bit specific to blogger since that's what I'm using, but the general procedure is similar whatever the blog or site.

So in blogger you have to edit your template. If you don't know how to edit your template your can check this article. Now that you're editing your template you want to start searching the following:

<data:post.body> 


And replace it with:


<!-- google_ad_section_start-->
      <data:post.body/>
<!-- google_ad_section_end --> 


Here you notice that we point for Adsense the content we want to be used for your ads. Now you want want to do the opposite instead, that is point to the sections you don't want to be considered. In order to do that you have to wrap the content to ignore the same way we wrapped the post's body above:

<!-- google_ad_section_start(weight=ignore)-->
Your content to ignore goes here
<!-- google_ad_section_end -->

And That's it. It is as simple as that. One important note is, the change in your ads won't be immediate, and it may take up to two weeks for the change to actually reflect in the content of your ads.

I hope this will be useful to you, don't hesitate to drop me a line if you that post helped you.



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.

Wednesday, October 7, 2009

2 greats tools to develop and test regular expressions

Regular expression are a very powerful tool that every developer should know, use, and I'd even dare say abuse. But just because they are useful doesn't mean they are easy to use and master. It can even be rather painful to develop and test them.

This is mostly due to the lack of good tools to help test regular expression. By lack I mean that there is not a lot, but there's two that I've found pretty useful and up to the task.

Expresso

Expresso is a very need piece of software that allow you create test, and debug your regular expression. It has everything one may want to work with regular expressions:

It has 3 differents tabs:
  • the first one regular expression testing
  • the second one is a regular expression designer
  • the last one is your regular expression library, which contains a set of commonly used regular expression, but also the history of regular expression you've used recently
 It can even generate C# code to use the regular expression you've designed. You can export to several languages ( C#, VB, C++)

All in all that the one I most often use.


Rubular

The second one is called Rubular and comes in the form of an online tool. It is much simpler and doesn't boast as much features as Expresso, but what it does it does well. If you just want a quick testing tool for your regular expression, without installing anything, then Rubular is what you want:


Quick & easy, straightforward and efficient.



Now where to find those. For Expresso, you can find it here, and for Rubular it's available at this address. I hope you find those tools useful.



Saturday, October 3, 2009

How to view 720p HD trailers in Frontrow

I came to wonder recently why it is that the trailers you see in Frontrow are still in low quality (still good, but nothing close to HD). When you come to think of it, broadband speed have evolved over the year, and it is now common to what HD video on internet, and if you go on the Apple portal you can see the trailer in HD if you want to.

So the question is how to configure Frontrow to display those HD trailer. Well after some searching in the setting it appears there's no easy way to choose the quality of the trailer. After some rummaging around, here's the solution.

Open a terminal and type the following command

defaults write com.apple.frontrow EnableHDMovieTrailers -boolean true && killall "Front Row"
Now reopen Frontrow, go to the Trailer menu. There’s now a HD Trailer at the top of the list.

Voila ;)

Enjoy.

GTD Outlook: create email to task macro

On a daily basis, I usually receive a lot of emails. Some I will never read because they don't concern me, but for most of those emails, I need to answer. I sometime don't have time to answer right away. I used to put it in a folder, or leave the email unread, so that I remember that i need to do something about it. But when I was getting reaaally a lot of emails, that wasn't really working. It's wasn't really convenient. Not only it isn't convenient,

Similarly I sometime send email to people, but ... other people are busy, or can't or simply won't answer. So you have to chase them. But again that means you have to remember to chase them.
That makes a lot of things to remember, on top of the usual things you have to do. It wasn't really efficient, which is bad enough, but it also hampered my productivity.

So whenever I receive an email I need to answer, or send an email and make sure I receive an answer in the timeframe I expect (or chase that person), I started to create an outlook task for each of those email. This way I don't have to clutter my mind with little things, and leave room for other more important things to remember. I suppose was the result of reading those article about GTD and productivity.

If you wonder what GTD (also known as "Getting Things Done") is, it is a method (or a collection of various methods) put together by David Allen, famous productivity guru. He wrote a book on the subject called Getting Things Done which i very highly recommand. It's one of those Must read book.

That being said back to the point. That trick of manually creating a task for each mail I was sending or receiving very rapidly grew old.

So I've made a little macro in outlook, that simply create a task from an email, and automatically add that email as an attachement of the task. A very simple to use I just select an mail, and launch a macro and it pops up the create task window, with the selected email automatically pasted in copy. I only have to set the reminder date.

Heres' the code of the macro in question:
Sub MakeTaskFromMail(MyMail As Outlook.MailItem)
   Dim objTask As Outlook.TaskItem
   Set objTask = Application.CreateItem(olTaskItem)
   With objTask
       .Subject = MyMail.Subject
       .DueDate = MyMail.SentOn
   End With

   objTask.Attachments.Add MyMail
   objTask.Display

End Sub

Function GetCurrentItem() As Object
   Dim objApp As Outlook.Application

   Set objApp = Application
   On Error Resume Next
   Select Case TypeName(objApp.ActiveWindow)
       Case "Explorer"
           Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
       Case "Inspector"
           Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
       Case Else
           ' anything else will result in an error, which is
           ' why we have the error handler above
   End Select

   Set objApp = Nothing
End Function

Sub MakeTaskFromCurrentMail()
   Dim currentMail As Outlook.MailItem
   Set currentMail = GetCurrentItem()
   MakeTaskFromMail currentMail
End Sub

Now that works pretty well, but since I'm really lazy and like things to go smoothly, I created a little button that calls the macro, and that the final touch (one could even associate a shortcut). Now in a single click I create a task from any email in my inbox and only have to set the date I want to be reminded.

That simple trick really eased my life and now i almost never forget to answer an email or chase someone to get my answer.

Hopefully this will help others.

What is a core dump and how to generate one




In this article we’re going to discuss about core files, what are their use, and how to get them, and what we can do with them.

What is a core file and how to get one

Starting from the beginning, let’s first define what is a core file. A core file, also called core dump is basically a dump, or image of the memory of a computer at a given time. It’s like a photo of everything in memory of your computer at the time of the creation of the core, the registry the stack and heap, pretty much every live data. Usually people will say “Oh S…t the software has cored again”. It’s usually not a good sign.
In most of case the generation of a core dump is involuntary, and is the result of a *violent* crash of a software. Amongst the common cause we can cite memory corruption core dump, stack overflow, lack of memory or even device failure. If your biggest problem is how to dump core, rest assured that'd the easy part by far. If think my favorite one is segmentation fault core dump ;)
Despite the fact that a core file usually means you’ve got a serious bug in your software, they are tremendously useful. Since they are a snapshot photo of the memory at the time of the crash, this can help a lot to figure out what happened. With the help of a core file you can explore the exact conditions at the time of the crash.
Does it mean that core file are the paramount of debugging, the panacea to bug, certainly not. For instance in the case of memory corruption, by the time you get the core file, it’s already too late, the corruption happened seconds before … which means ages for a computer.

What to do with a core file ?

Now if you get a core file that’s good. You can use a debugger that’s able to read core dump on the systems the core was generated. Each system (Windows, Linux, Solaris …) has its own debugger, and each has its own function. So depending the system you’re using you will have to search for the appropriate tool to read core dump. Here’s the list of what tools you can use to debug cores:
  • Solaris Core Dump: dbx or gdb
  • Linux Core Dump: gdb
  • Mac OS Core Dump: gdb or Xcode
This list is by no mean complete since each system boast different tools. Windows is absent from the list because its a it different from other systems in that aspect. For more information about core dump equivalent on windows called minidump, you can check here.

How to generate a core and why ?

As we’ve said above, core dumps are generally the result of a violent crash, so why would one want to force the generation of a core dump ? The answer is to try to investigate dead lock or process hanging issues. When your process is really frozen, you will have to kill it anyway, but if you do it right you can at the same time generate a core dump that will help you later on with your investigation.
You have two ways to generate a core:
  • gcore : This command will generate a core file for a live process but leave the process running
  • kill -6 : as the name of the command, this will kill the process but also generate a core of your application
Additionally before using the kill command you may want to use the command pstack , which will generate a text file that constains the list of all process stack (and each thread of your process) currently running. Executing the command a couple of time before actually killing your application can prove useful, since it’ll give you a better image of what thead is active and doing what than a simple core would.