Friday, March 25, 2011

Nanoc and Multiple Layout Templates

A while back we took a quick look at nanoc for generating static web sites. I've been using it a bit both experimenting and for a simple web site for an author friend of mine. I'm going to do a few short articles on using it, documenting what I've learned in much the same way I've tried to do with Ramaze.

Let's start with some simple code that uses multiple layout templates. You have this often with web sites where there's a home page that uses a different layout than the rest of the pages of the site. Let's start with creating a new site. We do that the same way we did last time with ...

nanoc create_site multiple_templates

and let's add a couple of pages to go along with them ...

nanoc create_item page1
nanoc create_item page2

We didn't really talk about the Rules in the last post and I'm only going to touch on them now. You should take a few minutes and read a bit about them here. We'll discuss them a bit more as we go through future posts.

Now, we need to modify the Rules file to tell it to use an alternate layout for the content/page[12].html files. I'm not going to show the entire file, just the compile rule for these ...


compile '/page*/' do
filter :erb
layout 'page'
end


What this rule says is that when we "compile" files in our content/ directory, we should first run them through the erb (embedded ruby) and then use the page template in the layouts directory.

Here's the default which will get use by everything else (essentially our main page at content/index.html).


compile '*' do
filter :erb
layout 'default'
end


This along with the code for all of this up on github should be enough to get you started. As always though, let me know if you have questions or comments.

Tuesday, March 1, 2011

Inkscape

This one is going to be short since I'm not a designer. I've been reading "Web Design for Developers" by Brian P. Hogan and he recommends using Illustrator for parts of his book. Since the cost was a bit umm... prohibitive for learning purposes, I started looking around for a suitable alternative. What I found was Inkscape. This is a nice little program for generating SVG (Scalable Vector Graphics) files. You can use it for generating files for say logos or banners on websites. While I won't say it's simple to use (and this probably says more about me than the program), with the tutorials available, you shouldn't have too much problem getting it to work. Additionally, there's a book on it that's reviewed on Slashdot. I haven't read the book yet but do have it on order and if it seems worthwhile, I'll post here about it.

To install ...

sudo apt-get install inkscape

and you should have an inkscape selection Applications/Graphics or at least on Ubuntu.

Once again, I'm not sure how much I'll be able to help, but if you have questions, be sure and let me know.

Friday, January 28, 2011

Nanoc and Static Web Sites

A few days ago we looked at Webby for generating static web sites. I had a couple of issues with it, including the fact that it didn't work with ruby 1.9.2 and it hasn't been updated for a couple of years now. Someone on the Ramaze email list suggested that I should take a look at nanoc too. It seems to have many of the same features as webby, but is actively maintained and runs on ruby 1.9.2. So let's take a look at getting it up, running, and generating a site. First, we'll need to add a few gems ...


gem install nanoc
gem install kramdown
gem install adsf


Here we're installing nanoc itself, kramdown, which is a Markdown converter, and adsf, which is used as a web server for showing the site.

With that out of the way, let's go ahead and create a new site ...

nanoc create_site my_site

This should create a new site in the directory my_site that has a few different files and directories. Go ahead and cd into the directory and then

nanoc compile

which should "compile" the site. In this case, compile will create an output directory where the resulting HTML files will reside. OK, now we can start a web server with

nanoc view

and then point our browser at http://localhost:3000. At this point you should see the default site for nanoc. For viewing, you could also use

nanoc autocompile

which will also compile a file when changes are made to the site automatically. For "real" work, this is probably the option to use.

OK, let's take a look at the content directory. This is where nanoc will keep the files that will get compiled and moved to the output directory. Go ahead and open up content/index.html in your favorite text editor and make a few changes. Go back to the browser, reload, and you should see your changes there. Now, take a look at layout/default.html. If you make a change in there, say adding an h1 tag before the h2 Documentation tag, it will appear in all the pages that are generated.

Finally, we're going to need to add new pages if this is going to be useful. Try this ...

nanoc create_item test

This will create a test.htnml in the content directory and when it gets compiled, you should be able to go to http://localhost:3000/test/ (don't forget the final "/") and see the page. A couple of things to note here. In the output directory, this will be output/test/index.html.

This should be enough to get you started. Definitely check out the nanoc site for more information. I'm going to use this to generate a personal site, so I'll let you know how it goes and what good/bad things I see.

Let me know if you have questions or comments.

Tuesday, January 25, 2011

Ruby and Rational Numbers

Here's a problem over on Programming Praxis that for whatever reason I wasn't having much luck posting there. Their loss, your gain ;-). Here's the code ...


class Fraction

attr_reader :n, :d

def initialize(n, d)
raise "Can't have zero denominator." if d == 0

n, d = -n, -d if d < 0

g = n.gcd(d)
if g == 1
@n = n
@d = d
else
@n = n / g
@d = n / g
end
end

def plus(f)
Fraction.new((@n*f.d)+(@d*f.n), @d*f.d)
end

def minus(f)
Fraction.new((@n*f.d)-(@d*f.n), @d*f.d)
end

def times(f)
Fraction.new(@n*f.n, @d*f.d)
end

def divide(f)
Fraction.new(@n*f.d, @d*f.n)
end

def to_s
"#{@n}/#{@d}"
end
end

f1 = Fraction.new(1, 3)
f2 = Fraction.new(-1, 7)
puts "#{f1} + #{f2} = #{f1.plus(f2)}"
puts "#{f1} + #{f2} = #{f1.minus(f2)}"
puts "#{f1} + #{f2} = #{f1.times(f2)}"
puts "#{f1} + #{f2} = #{f1.divide(f2)}"


Nothing too awfully complex here, but if you have questions, let me know.

Monday, January 24, 2011

Webby and Static Web Sites

I was trying to figure out recently how to build a static site with Ramaze and didn't really come up with anything, but someone pointed me in the direction of Webby that's designed with just this use case in mind. It seems pretty cool, so I thought I'd document how to get it up and running on Ubuntu.

First I tried just doing a gem install, but that didn't work because I'm using ruby 1.9.2 so ...

rvm install 1.8.7

to get the right ruby (assuming you're using rvm to manage this). Then, you'll need to set the correct ruby with

rvm 1.8.7

to add the following gems for ruby 1.8.7


gem install webby
gem install RedCloth


The RedCloth gem is needed for the default, but if you decide to use a different templating system, you don't need to install it.

OK, let's create a web site now.

webby-gen website my_site

There should now be a directory called my_site with a few different directories. Now you can

cd my_site
webby


and this should create a new directory called output with some HTML files and the css for the site. If you modify the file index.txt in the content directory, and then rerun the webby command, you should see the output/index.html change.

Since you've been reading this blog, you probably have a pretty good idea of how templating works, so there really shouldn't be too much here that you haven't seen in general even if you haven't looked at the RedCloth templating. webby also supports a number of other templating systems that you can use by setting a filter in the content file. This is documented in the User Manual.

To be honest, I just started looking at this today, so I'm not sure how much help I'm going to be with questions, but let me know if you have any and I'll give them a shot.

Tuesday, November 30, 2010

Ruby 1.9.2 and Ramaze

OK, I just found out something interesting about ruby 1.9.2. They've changed the default LOAD_PATH to not include "." (current directory). What's this actually mean? Well, for starters just about every example that I and most others have written will no longer work. Here's an example with something from the current Ramaze prototype (generated when you do a ramaze create foo

require 'model/user'

this will generate errors if you run it with ruby 1.9.2. What you'll need to do instead is either

__DIR__('user')

or

require_relative 'user'

The first version is compatible between 1.9 and 1.8 and the 2nd is 1.9 only. Both examples assume that the file this code is in is already in the model directory.

I should also note that Lee Jarvis has fixed the problem with the prototype already and it should be in the next update of Ramaze.

Like I said, most of my examples from previous posts are now wrong, but they should be pretty easy to fix once you know how. If you have any problems, be sure to let me know and I'll try to help get them worked out with you.

Wednesday, November 24, 2010

Ruby Book - $10

This is a pretty good deal for the latest Ruby pickaxe book from Dave Thomas and the Pragmatic Programmer group. It's $10 each for the paper or electronic version of the book. Head over here to grab it.