Octopress — adding category tags to the blog RSS feed

Right from the beginning, I’ve assigned broad categories to every post I’ve written here. (For example, this is my—very lacking—Health Monitoring series of posts.) However, Octopress does not include these category tags by default into the RSS feed. So if a reader is using an RSS feed-reader app or website, they cannot make use of the assigned categories even if the app or website was capable of doing so.

I’ve now added some code necessary to add the categories to the RSS feed, and this is what I did.

cellArray = {'Alpha','Beta','Gamma','Delta','GammaSquared'};
refString = 'Gamma';

At the outset, here is the code that I added:

{% for post in site.posts limit: 20 %}
<entry>
<!-- Other items that are included in the feed -->

{% capture catnum %}{{ post.categories | category_links | size }}{% endcapture %}
{% unless catnum == '0' %}
    <categories>
    {% for cct in post.categories %}
        {% assign idx=forloop.index0 %}<category>{{ post.categories[idx] }}</category>
    {% endfor %}
    </categories>
{% endunless %}

<!-- Other items that are included in the feed -->
<content type="html"><![CDATA[{{ post.content | expand_urls: site.url | cdata_escape }}]]></content>
</entry>

{% endfor %}

This code works great, but allow me to confess that I am not sure that this is the optimum implementation. To me this seems inelegant, but until I have a better solution, this performs the function appropriately and perfectly adequately.

I’ve only included the relevant portion and the context in which it must be inserted. (See the comment tags <!-- Other items that are included in the feed -->.)

The meat of the algorithm is from lines 7 through 11.

  • A <categories> tag is defined, and a for loop is executed over post.categories, which contains the list of categories for the post.
  • Within the for loop, each post category is enclosed in a <category></category> tag.

Now I had initially thought that the loop variable (cct here) would inherit sequentially the value of each category in post.categories, but apparently that does not work properly. Therefore, the workaround is to

  • identify the loop index (assign idx=forloop.index0) and
  • use individual values of the categories (post.categories[idx]).

We must use forloop.index0 and NOT forloop.index (both are valid commands; the index key starts numbering from 1) because the array numbering starts from 0, not 1.

OK, now that the meat of the algorithm is done, we must put in some code to handle the “unusual” cases—what happens if a post does not have any categories assigned? Such a scenario is handled by the capture command (line 5) and the unless segment that encloses our actual algorithm. The capture command simply captures a value, in our case the number of categories that exist. We only want to include the categories when they exist, therefore our algorithm is run only unless catnum=='0' i.e. when the number of categories is not 0.

Well, that’s it! I have added the code segment before the actual content of each post, but I don’t think it makes any difference if the segment appears after the <content> tag. It should work fine anywhere within the <entry> environment.


☛ Creating “Linked-List” type posts

One of my long-time to-do’s for this blog was to be able to create “linked-list” type posts, where the main heading points, not to a single webpage for the dedicated blog post, but to an external website of interest. (This type of post has been made famous by John Gruber, who is, incidentally, also the creator of the Markdown syntax.)

Well, now I know how to do this (evidence—this post! Ta-da! The title for this post points to The Candler Blog). It turns out it’s not too difficult, but even so, I had help all the way, from The Candler Blog. He has this same implementation, and it turns out, he also has a blog post dedicated to discussing how he did it!

Okay, so, “Daring Fireball-style Linked List posts,” for the uninitiated, refers to the publishing style of John Gruber’s Daring Fireball. For the most thorough explanation of how this works, see Shawn Blanc’s excellent 2009 article, “The Link Post” […]

But how is it done in Octopress? It’s actually very simple. I got a great deal of help, when I was first setting up the site, from Connor Montgomery, who posted his own link post tutorial a few weeks ago. I have since refined the code on my site beyond what we worked out together.

(The Candler Blog website seems otherwise very interesting as well. Go check it out!)


Of Cricket, Mankad-ing, and the Spirit of the Game

The Under-19 cricket world cup is on, and there has been a lot of controversy about a West Indies bowler running a Zimbabwean batsman out as he came in to bowl. Colloquially, this is called ‘Mankad’-ing, and some people view this form of dismissal as “not quite done”. As it happens every time, lots of people are talking about “spirit of the game” and “no warnings issued to the batsman”.

I think those people are in the wrong.

(Here’s the video.)

What would these same people say when a bowler gets a wicket, but his heel is found to be where the bat is spotted in our case? “Spirit of the game”, and give the batsman out? “Give a warning to the bowler”, and give the batsman out? No, of course not, because the rulebook says some part of the bowler’s foot must stay behind the line. The bowler made a mistake, and is penalized for it.

Well, guess what the rulebook says in this case.

Also, to be clear, backing up itself is not illegal; backing up too early is. ICC playing conditions says that the bowler may attempt this dismissal only if he has not completed his delivery swing. So, in effect, once the bowler is in the middle of rolling his bowling arm over to bowl, the bowler can no longer run the batsman out, and the batsman is free to start backing up.

In my opinion, “spirit of the game” issues should only come up when a) the fielding side resorts to subterfuge, or b) it is “obvious” that the batsman is not attempting to take an advantage, and is behaving as if the play is dead. For examples of this second case, see:

  • http://youtu.be/6zgvjC9WUCs (bad spirit of the game),
  • https://youtu.be/AsznuSW-1Ug (good spirit of the game) and
  • http://youtu.be/9vYPWYAoJhM (good spirit of the game, even though it was a close rescue).

In our present case, the batsman was definitely attempting to take advantage, and his opponent ran him out perfectly legally. The batsman made a mistake, and was penalized for it. What’s wrong with that, and what’s all this about giving the batsman a second chance?!

Play on, I say! (Or in this case, game over!)