Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:18:53+00:00 2026-05-15T14:18:53+00:00

I have the following rake file to create a static version of my sinatra

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:18:54+00:00Added an answer on May 15, 2026 at 2:18 pm

    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]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Rake file. Using RoR 2.3.8. desc Create shops sitemap task(:shops
I have an app that has the following in the routes file: namespace admin
I have the following seeds.rb file: initial_users = User.create( [ { :first_name => Joe,
I have everything installed. But when I run rake db:create, I get the following:
I have a project with the following Gemfile.lock file http://pastie.org/private/tfbnteruyahmwfhtt26ea , and when I
Rails 3.1 I have a heroku app made with the following command heroku create
I have a simple rake task importing records from a CSV file and saving
So I have a rake file like the following: require 'fileutils' task :copy do
I have the following gems defined in my environment.rb file: config.gem authlogic config.gem paperclip
I have the following in my routes file: resources :timelogs do member do post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.