tags:

views:

21

answers:

2

Let's say I scrapped my old wordpress installation and I put the following directories in /public (sinatra application)

2006        2008        2010        category    my-work
2007        2009        archives    tag

The year directories go down in this format:

/year/month/day/title-of-post/

Problem 1: I can get /year/month/day/title-of-post/index.html to load a page but I want to be able to load /year/month/day/title-of-post/ to load a page without having to type index.html at the end.

Problem 2:

I want to be able to type /archive and get a <li><a href="path-to-archived-post">post title</a></li> of every subdirectory mentioned above. How do I do this?

A: 

1.

get "/:year/:month/:day/:title" do
  # render the post
end

2.

get "/archive" do
  years = Dir.entries(PUBLIC_DIR)
  # cycle through years and render links
end
Dmitry Maksimov
+1  A: 

You have a few options for getting index.html to work in your public folders. The fastest option will be to have your load balancing server (e.g. Nginx or Apache) serve the static files. Nginx and Apache basically do this out of the box.

Another way is to manually read and send the files from a Sinatra route. You’ll get more control this way, but lose a little speed and use more memory. You might do that like this:

get "/:year/:month/:day/:title" do |*path|
  File.read(File.join(settings.public, path, "index.html"))
end

You can get a list of your posts by globbing with the Dir class:

get "/archive" do
  @posts = Dir["#{settings.public}/**/index.html"].map do |post|
    File.dirname(post).sub(settings.public, "")
  end

  haml :archive
end
Todd Yandell
You rock thank you.
DFischer
I was wondering if you potentially knew a solution to remove all items that would return "/2009/06" (basically empty directories that just point another level down for the post).
DFischer
It should already ignore empty directories (or at least dirs that don’t include an index.html file). If you want to exclude specific directories, even if they contain index.html files, you could throw in a reject block and reject any paths that start with a certain string: `@posts.reject! { |d| d =~ /^\/2009\/06/ }`.
Todd Yandell