<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rob Searles &#187; Tutorials</title>
	<atom:link href="http://www.robsearles.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robsearles.com</link>
	<description>Musing on the business of and development for "The Web"</description>
	<lastBuildDate>Mon, 31 May 2010 15:44:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NodeJS Tutorial &#8211; Part 2, Routing</title>
		<link>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/</link>
		<comments>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/#comments</comments>
		<pubDate>Mon, 31 May 2010 15:26:45 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=352</guid>
		<description><![CDATA[Note: This is the second part of my NodeJS tutorial. You may want to go back to the first part of this tutorial if you haven&#8217;t already read it.
In the first part of this tutorial we looked at reading a document from CouchDB and keeping our markup separate from our code using Haml templates. We arrived [...]]]></description>
			<content:encoded><![CDATA[<div style="background: #FFFEEB; margin: 10px 0 0 0; padding: 5px 10px; border: 1px solid #ccc;"><strong>Note: </strong>This is the second part of my <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">NodeJS tutorial</a>. You may want to go back to the <a href="http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/">first part of this tutorial</a> if you haven&#8217;t already read it.</div>
<p>In the first part of this tutorial we looked at reading a document from CouchDB and keeping our markup separate from our code using Haml templates. We arrived at the point where we clicked on a link, but nothing happened. Today we are going to put that right with some very simple routing.</p>
<h2>The URL Module</h2>
<p>The basis of all routing is the URL. Fortunately, Node has some basic functions which handle <a href="http://nodejs.org/api.html#url-276">reading the URL</a> so we can then decide what the user is actually trying to do.</p>
<p>First, we want to make sure we have included the URL module.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The next step is to extrapolate the path name from the URL that the user has visited:</p>
<p><span id="more-352"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> url_parts <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now, when I go to http://127.0.0.1:8000/create in Chromium, the system output is as follows:<br />
/create<br />
/favicon.ico</p>
<p>As you can see I have to deal with /create but also /favicon.ico &#8211; unfortunately I don&#8217;t have one. We&#8217;ll deal with that later by using a 404 error.</p>
<p>The pages we want to be dealing with are as follows:</p>
<ul>
<li>/</li>
<li>create</li>
<li>/edit</li>
</ul>
<p>For simplicity&#8217;s sake we&#8217;ll put them into a switch statement:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display root&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/create'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display create&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/edit'</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display edit&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">break</span>;
<span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;oh dear, 404&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you test this out you should see the relevant text displayed in the standard output.</p>
<h2 style="font-size: 1.5em;">Tidy up Existing Code</h2>
<p>Now we have a basic router telling us that it has detected which page the user wants to display, but is then displaying the default page regardless. To correct this we need to put our existing code within a function and then call that function in the relevant part of the switch statement.</p>
<p>So, create a new (sub) function called display_root() in the main bulk of the http.createServer() function, expecting a url, a server request object and aserver response object as parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> display_root<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Within the body of the function, you want to insert the main bulk of the application that we wrote last time. I have included the complete code at the bottom of this tutorial, and all code is of course on <a href="http://github.com/ibrow/erdnodeflips">GitHub</a>.</p>
<p>Now call this function within the switch statement:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">display_root<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Handling Missing Pages</h2>
<p>As said, Chromium is looking for a favicon.ico file, but currently this application doesn&#8217;t have one. As such it needs to be able to handle missing files gracefully, with the correct 404 response.</p>
<p>Create a new function within the main bulk of the http.createServer() function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> display_404<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;lt;h1&amp;gt;404 Not Found&amp;lt;/h1&amp;gt;&quot;</span><span style="color: #009900;">&#41;</span>;
res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The page you were looking for: &quot;</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; can not be found&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that we have specified the header to be 404.</p>
<p>Now you can call this from within the switch:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">display_404<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>You can test it out by going to http://127.0.0.1:8000/qwerty<br />
You should see:</p>
<p><strong>404 Not Found</strong><br />
The page you were looking for: /qwerty can not be found</p>
<p>We could use a Haml template to move the content out of the code, but for the purposes of this tutorial it will suffice.</p>
<h2>The Form</h2>
<p>Now we want to generate a &#8220;Create&#8221; page &#8211; which will be a form allowing us to enter a list name and 1-5 items. (Note: I have changed from 10 items to 5 for no other reason that I am lazy, and don&#8217;t particularly want 10 things on my list, also it keeps the code a bit more clean!)</p>
<p>First, create the Haml template that will be used. We&#8217;ll keep this very simple for now. The key part is the form generation. After some trial and error, this mark-up seemed to work:</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">%form(action=url method=&quot;post&quot;)
      :each i in [0,1,2,3,4]
        %p %label Item
           %input(type=&quot;text&quot; name=&quot;item[]&quot;)
      %p
        %input(type=&quot;submit&quot; value=&quot;Create List&quot;)</pre></div></div>

<p>You may want to read up on Haml here: <a href="http://haml-lang.com">http://haml-lang.com</a></p>
<p>The function for displaying the page is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> display_create<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/create.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create New List&quot;</span><span style="color: #339933;">,</span>
		message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter up to 5 things to remember&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> url
	    <span style="color: #009900;">&#125;</span>;
	    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now if you fill in the items and submit, the page simply refreshes itelf. We need to get hold of the posted form data and do something useful with it.</p>
<p>This will be what we&#8217;ll focus on next time.</p>
<h2>Tutorial Recap Part 2</h2>
<p>In part 2 of the Node JS tutorial, we started to explore some very basic routing techniques. A number of dedicated routing modules have sprung up, of two of which are listed here:</p>
<ul>
<li>Tim Caswell&#8217;s <a href="http://github.com/creationix/node-router">node-router</a></li>
<li>Tom Hughes-Croucher&#8217;s <a href="http://github.com/sh1mmer/node-routing">node-routing</a></li>
<li>Find more on the <a href="http://wiki.github.com/ry/node/modules">Node Modules wiki page</a></li>
</ul>
<h3>Complete Code</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> url_parts <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/'</span><span style="color: #339933;">:</span>
	display_root<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/create'</span><span style="color: #339933;">:</span>
	display_create<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'/edit'</span><span style="color: #339933;">:</span>
	sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display edit&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000066; font-weight: bold;">break</span>;
    <span style="color: #003366; font-weight: bold;">default</span><span style="color: #339933;">:</span>
	display_404<span style="color: #009900;">&#40;</span>url_parts.<span style="color: #660066;">pathname</span><span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span>;
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the document root
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_root<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
			message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
			link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
			link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		    <span style="color: #009900;">&#125;</span>;
		    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span>
	    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
			title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
			message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
			items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		    <span style="color: #009900;">&#125;</span>;
		    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the list creat page
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_create<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/create.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create New List&quot;</span><span style="color: #339933;">,</span>
		message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Please enter up to 5 things to remember&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> url
	    <span style="color: #009900;">&#125;</span>;
	    <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * Display the 404 page for content that can't be found
     **/</span>
    <span style="color: #003366; font-weight: bold;">function</span> display_404<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;
&lt;h1&gt;404 Not Found&lt;/h1&gt;
&quot;</span><span style="color: #009900;">&#41;</span>;
	res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;
&nbsp;
The page you were looking for: &quot;</span><span style="color: #339933;">+</span>url<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; can not be found&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<img src="http://www.robsearles.com/?ak_action=api_record_view&id=352&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/31/nodejs-tutorial-part-2-routing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NodeJS Tutorial with CouchDB and Haml &#8211; ErdNodeFlips</title>
		<link>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/</link>
		<comments>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/#comments</comments>
		<pubDate>Fri, 28 May 2010 10:38:37 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=338</guid>
		<description><![CDATA[A tutorial for creating a basic Node JS application. Using Node, CouchDB and Haml]]></description>
			<content:encoded><![CDATA[<p>The aim of this tutorial is to create a <a href="http://nodejs.org">Node</a> powered web app using a <a href="http://couchdb.apache.org/">CouchDB</a> database and <a href="http://haml-lang.com/">Haml</a> based templates. I wanted something relatively simple that I could do in a couple of sittings, but complex enough to be of some actual use. In the end I opted for a &#8220;top 10 list&#8221; type application. The functionality being as follows:</p>
<ol>
<li>To list a number of items</li>
<li>To remove an item from the list</li>
<li>To add an item to the list</li>
</ol>
<p>Obviously, before getting cracking with this tutorial I had to think up a name for this &#8220;killer app&#8221; of the Node world.</p>
<p>So I give you Erd<strong>Node</strong>Flips! In honour of <a href="http://de.wikipedia.org/wiki/Erdnussflips">erdnussflips</a> &#8211; the strangely addictive <a href="http://en.wikipedia.org/wiki/Wotsits">Wotsit</a> type things that taste of peanut butter and stick to the top of your mouth. Yum! (OMG &#8211; they even have their own <a href="http://www.facebook.com/pages/Erdnussflips/110701296397">Facebook page</a>!)</p>
<p>All code is available on <a href="http://github.com/ibrow/erdnodeflips">GitHub</a>.</p>
<p><span id="more-338"></span></p>
<h2>Directory Structure and Libraries</h2>
<p>The first step is to create a directory structure for the application and bring in the relevant libraries. We need two directories underneath the top-level application root: templates (which will hold the haml files) and libs (which will contain 3rd party libraries). We are going to grab two libraries off <a href="http://gitbub.com">GitHub</a>:</p>
<ul>
<li><a href="http://github.com/felixge/node-couchdb">Felix Geisendörfer&#8217;s node-couchdb</a> &#8211; this is what we&#8217;ll be using to interact with the Couch database</li>
<li><a href="http://github.com/creationix/haml-js">Tim Caswell&#8217;s haml-js</a> &#8211; this is our haml template engine</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>libs,templates<span style="color: #7a0874; font-weight: bold;">&#125;</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> libs
$ git clone git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>felixge<span style="color: #000000; font-weight: bold;">/</span>node-couchdb.git
$ git clone http:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>creationix<span style="color: #000000; font-weight: bold;">/</span>haml-js.git
$ <span style="color: #7a0874; font-weight: bold;">cd</span> ..<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<h2>Basic Web Server</h2>
<p>Now we can get started. Open up a new file called <strong>main.js</strong> in your <a href="http://www.robsearles.com/2009/10/25/a-week-with-emacs-one-week-later/">favourite editor</a>. Now, just to make sure that everything is working at this early stage you can copy and paste the http server example from the nodejs.org front page and run that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/plain'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello World<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ node .<span style="color: #000000; font-weight: bold;">/</span>main.js</pre></div></div>

<p>Navigate to http://127.0.0.1:8000/ and after 2 seconds you should see &#8220;Hello World&#8221; appear. If you see this, all is working. We have the (very basic) base of our application.</p>
<h2>Talk to the Database</h2>
<p>Once we know that our basic setup is working, we can try and interact with the database. Before we can do this, however, we actually need to create the database and add an record &#8211; or document.</p>
<p>CouchDB comes with a nifty little utility called <a href="http://couchdb.apache.org/screenshots.html">Futon</a>. We&#8217;ll use this to quickly create our database and add a document.</p>
<ul>
<li>Go to http://localhost:5984/_utils/</li>
<li>Create a new database: <strong>erdnodeflips</strong></li>
<li>Create a new document</li>
<li>Add Field &#8211; name it <strong>name</strong></li>
<li>Double click on the value for <strong>name</strong>, enter<strong> &#8220;Don&#8217;t forget&#8221;</strong> (including the quotes)</li>
<li>Add another field &#8211; name it <strong>items</strong></li>
<li>Double click on the value for <strong>items</strong> enter: <strong>["item 1", "item 2", "item 3"]</strong> (including quotes and square brackets)</li>
<li>Click on Save Document &#8211; copy the <strong>_id</strong> value</li>
<li>Navigate to: http://localhost:5984/erdnodeflips/[value of "_id"] to see something like: {&#8221;_id&#8221;:&#8221;2d36f401bc4b82c9160e1a4ea936aba3&#8243;,&#8221;_rev&#8221;:&#8221;2-3b9f738a50ff9928348ce32b0eb807b2&#8243;,&#8221;items&#8221;:["item 1","item 2","item 3"],&#8221;name&#8221;:&#8221;Rob&#8217;s List&#8221;}</li>
</ul>
<p>To read this within node we simply need to know the _id of the document. Save this as a var for now:</p>
<p>var doc_id = &#8216;2d36f401bc4b82c9160e1a4ea936aba3&#8242;;</p>
<p>and replace the setTimeout function with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fetched my new doc from couch:'</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
     res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-= Fin =-&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This is pretty simple, and should be relatively straight forward to read. Essentially the code tries to get the document identified by doc_id, invoking a function when done: <strong>db.getDoc(doc_id, function(error, doc){});</strong></p>
<p>The function that is invoked has 2 arguments, one containing the error (if any) and the second containing the actual document returned (if any).</p>
<p>Within this function we can then deal with the two arguments as needed. If there is an error, we output that to the browser, if no error and a doc is returned, output that instead.</p>
<p>Finally, we end the server response: <strong>res.end(&#8221;-= Fin =-&#8221;);</strong>. Note, we must end the server response within the callback of the getDoc() function. Due to the event driven, asynchronous nature of Node, if we end the response after that function then the server response will more than likely be closed before getDoc() has had a chance to write the additional server responses.</p>
<p>So the code now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Fetched my new doc from couch:'</span><span style="color: #009900;">&#41;</span>;
	    res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
     res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-= Fin =-&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Generate Nicer Output</h2>
<p>If you run the above code, you should get something along the lines of:</p>
<p>Fetched my new doc from couch:{&#8221;_id&#8221;:&#8221;2d36f401bc4b82c9160e1a4ea936aba3&#8243;,&#8221;_rev&#8221;:&#8221;2-3b9f738a50ff9928348ce32b0eb807b2&#8243;,&#8221;items&#8221;:["item 1","item 2","item 3"],&#8221;name&#8221;:&#8221;Rob&#8217;s List&#8221;}-= Fin =-</p>
<p>So it works, but it doesn&#8217;t look very good.</p>
<p>First, lets create a Haml template that we can use to output something better.</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">!!! Strict
%html(lang=&quot;en&quot;)
  %head
    %title&amp;amp;= title
  %body
    %h1&amp;amp;= title
    %p&amp;amp;= message
    :if items.length === 0
      %p There are no items to remember
    :if items.length &amp;gt; 0
      %ul
        :each item in items
          %li&amp;amp;=</pre></div></div>

<p>I&#8217;m not going to go into great depth. You can find more information on Haml at the <a href="http://haml-lang.com/">Haml homepage</a> and also have a look at Tim&#8217;s <a href="http://howtonode.org/haml-for-javascript">excellent tutorial</a> on his Haml-js library.</p>
<p>Essentially, this template is going to build an XHTML strict page that will have a title, a message and then should loop through the items in the list. To get this displayed, we then need to open up the template file, render it and then output it as a server response. The code to do that is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
		    items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Notice that I am simply ending the response with the generated HTML. Again, it is worth keeping an eye on exactly where the response is being ended otherwise you might find you are trying to add to a conversation that has already ended.</p>
<p>We can do the same for the &#8220;document not found&#8221; route:</p>

<div class="wp_syntax"><div class="code"><pre class="haml" style="font-family:monospace;">!!! Strict
%html(lang=&quot;en&quot;)
  %head
    %title&amp;amp;= title
  %body
    %h1&amp;amp;= title
    %p&amp;amp;= message
    %p %a{href: link}&amp;amp;= link_text</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
		    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
		    link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Quick Recap for this Tutorial</h2>
<p>In this tutorial we&#8217;ve only scratched the surface of what can be done. However, in a relatively short space of time we have our Node web server communicating with a CouchDB database and displaying some valid strict XHTML. Not bad for one sitting!</p>
<p>To recap, our code now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Node-CouchDB: http://github.com/felixge/node-couchdb</span>
<span style="color: #003366; font-weight: bold;">var</span> couchdb <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/node-couchdb/lib/couchdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    client <span style="color: #339933;">=</span> couchdb.<span style="color: #660066;">createClient</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5984</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'localhost'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    db <span style="color: #339933;">=</span> client.<span style="color: #660066;">db</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'erdnodeflips'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Haml-js: http://github.com/creationix/haml-js</span>
<span style="color: #003366; font-weight: bold;">var</span> haml <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./libs/haml-js/lib/haml'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> doc_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'2d36f401bc4b82c9160e1a4ea936aba3'</span>;
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'text/html'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    db.<span style="color: #660066;">getDoc</span><span style="color: #009900;">&#40;</span>doc_id<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #339933;">,</span> doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/no-doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No Document Found&quot;</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;No document could be found&quot;</span><span style="color: #339933;">,</span>
		    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/create&quot;</span><span style="color: #339933;">,</span>
		    link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Create a new document&quot;</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./templates/doc.haml'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Erdnodeflip document: &quot;</span><span style="color: #339933;">+</span>doc.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
		    message<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Your Erdnusflip document was found!&quot;</span><span style="color: #339933;">,</span>
		    items<span style="color: #339933;">:</span> doc.<span style="color: #660066;">items</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#125;</span>;
		<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> haml.<span style="color: #660066;">render</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>locals<span style="color: #339933;">:</span> data<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
		res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>;
	    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span>;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Server running at http://127.0.0.1:8000/'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>The exciting thing here is that we&#8217;ve thrown in a link! However, if you click on it, it will simply reload the page.</p>
<p>Being able to handle this kind of basic page navigation and actually adding stuff to the database is for next time.</p>
<p>Feedback always welcome. You can find all the code on my <a href="http://github.com/ibrow/erdnodeflips">GitHub pages</a>.</p>
<p><strong>Valuable Node resources:</strong></p>
<ul>
<li>The <a href="http://nodejs.org">official Node JS</a> site</li>
<li>The <a href="http://github.com/ry/node/">Node Github</a> page</li>
<li><a href="http://howtonode.org/">How To Node</a> &#8211; a great place for tutorials and other tips</li>
<li><a href="http://nodeblogs.com/">Node Blogs</a> &#8211; which appears to be down at the moment, but again is very useful</li>
<li><a href="http://groups.google.com/group/nodejs">NodeJS Google Group</a> &#8211; keep your finger on the pulse</li>
</ul>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=338&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/28/nodejs-tutorial-with-couchdb-and-haml-erdnodeflips/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>jQuery Validate URL, adding &#8220;http://&#8221;</title>
		<link>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/</link>
		<comments>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/#comments</comments>
		<pubDate>Thu, 27 May 2010 11:36:27 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=333</guid>
		<description><![CDATA[A project we have going on here at ibrow towers involves quite a bit of jQuery Validation. Whilst the Validation plugin is great with the default functions it offers, what makes it truly excellent is the fact that it is easily extendable.
For example, one thing that has been bugging me is to validate a URL [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://www.liveunsigned.com">project</a> we have going on here at <a href="http://www.ibrow.com">ibrow towers</a> involves quite a bit of <a href="http://docs.jquery.com/Plugins/Validation/Validator">jQuery Validation</a>. Whilst the Validation plugin is great with the default functions it offers, what makes it truly excellent is the fact that it is easily extendable.</p>
<p>For example, one thing that has been bugging me is to validate a URL the user must type in the initial http://. But why? Can the validation not just assume that if, for example, <strong>www.robsearles.com</strong> is typed in, that the user actually meant <strong>http://www.robsearles.com</strong>?</p>
<p>Well, after a quick read of the documentation and a hack around this little irritant is now gone. It&#8217;s all down to <a href="http://docs.jquery.com/Plugins/Validation/Validator/addMethod">addMethod()</a> which lets you add a custom validation method. Lets create one called <strong>complete_url()</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;complete_url&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// will contain our validation code</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'You must enter a valid URL'</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>A quick word about return values. If the method returns true, everything is considered OK. However, if false is returned then validation is assumed to have failed.</p>
<p>Our new method needs to do three things:</p>
<ol>
<li> If no url, don&#8217;t do anything</li>
<li>Check that the user had entered http:// in their URL, if not, add it in for them.</li>
<li> Check that the URL is now valid. For this I just used the <a href="http://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js">source</a> for the original url validation method.</li>
</ol>
<p>Lets translate this into some code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">validator</span>.<span style="color: #660066;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;complete_url&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// if no url, don't do anything</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>val.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>; <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if user has not entered http:// https:// or ftp:// assume they mean http://</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!/^</span><span style="color: #009900;">&#40;</span>https?|ftp<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>\<span style="color: #339933;">/</span>\<span style="color: #006600; font-style: italic;">//i.test(val)) {</span>
        val <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://'</span><span style="color: #339933;">+</span>val; <span style="color: #006600; font-style: italic;">// set both the value</span>
        $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// also update the form element</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// now check if valid url</span>
    <span style="color: #006600; font-style: italic;">// http://docs.jquery.com/Plugins/Validation/Methods/url</span>
    <span style="color: #006600; font-style: italic;">// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009966; font-style: italic;">/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>val<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Validating the form with this new method is very straight forward:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#form1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;complete_url&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Feel free to use/copy/modify as you see fit. If you do use it, why not let me know, or drop me a trackback?</p>
<p>Have fun</p>
<p><strong>Other jQuery Specific Posts</strong></p>
<ul>
<li><a href="http://www.robsearles.com/2010/03/11/ie-7-indexof-replacement/">IE 7 indexOf() Replacement</a></li>
<li><a href="http://www.robsearles.com/2009/01/27/fixed-jquery-datepicker-not-working-in-thickbox/">Fixed – jQuery DatePicker not working in Thickbox</a></li>
<li><a href="http://www.robsearles.com/2008/11/26/jquery-link-override-with-search-box/">jQuery: Link override with search box</a></li>
</ul>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=333&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/27/jquery-validate-url-adding-http/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easily Add Current File to SVN Within Emacs</title>
		<link>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/</link>
		<comments>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/#comments</comments>
		<pubDate>Fri, 14 May 2010 13:59:59 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=310</guid>
		<description><![CDATA[A quick howto for adding the current working file to an SVN repository within Emacs]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve now been <a href="http://www.robsearles.com/2009/10/19/a-week-with-emacs/">using Emacs</a> for quite a long time, I have picked up a few tips along the way. One thing I&#8217;ve been doing more and more is using the shell within Emacs. (For more details see <a href="http://jamesthornton.com/emacs/node/emacs_442.html">here</a> and <a href="http://www.gnu.org/software/emacs/manual/html_mono/eshell.html">here</a>.)</p>
<p>Some of my most used shell commands are using SVN, such as add, revert etc. However, I couldn&#8217;t find anywhere to just add/revert the current file I am working on. Fortunately there is <a href="http://stackoverflow.com">Stack Overflow</a>! Someone with a <a href="http://stackoverflow.com/questions/455345/in-emacs-how-to-insert-file-name-in-shell-command">very similar issue</a> to me has had a couple of good answers, which I have &#8220;borrowed&#8221; and now adding files to my SVN repository is really easy.</p>
<p>First, add the following to your <strong>.emacs</strong> file</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define<span style="color: #66cc66;">-</span>key minibuffer<span style="color: #66cc66;">-</span>local<span style="color: #66cc66;">-</span>map <span style="color: #66cc66;">&#91;</span>f3<span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>insert <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">-</span>file<span style="color: #66cc66;">-</span><span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now either open up Emacs, or reload your <strong>.emacs</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">M<span style="color: #66cc66;">-</span>x load<span style="color: #66cc66;">-</span>file RET ~<span style="color: #66cc66;">/</span>.emacs RET</pre></div></div>

<p>Now, when you are working on a new file you can easily add it to SVN with the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">M<span style="color: #66cc66;">-!</span> svn add F3 RET</pre></div></div>

<p>Nice!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=310&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/05/14/easily-add-current-file-to-svn-within-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Perfect Desktop Day 1 &#8211; The Base System</title>
		<link>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/</link>
		<comments>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 15:23:14 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=292</guid>
		<description><![CDATA[Today I installed the base system on my perfect desktop.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-296" title="Arch Linux logo" src="http://www.robsearles.com/wp-content/uploads/2010/03/arch-linux-logo1.png" alt="arch-linux-logo" width="200" height="168" />So today is the day I start building <a href="http://www.robsearles.com/2010/03/16/my-perfect-desktop-day-0/">my perfect system</a>. Before getting going,  I had a good think about <a href="http://distrowatch.com/">which distro</a> to use for the base install. I&#8217;ve been using <a href="http://www.ubuntu.com/">Ubuntu</a> for about 2 years now and love the ease of package management with <a href="http://ubuntuforums.org/showthread.php?t=447386">apt and aptitude</a>. I had a bit of a <a href="http://www.robsearles.com/2010/03/17/installing-php5-3-on-ubuntu-karmic/">play around with Ubuntu Karmic yesterday</a> and I must say it looks fantastic, and with Lucid Lynx only a <a href="https://wiki.ubuntu.com/LucidReleaseSchedule">few weeks away</a> it might come as a bit of a surprise to discover that my choice is a distro I&#8217;ve never used before: <a href="http://www.archlinux.org">Arch</a>.</p>
<p><strong>Why Arch?</strong></p>
<p>There are three main reasons I&#8217;ve picked Arch over Ubuntu, or even <a href="http://www.debian.org/">Debian</a>.</p>
<ol>
<li>I really like the idea of Arch&#8217;s rolling release system. My laptop is currently running Jaunty and in order to get the latest packages I would have to upgrade to Karmic. With Arch this is less of an issue.</li>
<li>As mentioned in my initial post, the base system should be small, quick and light. With Arch you can pick and choose exactly what you want at installation time, keeping it bloat free.</li>
<li>Finally, Arch will be something new to play with!</li>
</ol>
<p>After <a href="http://www.archlinux.org/download/">downloading</a> and burning a Net Install ISO I followed the excellent <a href="http://wiki.archlinux.org/index.php/Official_Arch_Linux_Install_Guide">installation guide in the Arch Wiki</a>.</p>
<p>Installation was relatively painless, the only two slight issues being:</p>
<p>I chose Auto prepare for partitioning, but I didn&#8217;t really know what filesystem to choose. Eventually I opted for Ext4, with so far, no ill effects.</p>
<p>During Boot Device Selection, again I wasn&#8217;t really sure what to do, but after some Googling I <a href="http://news.softpedia.com/news/How-to-Install-Arch-Linux-59239.shtml">found this page</a>, whose advice I followed, but just pressing enter.</p>
<p>I restarted and was greeted with a shell prompt. I quickly pinged Google and my network was still up and running through the ethernet on DHCP.</p>
<p>The next step up a non-root user and X. Again, the Arch <a href="http://wiki.archlinux.org/index.php/Beginners'_Guide">documentation</a> is excellent.</p>
<p>Setting up X was relatively simple. I originally decided to not opt for hotplugging, which was a mistake because I only have a USB keyboard and mouse, neither of which I could use in X<em> (muppet of the day award goes to me)</em>. After turning off the machine and setting up hotplugging, all was fine!</p>
<p>I have been using the <a href="http://awesome.naquadah.org/">Awesome window manager</a> for about a month now. Awesome is a tiling window manager which is heavily geared towards being keyboard friendly, and as it&#8217;s name suggests, it is pretty awesome.</p>
<p>Unlike Jaunty, installing the latest stable version of Awesome was a breeze, I simply ran:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># pacman -S awesome</span></pre></div></div>

<p>and edited my ~/.xinitrc file adding:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">exec</span> awesome</pre></div></div>

<p>at the bottom. When I started X again, Awesome was there without any problem.</p>
<p>Sound was equally as simple to install, which was a nice surprise.</p>
<p>So my base system is now setup, with my window manager of choice in place and sound ready and waiting for my music collection. And all this running under 100MB of RAM!</p>
<p>After my first foray into the world of Arch I&#8217;m pretty impressed. <a href="http://wiki.archlinux.org/index.php/Pacman">Pacman</a> is very straight forward to use, after a bit of tweaking here and there it has just worked.</p>
<p>Next time, file management tools.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=292&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/18/my-perfect-desktop-day-1-the-base-system/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing PHP5.3 on Ubuntu Karmic</title>
		<link>http://www.robsearles.com/2010/03/17/installing-php5-3-on-ubuntu-karmic/</link>
		<comments>http://www.robsearles.com/2010/03/17/installing-php5-3-on-ubuntu-karmic/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 23:02:53 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=275</guid>
		<description><![CDATA[Following my previous post about installing PHP5.3 on Jaunty, I have had a few requests about installing it on Karmic. I&#8217;ve never used Karmic before so I thought it would be a fun excuse to have a poke about whilst also keeping my blog (sort of) up-to-date.
After freshly installing Karmic (the desktop version looks very [...]]]></description>
			<content:encoded><![CDATA[<p>Following my <a href="http://www.robsearles.com/2009/10/04/installing-php53-on-ubuntu-jaunty">previous post about installing PHP5.3 on Jaunty</a>, I have had a few requests about installing it on Karmic. I&#8217;ve never used Karmic before so I thought it would be a fun excuse to have a poke about whilst also keeping my blog (sort of) up-to-date.</p>
<p>After freshly installing Karmic (the desktop version looks very nice btw) on a new <a href="http://virtualbox.org/">VirtualBox</a> image and then updating with the latest, er, updates I was ready to begin.</p>
<p>The entire installation procedure is very simple, once all dependencies are met.</p>
<p>Firstly you must edit your sources list to include the <a href="http://www.dotdeb.org/">DotDeb package repository</a> and the old Jaunty security packages to meet dependencies.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apt<span style="color: #000000; font-weight: bold;">/</span>sources.list</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># php5.3</span>
deb http:<span style="color: #000000; font-weight: bold;">//</span>php53.dotdeb.org stable all
deb-src http:<span style="color: #000000; font-weight: bold;">//</span>php53.dotdeb.org stable all
deb http:<span style="color: #000000; font-weight: bold;">//</span>security.ubuntu.com<span style="color: #000000; font-weight: bold;">/</span>ubuntu jaunty-security main</pre></div></div>

<p>Now update and upgrade Ubuntu</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> update
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

<p>You may find that you get the following warning:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">WARNING: The following packages cannot be authenticated<span style="color: #000000; font-weight: bold;">!</span>
libevent-<span style="color: #000000;">1.4</span>-<span style="color: #000000;">2</span>
Install these packages without verification <span style="color: #7a0874; font-weight: bold;">&#91;</span>y<span style="color: #000000; font-weight: bold;">/</span>N<span style="color: #7a0874; font-weight: bold;">&#93;</span>?</pre></div></div>

<p>I just answered Y and continued as normal</p>
<p>Once successfully upgraded then you can install Apache, MySQL and PHP as normal, feel free to cater the installation line to your tastes.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libicu38 apache2 apache2-mpm-prefork mysql-client-<span style="color: #000000;">5.1</span> mysql-server-<span style="color: #000000;">5.1</span> php5 php5-cli php5-mysql libapache2-mod-php5</pre></div></div>

<p>You will get a warning about untrusted packages being installed but this simply means that the PHP5.3 stuff is not signed, so I typed in “yes” and carried on.</p>
<p>Robmorin <a href="http://www.robsearles.com/2009/10/04/installing-php53-on-ubuntu-jaunty/?dsq=40007017#comment-39744535">commented in my Jaunty post</a> that he had issues when installing PHP MCrypt. The answer can be found within the <a href="http://www.dotdeb.org/2009/07/03/php-5-3-0-final-preview-packages-available-for-debian-lenny/#comment-1077">DotDeb comments </a></p>
<p>After you have installed everything you need to, test it out, either command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ php <span style="color: #660033;">-v</span>
PHP 5.3.2-0.dotdeb.1 with Suhosin-Patch <span style="color: #7a0874; font-weight: bold;">&#40;</span>cli<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>built: Mar  <span style="color: #000000;">9</span> <span style="color: #000000;">2010</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">14</span>:<span style="color: #000000;">53</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1997</span>-<span style="color: #000000;">2009</span> The PHP Group
Zend Engine v2.3.0, Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1998</span>-<span style="color: #000000;">2010</span> Zend Technologies with Suhosin v0.9.29, Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2007</span>, by SektionEins GmbH</pre></div></div>

<p>Or by using phpinfo() page within your Apache installation:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>info.php</pre></div></div>

<p>&lt;?PHP<br />
phpinfo();<br />
?&gt;</p>
<p>then fire up http://localhost/info.php and at the top of the page you should see something along the lines of<br />
<strong>PHP Version 5.3.2-0.dotdeb.1</strong></p>
<p>Note: if your browser asks if you want to download info.php, simply restart Apache and try again:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>All done. Not quite as simple as on Jaunty, but still better than compiling the source!</p>
<p>Post Script: Before starting this post, I did a quick search looking for tutorials to see if the work had already been done, but couldn&#8217;t find anything on installing PHP5.3 on Karmic. As there wasn&#8217;t anything I decided to continue with my tutorial and I ended up struggling for an hour or so with the dependency issues. Typically, after I had figured it out for myself, whilst searching for another dependency issue I found <a href="http://blog.jmoz.co.uk/post/435401471/install-php-5-3-on-ubuntu-karmic-koala-from-dotdeb">this post on the JMOZ blog about installing PH5.3 on Karmic Koala</a>.  No idea why it&#8217;s not in Google, but either way, damn, I could have saved myself an hour or so!</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=275&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/17/installing-php5-3-on-ubuntu-karmic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Converting from Mac to Unix line endings</title>
		<link>http://www.robsearles.com/2010/03/10/converting-from-mac-to-unix-line-endings/</link>
		<comments>http://www.robsearles.com/2010/03/10/converting-from-mac-to-unix-line-endings/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 11:12:18 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=263</guid>
		<description><![CDATA[This is more a post for me than anyone else, but I thought I&#8217;d share anyway.
In our team we have a mixture of Linux and Mac users, and we are constantly editing the same files. One of the most irritating things is when a file is saved with Mac line endings instead of Unix line [...]]]></description>
			<content:encoded><![CDATA[<p>This is more a post for me than anyone else, but I thought I&#8217;d share anyway.</p>
<p>In our team we have a mixture of Linux and Mac users, and we are constantly editing the same files. One of the most irritating things is when a file is saved with Mac line endings instead of Unix line endings (displaying as <strong>^M</strong> in <a href="http://www.robsearles.com/2009/10/25/a-week-with-emacs-one-week-later/">Emacs</a>). This happens very rarely, but on the occasion it does I can quickly convert back using the following commands on my Linux box:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #000000; font-weight: bold;">&lt;</span> file-in-question.txt <span style="color: #000000; font-weight: bold;">&gt;</span> tmp.file
$ <span style="color: #c20cb9; font-weight: bold;">mv</span> tmp.file file-in-question.txt</pre></div></div>

<p>Works perfectly</p>
<p>If you want to find more about the tr (Translate) program, <a href="http://linux.die.net/man/1/tr">visit the man page</a>.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=263&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2010/03/10/converting-from-mac-to-unix-line-endings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Bitwise Operators (hopefully)</title>
		<link>http://www.robsearles.com/2009/12/04/understanding-bitwise-operators-hopefully/</link>
		<comments>http://www.robsearles.com/2009/12/04/understanding-bitwise-operators-hopefully/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 23:13:02 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=256</guid>
		<description><![CDATA[After the trouble I had with bitwise operators yesterday I found some time to really sit down and get my head properly around them. Let&#8217;s dive straight in.
We need to initially define our flags:

define&#40;'BASE', 0&#41;; // binary 00000000
define&#40;'F1', 1&#41;;   // binary 00000001
define&#40;'F2', 2&#41;;   // binary 00000010
define&#40;'F3', 4&#41;;   // binary 00000100

To start with we have no [...]]]></description>
			<content:encoded><![CDATA[<p>After the <a href="http://www.robsearles.com/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/">trouble I had with bitwise operators yesterday</a> I found some time to really sit down and get my head properly around them. Let&#8217;s dive straight in.</p>
<p>We need to initially define our flags:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'BASE'</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// binary 00000000</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F1'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;   <span style="color: #666666; font-style: italic;">// binary 00000001</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F2'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>;   <span style="color: #666666; font-style: italic;">// binary 00000010</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F3'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>;   <span style="color: #666666; font-style: italic;">// binary 00000100</span></pre></div></div>

<p>To start with we have no flags set, so if we set F1 using the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1_set</span> <span style="color: #339933;">=</span> BASE <span style="color: #339933;">+</span> F1; <span style="color: #666666; font-style: italic;">// $f1_set = 1</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;F1 set = $f1_set<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>All is well and good, $f1_set = 1 as expected.</p>
<p>However, what if we set F1 again?</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1_set_twice</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_set</span> <span style="color: #339933;">+</span> F1; <span style="color: #666666; font-style: italic;">// $f1_set_twice = 2 !!! wrong !!!</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;F1 set twice = $f1_set_twice<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>As you can see, if we set F1 twice, it effectively &#8220;unsets&#8221; F1 and sets F2. Not what we were after.</p>
<p>So why is this? Well, it&#8217;s kind of obvious and I was being a bit of a muppet for not spotting it yesterday. The reason is pretty simple: 1 + 1 = 2. (I told you it was obvious!)</p>
<p>Clearly this is not what we want, but how can we solve this? By using the bitwise OR. If we change the statements slightly as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1_or_set</span> <span style="color: #339933;">=</span> BASE | F1; <span style="color: #666666; font-style: italic;">// $f1_or_set = 1</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;F1 OR set = $f1_or_set<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$f1_or_set_twice</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_or_set</span> | F1; <span style="color: #666666; font-style: italic;">// $f1_or_set_twice = 1 - huzzah!</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;F1 OR set twice = $f1_or_set_twice<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>As far as &#8220;unsetting&#8221; the flags if we use my original method we fall (again) into trouble.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">=</span> BASE | F1 | F3; <span style="color: #666666; font-style: italic;">// $f1_and_f3 = 5;</span>
<span style="color: #000088;">$unset_f3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">-</span> F3; <span style="color: #666666; font-style: italic;">// $unset_f3 = 1</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F3 = $unset_f3<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$unset_f1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">-</span> F1; <span style="color: #666666; font-style: italic;">// $unset_f1 = 4;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F1 = $unset_f1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Now, if we try to &#8220;unset&#8221; F1 twice, we arrive at the problem.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$unset_f1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">-</span> F1; <span style="color: #666666; font-style: italic;">// $unset_f1 = 3;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F1 = $unset_f1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Unsetting F1 twice here effectively turns off F3 and sets F1 and F2 &#8211; completely wrong!</p>
<p>Instead, if we use the &amp;~ binary operator mentioned in <a href="http://www.robsearles.com/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/#disqus_thread">Jesper&#8217;s comment</a> all works as expected. (note: I can&#8217;t find mention of this operator in the PHP docs, please someone help me out)</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">=</span> BASE | F1 | F3; <span style="color: #666666; font-style: italic;">// $f1_and_f3 = 5;</span>
<span style="color: #000088;">$unset_f3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">&amp;</span>amp;~ F3; <span style="color: #666666; font-style: italic;">// $unset_f3 = 1</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F3 = $unset_f3<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$unset_f1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">&amp;</span>amp;~ F1; <span style="color: #666666; font-style: italic;">// $unset_f1 = 4;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F1 = $unset_f1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Even if we try to &#8220;unset&#8221; a flag twice, it still has the same results:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$unset_f1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_and_f3</span> <span style="color: #339933;">&amp;</span>amp;~ F1; <span style="color: #666666; font-style: italic;">// $unset_f1 = 4;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F1 = $unset_f1<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$unset_f1_twice</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$unset_f1</span> <span style="color: #339933;">&amp;</span>amp;~ F1; <span style="color: #666666; font-style: italic;">// $unset_f1_twice = 4;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Unset F1 twice = $unset_f1_twice<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Also in Jesper&#8217;s comment and <a href="http://noehr.org/2009/08/27/bitwise-permissions-in-python-and-django/">original post</a> was the use of the left shift operator: &lt;&lt;. After playing around with this it seems very simple to use, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;    <span style="color: #666666; font-style: italic;">// 00000001</span>
<span style="color: #000088;">$f2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">1</span>; <span style="color: #666666; font-style: italic;">// 00000010</span>
<span style="color: #000088;">$f3</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">2</span>; <span style="color: #666666; font-style: italic;">// 00000100</span>
<span style="color: #000088;">$f4</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">3</span>; <span style="color: #666666; font-style: italic;">// 00001000</span></pre></div></div>

<p>Or to put it another way:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$f1</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;      <span style="color: #666666; font-style: italic;">// 00000001</span>
<span style="color: #000088;">$f2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">1</span>; <span style="color: #666666; font-style: italic;">// 00000010</span>
<span style="color: #000088;">$f3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f2</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">1</span>; <span style="color: #666666; font-style: italic;">// 00000100</span>
<span style="color: #000088;">$f4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f3</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">1</span>; <span style="color: #666666; font-style: italic;">// 00001000</span></pre></div></div>

<p>After all this I think I am a little closer to understanding Bitwise operations, hopefully! Tomorrow I&#8217;ll have a crack at testing to see if a flag is turned on or not. Until then&#8230;</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=256&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2009/12/04/understanding-bitwise-operators-hopefully/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bitwise Operators used for Flagging Items</title>
		<link>http://www.robsearles.com/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/</link>
		<comments>http://www.robsearles.com/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 17:34:21 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=245</guid>
		<description><![CDATA[I have always wondered what the point of Bitwise Operators were,to me they seem to belong to a distant past. However, after reading a couple of great blog posts I have at last an understanding of how they can be put to use, and have started playing around with them a bit (ba dum!).]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong>: Thanks to <a href="http://noehr.org/">Jesper Noehr</a> of <a href="http://bitbucket.org/">BitBucket</a> fame for pointing out <strong>gaping flaws</strong> in my post below (<a href="/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/#disqus_thread">see his comment</a>). I strongly advise you disregard all I have said below, because it will get you into a mess, in much the same way it has me. I&#8217;m going to sit down when I have a spare 1/2 hour and try to work out exactly what is going on! Many thanks and big kudos to Jesper, I really appreciate the time you took to correct me.</p>
<hr />
<p>I have always wondered what the point of <a href="http://en.wikipedia.org/wiki/Bitwise_operation">Bitwise Operators</a> were,to me they seem to belong to a distant past. However, after reading a couple of great blog posts I have at last an understanding of how they can be put to use, and have started playing around with them a bit (ba dum!).</p>
<p>Jesper Noehr has written about using <a href="http://noehr.org/2009/08/27/bitwise-permissions-in-python-and-django/">bitwise operators for a flexible permissions scheme</a> within Python  and Jonathan Snook has taken the bitwise concept further creating a <a href="http://snook.ca/archives/javascript/creative-use-bitwise-operators">great calendar app in Javascript</a>. After reading these I thought I better dive in, and an opportunity came along yesterday when I had to code a flagging system within PHP.</p>
<p><span id="more-245"></span></p>
<p>For the benefit of this post I have greatly simplified the problem but hopefully it should be enough to get an understanding and allow you to get started with playing around with bits.</p>
<p>The problem is as follows. I have a number of items and each item has 3 different flags associated with it. We&#8217;ll call these flags F1, F2 and F3. A single item can have none, some or all flags set, and needs to have the ability to turn each flag on or off independently. The &#8220;base&#8221; state for each item is that no flags are set.</p>
<p>Each flag is a boolean, on or off, or to put it another way, 1 or 0. This translates perfectly into binary numerals. If we visualise this with a number of examples:</p>
<p><strong>No flags switched on</strong></p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">F1</td>
<td valign="top">F2</td>
<td valign="top">F3</td>
<td valign="top">Binary Number</td>
</tr>
<tr>
<td valign="top">0</td>
<td valign="top">0</td>
<td valign="top">0</td>
<td valign="top">0</td>
</tr>
</tbody>
</table>
<p><strong>Just F1 switched on</strong></p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">F1</td>
<td valign="top">F2</td>
<td valign="top">F3</td>
<td valign="top">Binary Number</td>
</tr>
<tr>
<td valign="top">1</td>
<td valign="top">0</td>
<td valign="top">0</td>
<td valign="top">001 or 1</td>
</tr>
</tbody>
</table>
<p><strong>Just F2 switched on</strong></p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">F1</td>
<td valign="top">F2</td>
<td valign="top">F3</td>
<td valign="top">Binary Number</td>
</tr>
<tr>
<td valign="top">0</td>
<td valign="top">1</td>
<td valign="top">0</td>
<td valign="top">010 or 10</td>
</tr>
</tbody>
</table>
<p><strong>Just F3 switched on</strong></p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">F1</td>
<td valign="top">F2</td>
<td valign="top">F3</td>
<td valign="top">Binary Number</td>
</tr>
<tr>
<td valign="top">0</td>
<td valign="top">0</td>
<td valign="top">1</td>
<td valign="top">100</td>
</tr>
</tbody>
</table>
<p><strong>F1 and F3 switched on</strong></p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">F1</td>
<td valign="top">F2</td>
<td valign="top">F3</td>
<td valign="top">Binary Number</td>
</tr>
<tr>
<td valign="top">1</td>
<td valign="top">0</td>
<td valign="top">1</td>
<td valign="top">101</td>
</tr>
</tbody>
</table>
<p>The reason we have translated the binary number for just F1 switched on to 001 and not 100 is because from a text point of view, we read F1, F2 then F3 from left to right, but from a numeric point of view we read numbers right to left, i.e. 1s then 10s then 100s then 1000s etc.</p>
<p>From the above examples, we can see that if F1 is already switched on, we can easily switch on F3 as well by simply turning on the F3 bit. We accomplish this by simply adding the F3 bit:<br />
Base + F1 + F3 = 0 + 1 + 100 = 000 + 001 + 100  = 101</p>
<p>We can also remove flags, for example if we have F3 and F1 set, but then remove F1 we have:<br />
101 &#8211; 001 = 101 &#8211; 1 = 100<br />
But even though these are all ones and noughts we can translate these into decimal. PHP has a handy little utility function for this: decbin() [http://php.net/manual/en/function.decbin.php]</p>
<table class="tbl-border" border="1" cellspacing="2" cellpadding="2" width="50%">
<tbody>
<tr>
<td valign="top">State</td>
<td valign="top">Binary</td>
<td valign="top">Decimal</td>
</tr>
<tr>
<td valign="top">All off</td>
<td valign="top">0</td>
<td valign="top">0</td>
</tr>
<tr>
<td valign="top">F1 on</td>
<td valign="top">1</td>
<td valign="top">1</td>
</tr>
<tr>
<td valign="top">F2 on</td>
<td valign="top">10</td>
<td valign="top">2</td>
</tr>
<tr>
<td valign="top">F3 on</td>
<td valign="top">100</td>
<td valign="top">4</td>
</tr>
<tr>
<td valign="top">F1 and F3 on</td>
<td valign="top">101</td>
<td valign="top">5 (1 + 4)</td>
</tr>
<tr>
<td valign="top">All on</td>
<td valign="top">111</td>
<td valign="top">7 (1 + 2 + 4)</td>
</tr>
</tbody>
</table>
<p>So now this makes it really easy to turn flags on an off.<br />
In PHP it would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'BASE'</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F1'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F2'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="">'F3'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;No flags = &quot;</span><span style="color: #339933;">.</span>BASE<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span>BASE<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// switching flags on</span>
<span style="color: #000088;">$f1_on</span> <span style="color: #339933;">=</span> BASE<span style="color: #339933;">+</span>F1;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Turn on F1 = $f1_on:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f1_on</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$f2_on</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f1_on</span><span style="color: #339933;">+</span>F2;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Turn on F2 = $f2_on:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f2_on</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$f3_on</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f2_on</span><span style="color: #339933;">+</span>F3;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Turn on F3 = $f3_on:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f3_on</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">// switching flags off</span>
<span style="color: #000088;">$f2_off</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f3_on</span><span style="color: #339933;">-</span>F2;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Turn off F2 = $f2_off:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f2_off</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #000088;">$f1_off</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$f2_off</span><span style="color: #339933;">-</span>F1;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Turn off F1 = $f1_off:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">decbin</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$f1_off</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;</pre></div></div>

<p>Save this as bits.php and then run it from the command line with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ php .<span style="color: #000000; font-weight: bold;">/</span>bits.php
No flags = 0:0
Turn on F1 = <span style="color: #000000;">1</span>:<span style="color: #000000;">1</span>
Turn on F2 = <span style="color: #000000;">3</span>:<span style="color: #000000;">11</span>
Turn on F3 = <span style="color: #000000;">7</span>:<span style="color: #000000;">111</span>
Turn off F2 = <span style="color: #000000;">5</span>:<span style="color: #000000;">101</span>
Turn off F1 = <span style="color: #000000;">4</span>:<span style="color: #000000;">100</span></pre></div></div>

<p>All good so far, but now we need to see if a particular flag is switched on or off. I&#8217;ll publish that in my next blog post. So stay tuned.</p>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=245&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2009/12/02/bitwise-operators-used-for-flagging-items-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Emacs CSS-Mode Fix (adding to the chain!)</title>
		<link>http://www.robsearles.com/2009/10/29/emacs-css-mode-fix/</link>
		<comments>http://www.robsearles.com/2009/10/29/emacs-css-mode-fix/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 13:52:25 +0000</pubDate>
		<dc:creator>Rob Searles</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Emacs]]></category>

		<guid isPermaLink="false">http://www.robsearles.com/?p=202</guid>
		<description><![CDATA[After my week with Emacs I&#8217;m still struggling along. One of my main annoyances is the default way some of the modes indent the code. I think the worst offender of this is the default CSS-mode. After frustration when, yet again, I hit the tab key and the cursor rockets about 80 places to the [...]]]></description>
			<content:encoded><![CDATA[<p>After my <a href="http://www.robsearles.com/2009/10/19/a-week-with-emacs/" target="_blank">week with Emacs</a> I&#8217;m still struggling along. One of my main annoyances is the default way some of the modes indent the code. I think the worst offender of this is the default <a href="http://www.emacswiki.org/emacs/css-mode.el" target="_blank">CSS-mode</a>. After frustration when, yet again, I hit the tab key and the cursor rockets about 80 places to the right I decided to Google for a fix.</p>
<p><em>The internet is great!</em> Clearly it wasn&#8217;t just me having a problem, <a href="http://transcyberia.info/archives/50-fixing-emacs-css-mode.html" target="_blank">Guido Stevens</a> was also suffering and posted a fix, which he himself found on another <a href="http://www.chrisamiller.com/blog/2009/01/20/fix-css-mode-indention-in-emacs/" target="_blank">blog post by Chris Miller</a>, which Chris in turn <a href="http://www.stokebloke.com/wordpress/2008/03/21/css-mode-indent-buffer-fix/" target="_blank">found on StokeBloke.com</a> which had been found originally on a now dead post.</p>
<p>Not bad! Four people all having problems with the same thing and posting a fix for others to share. So I thought I&#8217;d join the chain, and write this blog post.</p>
<p>Oh yes, and the fix itself:</p>
<pre>;; fix css mode
(require 'css-mode)
(setq cssm-indent-level 2)
(setq cssm-newline-before-closing-bracket t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-mirror-mode t)</pre>
<img src="http://www.robsearles.com/?ak_action=api_record_view&id=202&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.robsearles.com/2009/10/29/emacs-css-mode-fix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
