<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
    <title>Emil Loer</title>
    
    <link href="http://emilloer.com/" />
    <updated>2011-10-15T23:06:02+02:00</updated>
    <id>http://emilloer.com/</id>
    <author>
        <name>Emil Loer</name>
        <email>emil@koffietijd.net</email>
    </author>
    
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/EmilLoer" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="emilloer" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
            <title>The Rake task-killer</title>
            <link href="http://emilloer.com/2011/10/15/the-rake-task-killer/" />
            <updated>2011-10-15T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/10/15/the-rake-task-killer</id>
            <content type="html">&lt;p&gt;If you have a lot of private gems that are packaged using Bundler then there is always the fear of someone accidentaly running &lt;code&gt;rake release&lt;/code&gt; and pushing all of your fancy enterprise code to RubyGems. Let’s prevent this from happening!&lt;/p&gt;

&lt;p&gt;First, add the following to your &lt;code&gt;Rakefile&lt;/code&gt;, just below the require statements.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rake&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Task&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;kill!&lt;/span&gt;
    &lt;span class="vi"&gt;@actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;
    &lt;span class="n"&gt;prerequisites&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;
    &lt;span class="n"&gt;enhance&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;This task is not allowed!&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This snippet adds a &lt;code&gt;kill&lt;/code&gt; method to all Rake tasks, even those that are defined outside of the &lt;code&gt;Rakefile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, whenever you want to prevent a task from being used you can kill it using the following invocation:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Rake&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:release&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;kill!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When you invoke the &lt;code&gt;release&lt;/code&gt; task after calling &lt;code&gt;kill!&lt;/code&gt; on it you will be presented with a warning:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rake release
This task is not allowed!
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Of course you can use it to kill nested tasks too:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Rake&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:&amp;quot;db:drop&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;kill!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this example the Ruby on Rails &lt;code&gt;db:drop&lt;/code&gt; task is killed.&lt;/p&gt;

&lt;h4 id="replacing-tasks"&gt;Replacing tasks&lt;/h4&gt;

&lt;p&gt;Normally, when you create a task entry for a task that already exists the given block will be appended to the task. This means that vanilla Rake only has the ability to add work to a given task, not replace it.&lt;/p&gt;

&lt;p&gt;However, if we clear the actions first and then append a new block to the list of actions (this is what the &lt;code&gt;enhance&lt;/code&gt; call does) we can still manage to replace the entire task.&lt;/p&gt;

&lt;p&gt;Here’s an example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rake&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Task&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;replace&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;
    &lt;span class="vi"&gt;@actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;
    &lt;span class="n"&gt;prerequisites&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;
    &lt;span class="n"&gt;enhance&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Rake&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:release&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;replace&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Put your own release code here&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

</content>
        </entry>
    
        <entry>
            <title>The one true Objective-C brace style</title>
            <link href="http://emilloer.com/2011/08/24/the-one-true-objective-c-brace-style/" />
            <updated>2011-08-24T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/08/24/the-one-true-objective-c-brace-style</id>
            <content type="html">&lt;p&gt;Whenever you create a new project in Xcode there are a couple of Objective-C files automatically generated, such as the application delegate. One thing in particular that bothers me about these files is the messy coding style.&lt;/p&gt;

&lt;p&gt;One example of this is the inconsistent placing of the opening curly braces. On methods the opening brace is placed on a new line below the method declaration, and on if statements and the like they are placed on the same line.&lt;/p&gt;

&lt;p&gt;Most of the templates also have a lot of trailing whitespace, and the Xcode editor itself is rather sloppy when it comes to cleaning that up.&lt;/p&gt;

&lt;p&gt;Because I primarily use Vim for my editing needs, I wrote a little function that cleans things up. You can put it in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="vim"&gt;&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; TransformObjC&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c"&gt;    &amp;quot; correct the code style of an Xcode Objective-C file&lt;/span&gt;
    &lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; execute &lt;span class="s2"&gt;&amp;quot;%s/- (/-(/&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; execute &lt;span class="s2"&gt;&amp;quot;%s/+ (/+(/&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; execute &lt;span class="s2"&gt;&amp;quot;%s/\\n{\\n/ {\\r/&amp;quot;&lt;/span&gt;
    &lt;span class="k"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt; execute &lt;span class="s2"&gt;&amp;quot;%s/\\ \\+$//&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;endfunction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This function does the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Remove the space between the plus or minus and the return type parenthesis on method signatures.&lt;/li&gt;
  &lt;li&gt;Place the opening curly brace on the same line as the method signature.&lt;/li&gt;
  &lt;li&gt;Clean up any trailing whitespace.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because these regular expressions only do minor corrections you can call the function as an auto command when you load a new Objective-C file. This can be done as follows in the &lt;code&gt;.vimrc&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="vim"&gt;autocmd&lt;span class="p"&gt;!&lt;/span&gt; &lt;span class="nb"&gt;BufRead&lt;/span&gt; *.&lt;span class="k"&gt;m&lt;/span&gt; &lt;span class="k"&gt;call&lt;/span&gt; TransformObjC&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now you can easily mix edits in Vim and Xcode without being annoyed by sloppy formatting. Just remember to reload buffers when going from Xcode to Vim.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Dealing with project.pbxproj in Ruby</title>
            <link href="http://emilloer.com/2011/08/15/dealing-with-project-dot-pbxproj-in-ruby/" />
            <updated>2011-08-15T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/08/15/dealing-with-project-dot-pbxproj-in-ruby</id>
            <content type="html">&lt;p&gt;For Mac and iOS development the weapon of choice is Xcode. And it’s pretty good for what it does (if you disregard the current stability issues with Lion) when you are just making simple apps. In my own projects, however, things usually get more complex and I need to put a lot of stuff in static libraries and juggle these around in all the projects that use them. It turns out that using Xcode for this library management is quite cumbersome, so let’s see what we can do to automate this.&lt;/p&gt;

&lt;p&gt;When you look at a typical Xcode project you see a bunch of code assets and a &lt;code&gt;projectname.xcodeproj&lt;/code&gt; file. This file is actually a directory in disguise and contains all the project metadata. Most of this metadata is just settings for the Xcode user interface and is not directly related to our code.&lt;/p&gt;

&lt;p&gt;The file that we are interested in is called &lt;code&gt;project.pbxproj&lt;/code&gt;. This file contains the entire Xcode project structure. The project is described as an object graph, meaning that every entity in the project is a node that has an identifier, a type and zero or more references to other entities. The ideal representation for such a graph is a list of key-value pairs, and indeed the pbxproj file resembles this. Users of document oriented databases such as MongoDB should feel right at home.&lt;/p&gt;

&lt;p&gt;Now when you open the pbxproj file in a text editor you see that the file has a syntax that is somewhat reminiscent of JSON. What you are looking at is in fact a &lt;a href="http://en.wikipedia.org/wiki/Property_list"&gt;plist&lt;/a&gt; file in the ancient (and even deprecated) NeXTSTEP ASCII format. Why Apple still uses this format for Xcode remains a mystery to me.&lt;/p&gt;

&lt;p&gt;There are a couple of Ruby gems available to parse and manipulate pbxproj files. However, I find all of these gems too bloated for what they do. We can apply a bit of DRY by utilizing two existing tools.&lt;/p&gt;

&lt;p&gt;On OS X there is a command line utility called &lt;code&gt;plutil&lt;/code&gt; that, accoording to the manual page, is supposed to be used for syntax checking and plist format conversion. Of notable interest is that it can convert plist files to JSON, which we can parse using Ruby’s own JSON parser. Another interesting thing is that it can read all types of plist formats, including the NeXTSTEP format we are interested in.&lt;/p&gt;

&lt;p&gt;This means that a simple pbxproj parser can be shrunk down to a single line of code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_pbxproj&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;
  &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`plutil -convert json -o - &amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;&amp;quot;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This function returns a Hash object containing the entire object graph which you can then manipulate in whatever way you want. When the time comes to save the changes you can make use of these extra methods:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="n"&gt;to_plist&lt;/span&gt; &lt;span class="n"&gt;to_json&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_plist&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_plist&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;( &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; )&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Hash&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_plist&lt;/span&gt;
    &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_plist&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; = &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_plist&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;&amp;quot;&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;{ &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; }&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_pbxproj&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;
  &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_plist&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now remember that the only types that you should use in a plist are strings, arrays and hashes. If you have anything else, like an integer, you will have to convert it to a string.&lt;/p&gt;

&lt;p&gt;The methods above have been tested on about ten different projects using Xcode 4.1 and 4.2 beta and work perfectly. For best results reload your Xcode project after manipulating the pbxproj file with an external program, especially when developing for iOS.&lt;/p&gt;

&lt;p&gt;Of course, this is just the beginning. I have used this parser myself to build a tool that can synchronize Xcode projects and GNU makefiles with good success. What will you do to accelerate your workflow?&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Const function substitution in Clang</title>
            <link href="http://emilloer.com/2011/07/17/const-function-substitution-in-clang/" />
            <updated>2011-07-17T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/07/17/const-function-substitution-in-clang</id>
            <content type="html">&lt;p&gt;I am currently working on a synthesizer for the iPad. An important thing here is that the sound rendering callback must be as fast as possible so that you can reduce the audio buffer size and thus get a lower latency.&lt;/p&gt;

&lt;p&gt;This means that, if you want to do anything more than a stupid non-bandlimited monosynth with horrible sound quality (such as the currently trending free Core Synth, but there are many others), you will have to spend a lot of time optimizing the renderer. This is especially important when you want to build a polyphonic synthesizer, where rendering time scales linear with the amount of concurrently playing notes.&lt;/p&gt;

&lt;p&gt;Recently I discovered that Clang optimizes differently depending on whether you use single or double precision floating point numbers. The result of this is something you would not expect. Today I want to focus on a single aspect which might slow a lot of things down. Suppose you have this piece of code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="c"&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;123.456&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Any sane programmer would convert this to machine code by evaluating the exponential function during compilation and writing down a single load-constant operation in the compiled program. In other words, the compiler would treat the exponential function as a &lt;em&gt;const function&lt;/em&gt;. That is a function that produces an output value that only depends on the input, meaning that there are no side effects and every invocation of the function with a constant input value will always result in the same output value.&lt;/p&gt;

&lt;p&gt;However, it turns out that Clang can only optimize away some of these functions when they are called with constant parameters. So I’ve made a little table which tells you what functions you can safely use with constants. The functions that have a “no” will be compiled to a normal function call even when the input is a constant, so they should be replaced with precomputed constants if possible.&lt;/p&gt;

&lt;p&gt;So here’s the table. Measurements were done using the Apple LLVM 3.0 compiler which is found in the XCode 4.2 beta.&lt;/p&gt;

&lt;table&gt;
  &lt;col align="left" /&gt;
  &lt;col align="left" /&gt;
  &lt;col align="left" /&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Function&lt;/th&gt;
      &lt;th&gt;float&lt;/th&gt;
      &lt;th&gt;double&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;sin&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;cos&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;tan&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;asin&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;acos&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;atan&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;atan2&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;sinh&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;cosh&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;tanh&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;exp&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;exp2&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;expm1&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;log&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;log2&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;log10&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;log1p&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;fmod&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;fabs&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;sqrt&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;pow&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;ceil&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;floor&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;rint&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;round&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;trunc&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;When you write your own functions you can give Clang a hint that this function is a const function and has no side effects. This helps Clang to optimize away instances which are called with constants. In order to help Clang you have to modify your function declaration to be as follows:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="c"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;__attribute__&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;myfunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This trick originates from GCC, so more documentation about function attributes can be found in the &lt;a href="http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Function-Attributes.html"&gt;GCC documentation&lt;/a&gt;.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Plotting command line output</title>
            <link href="http://emilloer.com/2011/07/09/plotting-command-line-output/" />
            <updated>2011-07-09T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/07/09/plotting-command-line-output</id>
            <content type="html">&lt;p&gt;Consider the following piece of Python code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="python"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RCFilter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decaytime&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coeff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;decaytime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coeff&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is a simple one-pole lowpass &lt;a href="http://en.wikipedia.org/wiki/RC_circuit"&gt;RC-filter&lt;/a&gt;, frequently used as a smoother for parameters that change over time but should never be changed instantly. You instantiate the class with a time coefficient which indicates how many samples it it should take to reach a ratio of 1 / &lt;em&gt;e&lt;/em&gt; between the old and the new value. After that you can call &lt;code&gt;apply&lt;/code&gt; to smooth out an input signal.&lt;/p&gt;

&lt;p&gt;Now, we want to test the behaviour of this filter with a given signal. This can be done as follows:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="python"&gt;&lt;span class="nb"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;RCFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;xrange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now we have generated 120 samples, but from the numbers alone it is hard to see what is happening. However, we can use some command line magicks to turn this into a plot. For this we are going to use Gnuplot, which should be available in your favourite package manager.&lt;/p&gt;

&lt;p&gt;In order to make the plotting as simple as possible, we want to invoke it as a command line script that reads numbers from standard input and presents a plot on screen. This way we can pipe any program we write directly to our plot script.&lt;/p&gt;

&lt;p&gt;The hard part is getting Gnuplot to read stuff from the standard input stream of the invoking script. This is not trivial because Gnuplot flushes its own input stream and assumes that standard input is the same stream as the one that is used to enter the plot commands. That means we can only get this working if we concatenate the script and standard input. The Gnuplot commands to plot data from what Gnuplot considers the input stream are like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="gnuplot"&gt;&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="nb"&gt;term&lt;/span&gt; &lt;span class="n"&gt;pdfcairo&lt;/span&gt; &lt;span class="n"&gt;enhanced&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="n"&gt;solid&lt;/span&gt; \
    &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Helvetica Neue,8&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;lw&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;plot&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-&amp;quot;&lt;/span&gt; &lt;span class="nb"&gt;with&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="nb"&gt;title&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;These commands tell Gnuplot to produce a PDF file which has been rendered using Cairo. This gives us the best visual result. If you want to use PNG output you can use the &lt;code&gt;pngcairo&lt;/code&gt; engine instead, although I recommend using a tool like ImageMagick for the conversion because it generates better antialiased results and can handle transparency if you need it. We also set some good looking appearance settings, such as font, line width and output size (in inches at 72 dpi). The second line is the actual plot line and can of course be modified if you want a bar chart instead of a line chart or whatever.&lt;/p&gt;

&lt;p&gt;Using these commands directly will not work because we have not yet concatenated anything. To do that we will wrap the Gnuplot commands in a Bash script, like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="c"&gt;#/bin/bash&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="s2"&gt;        set term pdfcairo enhanced color solid \&lt;/span&gt;
&lt;span class="s2"&gt;            font \&amp;quot;Helvetica Neue,8\&amp;quot; lw 5 size 8,6; &lt;/span&gt;
&lt;span class="s2"&gt;        plot \&amp;quot;-\&amp;quot; with lines title \&amp;quot;\&amp;quot;&amp;quot;&lt;/span&gt;; 
    cat -
&lt;span class="o"&gt;)&lt;/span&gt; | gnuplot | open -f -a Preview
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now we have our plot commands concatenated with the script’s standard input and pipe that to Gnuplot. This produces a PDF file in standard output which we can pipe to our favourite display program. The &lt;code&gt;open&lt;/code&gt; command is Mac OS X specific and giving it the &lt;code&gt;-f -a Preview&lt;/code&gt; argument tells it to write standard input to a temporary file and open that file with Preview. For Linux you can use &lt;code&gt;display&lt;/code&gt;, &lt;code&gt;xdg-open&lt;/code&gt;, &lt;code&gt;eog&lt;/code&gt; or whatever.&lt;/p&gt;

&lt;p&gt;Now when we run our Python filter script and pipe it to this plot script we get the following window:&lt;/p&gt;

&lt;p&gt;&lt;img src="/2011/07/09/plotting-command-line-output/plot.png" /&gt;&lt;/p&gt;

&lt;p&gt;Because the generated PDF file contains a vector representation of the plot, you can resize the plot to whatever size you need without any loss of detail.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Searching the news with Tapir</title>
            <link href="http://emilloer.com/2011/05/04/searching-the-news-with-tapir/" />
            <updated>2011-05-04T00:00:00+02:00</updated>
            <id>http://emilloer.com/2011/05/04/searching-the-news-with-tapir</id>
            <content type="html">&lt;p&gt;Today I read an announcement that &lt;a href="http://tapirgo.com"&gt;Tapir&lt;/a&gt; had been launched. Tapir is a new search service for static page blogs. Because my own blog is based on a static site generator (Jekyll) I thought this was an interesting thing to check out.&lt;/p&gt;

&lt;p&gt;Tapir enables you to build your own search by using their API to fetch results, thus staying in control when it comes to appearance. Quite the difference when compared to Google Site Search, which maintains its own layout and such.&lt;/p&gt;

&lt;p&gt;So here’s how it works. You supply an RSS feed and email address to them and you get an API token for that in return. Then you can hit the API URL with the token and a search query and it will give you a JSON serialized list of results containing amongst other things a title, description and search relevance score. Using some jQuery magic (or whatever) you can display these results however you like.&lt;/p&gt;

&lt;p&gt;Now this is all cool and such, but a service like this can be used for many other purposes than searching in programmer blogs.&lt;/p&gt;

&lt;p&gt;To explore this I built a little news search engine. I created several API tokens, one for each in a set of news sites like CNN or the Dutch NOS. For each of the tokens the engine hits the API with a search. Now we have several result sets that we can concatenate and sort by relevance.&lt;/p&gt;

&lt;p&gt;Because Tapir will continue to poll RSS feeds every 15 minutes the total amount of news headlines you can search in will keep on growing. I wonder how useful such a thing would be after a couple of weeks.&lt;/p&gt;

&lt;p&gt;Anyway, you can see my news search engine at &lt;a href="http://emilloer.com/newssearch"&gt;http://emilloer.com/newssearch&lt;/a&gt;. Have fun!&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Fixing column alignment with jQuery</title>
            <link href="http://emilloer.com/2011/02/12/fixing-column-alignment-with-jquery/" />
            <updated>2011-02-12T00:00:00+01:00</updated>
            <id>http://emilloer.com/2011/02/12/fixing-column-alignment-with-jquery</id>
            <content type="html">&lt;p&gt;Lately I have been working a lot with &lt;a href="http://kramdown.rubyforge.org/"&gt;Kramdown&lt;/a&gt;, which is a superset of Markdown. One of the cool features it adds is support for tables. Here is an example:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="html"&gt;| Product      | Price |
|:-------------|------:|
| Franziskaner |  3.20 |
| Erdinger     |  2.50 |
| Paulaner     |  3.00 |
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you look carefully you can spot two colon characters in the header separating the header and body. These colons are used to specify the text alignment of the column. &lt;/p&gt;

&lt;p&gt;If the colon is on the left then it should be left-aligned. If it is right then it should be right-aligned. When you see a colon on either side the text should be centered. No colons at all means we should leave the text as-is.&lt;/p&gt;

&lt;p&gt;So in our little example table we have a left-aligned and a right-aligned column. Now let’s see how this table renders to HTML:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="html"&gt;&lt;span class="nt"&gt;&amp;lt;table&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;col&lt;/span&gt; &lt;span class="na"&gt;align=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;left&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;col&lt;/span&gt; &lt;span class="na"&gt;align=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;right&amp;quot;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Product&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Price&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;Franziskaner&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;3.20&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;Erdinger&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;2.50&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;Paulaner&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;3.00&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Oooh, that’s nasty! Kramdown specifies table alignment with the &lt;code&gt;align&lt;/code&gt; attribute of the &lt;code&gt;col&lt;/code&gt; tag. This was deprecated way back in HTML 4.01 and made obsolete in &lt;strike&gt;HTML5&lt;/strike&gt; HTML. And apparently the only browser that currently implements it is Internet Explorer. So let’s fix this by using a litle bit of jQuery magic.&lt;/p&gt;

&lt;p&gt;To do this we will have to find all &lt;code&gt;col&lt;/code&gt; tags and extract the alignment attribute from it. Then we will inject the value of the attribute in a CSS &lt;code&gt;text-align&lt;/code&gt; attribute. The trick here is that we need to find the cells that belong to each column.&lt;/p&gt;

&lt;p&gt;By iterating over the jQuery object containing the columns of a given table we are given an index. We can use this index in a &lt;code&gt;:nth-child&lt;/code&gt; selector to find the cell which belongs to the column. These cells always exist in row elements so we can limit our search to direct descendants of &lt;code&gt;tr&lt;/code&gt; tags that are either a &lt;code&gt;th&lt;/code&gt; or &lt;code&gt;td&lt;/code&gt; element. This gives us the final selector &lt;code&gt;tr&amp;gt;(th|td):nth-child(index)&lt;/code&gt; which we can apply to our table. Do note that the &lt;code&gt;:nth-child&lt;/code&gt; selector counts from 1!&lt;/p&gt;

&lt;p&gt;Here is the full code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="javascript"&gt;&lt;span class="c1"&gt;// For each table&lt;/span&gt;
&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Store the jQuery object to the current table for later use&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Iterate over the &amp;lt;col&amp;gt; tags in the current table&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;col&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;column&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        
        &lt;span class="c1"&gt;// Get the value of the align attribute&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;align&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;column&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;align&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        
        &lt;span class="c1"&gt;// Apply alignment if necessary&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;align&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;tr&amp;gt;(th|td):nth-child(&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;)&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                 &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;text-align&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;align&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

</content>
        </entry>
    
        <entry>
            <title>Serving static files from within a Rack app</title>
            <link href="http://emilloer.com/2011/02/01/serving-static-files-from-within-a-rack-app/" />
            <updated>2011-02-01T00:00:00+01:00</updated>
            <id>http://emilloer.com/2011/02/01/serving-static-files-from-within-a-rack-app</id>
            <content type="html">&lt;p&gt;Here’s a common pattern you might run across when developing a custom Rack application or middleware. Suppose you are developing some kind of web application that should serve everything from a certain directory except for the root index file, which we want to generate programmatically. &lt;/p&gt;

&lt;p&gt;Now, what I see very often is something like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;disk&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;However, in order to do it properly you will also have to implement decent error handling, proper header generation, permission handling and all that jazz. So the “read file from disk” step is actually a large chunk of code if done right. &lt;/p&gt;

&lt;p&gt;Luckily, we can apply some of the DRY principle here by using the built-in &lt;code&gt;Rack::File&lt;/code&gt; application class. A simple scaffold for the described application then comes down to just a few lines of code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
    &lt;span class="vi"&gt;@files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Put your own initialization here&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Get the request path&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;path_info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path_info&lt;/span&gt;

    &lt;span class="c1"&gt;# Check for paths we want to override&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;path_info&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;path_info&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/index.html&amp;quot;&lt;/span&gt;
      &lt;span class="c1"&gt;# Generate your content here&lt;/span&gt;
      &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Content-Type&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="c1"&gt;# Delegate to Rack::File&lt;/span&gt;
      &lt;span class="vi"&gt;@files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And that’s all there is to it. If you also want to serve directory indexes then you can replace &lt;code&gt;Rack::File&lt;/code&gt; with &lt;code&gt;Rack::Directory&lt;/code&gt;, which applies a similar technique under the hood.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Hashfeedr is back</title>
            <link href="http://emilloer.com/2011/01/17/hashfeedr-is-back/" />
            <updated>2011-01-17T00:00:00+01:00</updated>
            <id>http://emilloer.com/2011/01/17/hashfeedr-is-back</id>
            <content type="html">&lt;p&gt;About six months ago me and my good friends &lt;a href="http://twitter.com/michielheijkoop"&gt;Michiel Heijkoop&lt;/a&gt; and &lt;a href="http://twitter.com/pnoordhuis"&gt;Pieter Noordhuis&lt;/a&gt; teamed up to participate in the 2010 edition of the &lt;a href="http://djangodash.com"&gt;Django Dash&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The Django Dash is a contest in which up to three developers have 48 hours to create a new Django application from scratch. Our contest entry was called Hashfeedr and can be found at &lt;a href="http://hashfeedr.com"&gt;hashfeedr.com&lt;/a&gt;. A brief description of our application (from the site itself):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Hashfeedr is a real-time Twitter tracker designed to run on a big screen (e.g. a beamer) to provide the necessary “twentertainment” at your next party or display what the customers are tweeting about your company by running hashfeedr at the office.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After the contest the site went offline becase we had no proper place to host it anymore, and nobody had the time to do anything about it.&lt;/p&gt;

&lt;p&gt;However, today I am happy to announce that Hashfeedr is back online, powered by a new fast server, and that I will continue to maintain it here until bit rot eventually makes it fall apart. But let’s hope that moment will not be anytime soon.&lt;/p&gt;

&lt;p&gt;By the way, Hashfeedr is open source! You can find the source in our &lt;a href="https://github.com/hashfeedr/hashfeedr"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Preventing CSRF in Sinatra</title>
            <link href="http://emilloer.com/2011/01/15/preventing-csrf-in-sinatra/" />
            <updated>2011-01-15T00:00:00+01:00</updated>
            <id>http://emilloer.com/2011/01/15/preventing-csrf-in-sinatra</id>
            <content type="html">&lt;p&gt;Recently I have been doing a lot of development work with the Ruby based Sinatra framework. For very small applications it is an excellent choice. However, Sinatra is very minimal and does not venture far in the land of security. So if you’re dealing with sensitive form data it’s a good practice to add some sort of protection against Cross Site Request Forgery (CSRF) attacks.&lt;/p&gt;

&lt;p&gt;We can add CSRF protection in our Sinatra app by using the &lt;code&gt;Rack::Csrf&lt;/code&gt; middleware. You can install it from RubyGems:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;rack_csrf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now, to use the middleware we have to add the following snippet to our Sinatra application file. Note that we also add a session middleware. This is a dependency which is required by the CSRF middleware and is used to store a unique anti-forgery token per visitor.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;rack/csrf&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;configure&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:secret&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;some unique secret string here&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Csrf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:raise&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I added the &lt;code&gt;:raise&lt;/code&gt; option so that the middleware raises an exception in case of an attack. That enables us to see it in the error log.&lt;/p&gt;

&lt;p&gt;After restarting your app the middleware will check all POST, PUT and DELETE requests for the token. Any of these requests not having the token will be blocked, which means we will have to patch all our forms to supply the token. We can get the token from the middleware by calling the &lt;code&gt;Rack::Csrf.csrf_token&lt;/code&gt; method with our environment as the argument. There is also a &lt;code&gt;Rack::Csrf.csrf_tag&lt;/code&gt; method which works similarly but returns a string with an HTML input tag. &lt;/p&gt;

&lt;p&gt;I don’t feel like typing this long method call over and over in my templates, so let’s make some helper functions to keep our templates clean.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;helpers&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;csrf_token&lt;/span&gt;
    &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Csrf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csrf_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;csrf_tag&lt;/span&gt;
    &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Csrf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csrf_tag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you have worked with Rails these methods might seem familiar, and indeed they do similar things.&lt;/p&gt;

&lt;p&gt;Here is an example ERB snippet with the token inserted into a form:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="erb"&gt;&lt;span class="x"&gt;&amp;lt;form method=&amp;quot;post&amp;quot; action=&amp;quot;/tweet&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt;  &lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;csrf_tag&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x" /&gt;
&lt;span class="x"&gt;  &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;message&amp;quot;/&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt;  &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Submit a tweet!&amp;quot;/&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And there you have it! Your Sinatra app is safe from any CSRF attacks. For more information about the &lt;code&gt;Rack::Csrf&lt;/code&gt; middleware check out its &lt;a href="https://github.com/baldowl/rack_csrf"&gt;GitHub project page&lt;/a&gt;.&lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Control surface</title>
            <link href="http://emilloer.com/2010/12/20/control-surface/" />
            <updated>2010-12-20T00:00:00+01:00</updated>
            <id>http://emilloer.com/2010/12/20/control-surface</id>
            <content type="html">&lt;p&gt;I present you the results of two hours of photoshopping:&lt;/p&gt;

&lt;p&gt;&lt;img src="/2010/12/20/control-surface/mockup1.png" /&gt;&lt;/p&gt;

&lt;p&gt;These knobs are for a super secret project which was in desperate need of some fancy visuals. It was a first attempt at using only shape layers and layer styles to create depth effects, and I think it turned out pretty well. &lt;/p&gt;
</content>
        </entry>
    
        <entry>
            <title>Checking for dependencies in Bash</title>
            <link href="http://emilloer.com/2010/12/09/checking-for-dependencies-in-bash/" />
            <updated>2010-12-09T00:00:00+01:00</updated>
            <id>http://emilloer.com/2010/12/09/checking-for-dependencies-in-bash</id>
            <content type="html">&lt;p&gt;Here is a quick Bash trick to check for external command dependencies and install them if they aren’t available.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="k"&gt;function &lt;/span&gt;depcheck &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;type&lt;/span&gt; -P &lt;span class="nv"&gt;$1&lt;/span&gt; &amp;amp;&amp;gt;/dev/null &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="k"&gt;  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Installing $2&amp;quot;&lt;/span&gt;
  sudo apt-get -y install &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="k"&gt;  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Error installing $2&amp;quot;&lt;/span&gt;
  &lt;span class="nb"&gt;exit &lt;/span&gt;1
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;type&lt;/code&gt; internal command prints how a shell command is interpreted by Bash. Specifying the &lt;code&gt;-P&lt;/code&gt; argument causes it to enforce a shell path search, bypassing any aliases, functions and internal commands. A nonzero exit status is returned when the argument can’t be interpreted.&lt;/p&gt;

&lt;p&gt;Now, if the command exists all is well and we can return. When it isn’t, we are going to install it using &lt;code&gt;sudo&lt;/code&gt;. For this you can use whatever package manager you have available. In this snippet I am assuming you are running a Debian based system, which is in my case Ubuntu. Because the command you are checking for might be different from the package name it is in (e.g. &lt;code&gt;git&lt;/code&gt; in &lt;code&gt;git-core&lt;/code&gt;) we supply a package name as the second argument of our function. &lt;/p&gt;

&lt;p&gt;Using our function is easy. For example, this line adds a dependency for the previously mentioned &lt;code&gt;git&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;depcheck git git-core
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I use this function myself in my dotfiles bootstrapping script. This script clones my dotfiles repository, initializes git submodules and does some other small chores to prepare the account for some serious coding business. But it cannot do so without making sure Vim is available. ;)&lt;/p&gt;
</content>
        </entry>
    
</feed>

