Daniel Fischer
Daniel Fischer

Reputation: 3062

How to render archive-like structure in sinatra of old wordpress posts?

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?

Upvotes: 0

Views: 134

Answers (2)

Todd Yandell
Todd Yandell

Reputation: 14696

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

Upvotes: 1

Dmitry Maksimov
Dmitry Maksimov

Reputation: 2861

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

Upvotes: 0

Related Questions