tags:

views:

41

answers:

1

I have the following rake file to create a static version of my sinatra app, stolen from http://github.com/semanticart/stuff-site/blob/master/Rakefile

class View
  attr_reader :permalink
  def initialize(path)
    filename = File.basename(path)
    @permalink = filename[0..-6]
  end
end

view_paths = Dir.glob(File.join(File.dirname(__FILE__), 'views/pages', '*.haml'))
ALL_VIEWS = view_paths.map {|path| View.new(path) }

task :build do
  def dump_request_to_file url, file
    Dir.mkdir(File.dirname(file)) unless File.directory?(File.dirname(file))
    File.open(file, 'w'){|f| f.print @request.get(url).body}
  end

  static_dir = File.join(File.dirname(__FILE__), 'public')

  require 'sinatra'
  require 'c4eo'
  @request = Rack::MockRequest.new(Sinatra::Application)

  ALL_VIEWS.each do |view|
    puts view
    dump_request_to_file("/#{view.permalink}", File.join(static_dir, view.permalink+'.html'))
  end
end

ALL_VIEWS is now an array containing all the Haml files in the root of my 'views/pages' directory.

How do I modify ALL_VIEWS and the dump_request_to_file method to cycle through all the subdirectories in my views/pages directory?

My views directory looks a bit like this: http://i45.tinypic.com/167unpw.gif

If it makes life a lot easier, I could have all my files named index.haml, inside directories.

Thanks

+1  A: 

To cycle through all subdirs, change 'views/pages' to 'views/pages/**'

The double splats tells it to search recursively, you can see it in the docs at http://ruby-doc.org/core/classes/Dir.html#M002322


Note that I haven't looked thoroughly at your use case, but preliminarily it appears that you may have trouble generating a permalink. When I checked the results, I got:

[#<View:0x1010440a0 @permalink="hound">,
 #<View:0x101044078 @permalink="index">,
 #<View:0x101044000 @permalink="hound">,
 #<View:0x101043f88 @permalink="index">,
 #<View:0x101043f10 @permalink="references">,
 #<View:0x101043e98 @permalink="do_find">,
 #<View:0x101043e20 @permalink="index">,
 #<View:0x101043da8 @permalink="README">]

Which were generated from these files:

["/Users/josh/deleteme/fileutilstest/views/pages/bar/cheese/rodeo/hound.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/bar/cheese/rodeo/outrageous/index.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/hound.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/index.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/bar/pizza/references.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/do_find.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/tutorials/index.haml",
 "/Users/josh/deleteme/fileutilstest/views/pages/tutorials/README.haml"]

And it looks like you create the link with: File.join(static_dir, view.permalink+'.html')

So you can see that in this case, that would create three files like static_dir/index.html

A fairly obvious solution is to include the relative portion of the link, so it would become

static_dir/bar/cheese/rodeo/outrageous/index.html
static_dir/bar/pizza/index.html
static_dir/tutorials/index.html

EDIT: In regards to addressing how to find the relative url, this seems to work

class View
  attr_reader :permalink
  def initialize( root_path , path )
    root_path = File.expand_path(root_path).sub(/\/?$/,'/')
    path      = File.expand_path path
    filename  = path.gsub root_path , String.new
    raise "#{path} does not appear to be a subdir of #{root_path}" unless root_path + filename == path
    @permalink = filename[0..-6]
  end
end


view_paths = Dir.glob(File.join(File.dirname(__FILE__), 'views/pages/**', '*.haml'))
ALL_VIEWS = view_paths.map { |path| View.new 'views/pages' , path }

require 'pp'
pp ALL_VIEWS

I'm not all that keen on the [0..-6] thing, it only works if you know your file has a suffix and that it is five characters long. But I'm going to leave it alone since I don't really know how you would want to handle the different future situations I might anticipate (ie generate an html from the haml and serve that up, now you have two files index.html and index.haml, which, after you remove their extensions, are both just index. Or styles.css which loses part of its filename when you attempt to remove its extension by pulling in [0..-6]

Joshua Cheek
Thanks for this, any ideas how I could produce the relative portion of the link?
Dr. Frankenstein
Added an example of how to address the relative path
Joshua Cheek
Thanks for taking the time, that is a great addition.
Dr. Frankenstein