<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://www.danhoey.com/</id>
  <title>Dan Hoey - coding it like it is</title>
  <updated>2011-10-03T13:00:00Z</updated>
  <link rel="alternate" href="http://www.danhoey.com/"/>
  <link rel="self" href="http://www.danhoey.com/blog/feed.xml"/>
  <author>
    <name>Daniel Hoey</name>
    <uri>www.danhoey.com</uri>
  </author>
  <entry>
    <id>tag:www.danhoey.com,2011-10-04:/blog/2011_10_04_spans_and_whitespace/</id>
    <title type="html">HTML spans and whitespace</title>
    <published>2011-10-03T13:00:00Z</published>
    <updated>2011-10-03T13:00:00Z</updated>
    <link rel="alternate" href="http://www.danhoey.com/blog/2011_10_04_spans_and_whitespace/"/>
    <content type="html">&lt;p&gt;Did you know that whitespace between elements is significant in HTML? I did not. Take this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;span&amp;gt;one&amp;lt;/span&amp;gt;
  &amp;lt;span&amp;gt;two&amp;lt;/span&amp;gt;
  &amp;lt;span&amp;gt;three&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is rendered as:&lt;/p&gt;

&lt;style&gt;
  #content span { background-color: #ddf; }
&lt;/style&gt;
&lt;div&gt;
  &lt;span&gt;one&lt;/span&gt;
  &lt;span&gt;two&lt;/span&gt;
  &lt;span&gt;three&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;But if you remove the line breaks&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;span&amp;gt;one&amp;lt;/span&amp;gt;&amp;lt;span&amp;gt;two&amp;lt;/span&amp;gt;&amp;lt;span&amp;gt;three&amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output is:&lt;/p&gt;

&lt;div&gt;
  &lt;span&gt;one&lt;/span&gt;&lt;span&gt;two&lt;/span&gt;&lt;span&gt;three&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;Looking at the page in firebug won’t give you any clues. It reformats the HTML so both examples appear to be exactly the same. Looking at the style or layout of any of the elements will also give you no idea where the spaces are coming from. The only way to introspect what is happening is to examine one of &lt;code&gt;textContent&lt;/code&gt;, &lt;code&gt;outerHTML|Text&lt;/code&gt; &lt;code&gt;innerHTML|Text&lt;/code&gt; properties of the parent element (the &lt;code&gt;div&lt;/code&gt; in this case) which will show the line breaks and spaces.&lt;/p&gt;

&lt;p&gt;If you are relying on the background color of your spans to make your UI look pretty (or bareable in my case), then be warned. Whitespace between spans matters.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:www.danhoey.com,2011-09-26:/blog/2011_09_23_building_a_blog_with_nanoc/</id>
    <title type="html">Building a blog with Nanoc</title>
    <published>2011-09-25T14:00:00Z</published>
    <updated>2011-09-25T14:00:00Z</updated>
    <link rel="alternate" href="http://www.danhoey.com/blog/2011_09_23_building_a_blog_with_nanoc/"/>
    <content type="html">&lt;p&gt;This blog is built using &lt;a href="http://nanoc.stoneship.org"&gt;Nanoc&lt;/a&gt;. Nanoc is a comprehensive tool for making static websites. It is written in Ruby and has a lot of flexibility. I used &lt;a href="http://starrhorne.com/posts/howto_build_a_blog_with_nanoc/"&gt;Howto build a blog with nanoc&lt;/a&gt; to get started with my blog. However this article is a few years old now and some of the instructions didn’t work for me. So here is my tutorial on building a blog with Nanoc.&lt;/p&gt;

&lt;p class="notes"&gt;Notes:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;I am using nanoc version 3.1.9.&lt;/li&gt;
  &lt;li&gt;Nanoc provides a command line tool for creating new items (pages, stylesheets etc) and layouts. I don’t use them, I just make the file using my editor (vim).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id="getting-started"&gt;Getting Started&lt;/h1&gt;
&lt;p&gt;Install nanoc and create a site: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install nanoc
nanoc create_site my_blog
cd my_blog
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Have a look at the file structure that nanoc has created. The most important directories are &lt;code&gt;content&lt;/code&gt; which defines all the items for your site (pages, stylesheets etc) and &lt;code&gt;layout&lt;/code&gt; which has templates that are shared across items. 
Start nanoc auto-compile server: &lt;code&gt;nanoc aco &amp;amp;&lt;/code&gt; and point your browser to &lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt;. You should see the default nanoc welcome screen.&lt;/p&gt;

&lt;h1 id="making-our-first-blog-post"&gt;Making our first blog post&lt;/h1&gt;
&lt;p&gt;We’ll make a new file &lt;code&gt;content/blog/first_post.md&lt;/code&gt; which will contain our post content and meta data&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir content/blog
&lt;/code&gt;&lt;/pre&gt;
&lt;p class="file_path"&gt;content/blog/first_post.md&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;---
kind: article
created_at: 1 January 1970
title: First Post
---
Hello World!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All blog posts should have &lt;code&gt;kind: article&lt;/code&gt; and &lt;code&gt;created_at: &amp;lt;timestamp&amp;gt;&lt;/code&gt;. The timestamp can be any text that is parseable by &lt;code&gt;Time.parse&lt;/code&gt;. This meta data is used later on when we make an landing page for our blog.&lt;/p&gt;

&lt;p&gt;Go to &lt;a href="http://localhost:3000/blog/first_post/index.html"&gt;http://localhost:3000/blog/first_post/index.html&lt;/a&gt;. You should see your post with the default nanoc layout. The layout is specified in &lt;code&gt;layout/default.html&lt;/code&gt;, you should modify this file to suit.&lt;/p&gt;

&lt;h1 id="adding-post-meta-data"&gt;Adding post meta data&lt;/h1&gt;
&lt;p&gt;To show your post title and date you need to include some ruby code just above &lt;code&gt;&amp;lt;%= yield %&amp;gt;&lt;/code&gt; in the default layout:&lt;/p&gt;

&lt;p class="file_path"&gt;layout/default.html&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; ...
 &amp;lt;h1&amp;gt;&amp;lt;%= link_to(@item[:title], @item.path) %&amp;gt;&amp;lt;/h1&amp;gt;
 &amp;lt;p class="created_at"&amp;gt;&amp;lt;%= @item[:created_at] %&amp;gt;&amp;lt;/p&amp;gt;
 &amp;lt;%= yield %&amp;gt;
 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For &lt;code&gt;link_to&lt;/code&gt; to work we need to load an extra nanoc helper:&lt;/p&gt;

&lt;p class="file_path"&gt;lib/helper.rb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;include Nanoc3::Helpers::LinkTo
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Reload the page in your browser. You should see the title and date included in your post.&lt;/p&gt;

&lt;h1 id="making-a-landing-page-for-your-blog"&gt;Making a landing page for your blog&lt;/h1&gt;
&lt;p&gt;To make the landing page for the blog we are going to need to use some more advanced features of Nanoc. First of all we will want to share the common layout between the landing page and the individual posts. To do this make a new layout that is specific to posts:&lt;/p&gt;

&lt;p class="file_path"&gt;layout/post.html.erb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; &amp;lt;h1&amp;gt;&amp;lt;%= link_to(@item[:title], @item.path) %&amp;gt;&amp;lt;/h1&amp;gt;
 &amp;lt;p class="created_at"&amp;gt;&amp;lt;%= @item[:created_at] %&amp;gt;&amp;lt;/p&amp;gt;
 &amp;lt;%= yield %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remove the post specific code from &lt;code&gt;layout/default.html&lt;/code&gt; and modify the &lt;code&gt;Rules&lt;/code&gt; file so that posts use this new layout.&lt;/p&gt;

&lt;p class="file_path"&gt;Rules&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;...
compile '/blog/*/' do
  layout 'post'
  layout 'default'
end

compile '*' do
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is vital that that &lt;code&gt;compile '/blog/*/' do&lt;/code&gt; is placed before &lt;code&gt;compile '*' do&lt;/code&gt; as Nanoc only uses the first compile rule that matches an item. Check that you post is still rendering correctly. &lt;/p&gt;

&lt;p&gt;Next we will need to load the blogging helper and use the &lt;code&gt;sorted_articles&lt;/code&gt; method to render links to the articles sorted by &lt;code&gt;created_at&lt;/code&gt; date.&lt;/p&gt;

&lt;p class="file_path"&gt;lib/helper.rb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;...
include Nanoc3::Helpers::Blogging
&lt;/code&gt;&lt;/pre&gt;

&lt;p class="file_path"&gt;content/blog.html.erb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;% (@site.sorted_articles[0..4]).each do |post| %&amp;gt;
  &amp;lt;%= link_to(post[:title], post.path) %&amp;gt;
  &amp;lt;%= post[:created_at] %&amp;gt; 
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Go to &lt;a href="http://localhost:3000/blog/index.html"&gt;http://localhost:3000/blog/index.html&lt;/a&gt;. You should the title and date of your post. Click the title link should take you to the body of the post.&lt;/p&gt;

&lt;h1 id="show-the-latest-article-in-full-on-landing-page"&gt;Show the latest article in full on landing page&lt;/h1&gt;
&lt;p&gt;Lets improve this page to show the first article in full. We need to have access to the body of the first post, without the default layout. We can get this by modifying the blog post compile rule to take a snapshot:&lt;/p&gt;

&lt;p class="file_path"&gt;Rules&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;...
compile '/blog/*/' do
  layout 'post'
  snapshot :body
  layout 'default'
end
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p class="file_path"&gt;content/blog.html.erb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;%= articles = @site.sorted_articles %&amp;gt;
&amp;lt;%= articles.first.compiled_content(:snapshot =&amp;gt; :body) %&amp;gt;
&amp;lt;% (articles[1..4] || []).each do |post| %&amp;gt;
  &amp;lt;%= link_to(post[:title], post.path) %&amp;gt;
  &amp;lt;%= post[:created_at] %&amp;gt; 
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;:body&lt;/code&gt; snapshot is the content of the post after the ‘post’ layout has been applied. We render this snapshot in &lt;code&gt;content/blog.html.erb&lt;/code&gt;&lt;/p&gt;

&lt;h1 id="add-a-feed"&gt;Add a feed&lt;/h1&gt;
&lt;p class="file_path"&gt;content/blog/feed.erb&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;%= atom_feed :title =&amp;gt; 'Nanoc test blog', :author_name =&amp;gt; '&amp;lt;your name&amp;gt;', 
              :author_uri =&amp;gt; '&amp;lt;your home page&amp;gt;', :limit =&amp;gt; 10 %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;atom_feed&lt;/code&gt; relies on the base_url configuration paramter which is set in &lt;code&gt;config.yml&lt;/code&gt;:&lt;/p&gt;

&lt;p class="file_path"&gt;config.yml&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;base_url: '&amp;lt;your site uri&amp;gt;'
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lastly we need compile and routing rules for the feed.  Again the order is important, ensure &lt;code&gt;compile 'blog/feed' do&lt;/code&gt; is before &lt;code&gt;compile 'blog/*/' do&lt;/code&gt;&lt;/p&gt;

&lt;p class="file_path"&gt;Rules&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;...
compile 'blog/feed' do
  filter :erb
end

route 'blog/feed' do
  '/blog/feed.xml'
end

compile 'blog/*/' do
...
&lt;/code&gt;&lt;/pre&gt;

&lt;h1 id="conclusion"&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;In conclusion we have made an awesome blog. &lt;/p&gt;

&lt;p&gt;However if you are not yet entirely satisfied can I recommend that you:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;improve the style (look in &lt;code&gt;content/stylesheets&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;add comments via &lt;a href="http://disqus.com/"&gt;Disqus&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;add search via &lt;a href="http://www.google.com/sitesearch/"&gt;Google sitesearch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;add tags via &lt;a href="http://nanoc.stoneship.org/docs/api/3.1/Nanoc3/Helpers/Tagging.html"&gt;Nanoc tagging&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Go nuts.&lt;/strong&gt;&lt;/p&gt;
</content>
  </entry>
</feed>


