Skip to content

Instantly share code, notes, and snippets.

@sc0ttman
Created June 23, 2011 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttman/1041602 to your computer and use it in GitHub Desktop.
Save sc0ttman/1041602 to your computer and use it in GitHub Desktop.
Export static pages
namespace :static do
desc 'Generate static html files of all views in the pages folder'
#usage: rake static:generate
task :generate do
# note: add these gems to your gemfile:
# gem "nokogiri"
# gem "rest-open-uri","1.0.0"
puts "generating..."
views = Dir.new("#{::Rails.root.to_s}/app/views/pages").entries
views.each do |view|
url = view.gsub(".html.erb","")
unless (url == "." || url == ".." || url[0,1]=="_")
puts url
File.open("#{::Rails.root.to_s}/public/static_files/#{url}.html", "w") { |f| f.write Nokogiri::HTML(open("http://localhost:3000/#{url}")) }
end
end
end
end
@sc0ttman
Copy link
Author

Basically, I needed a way to generate static HTML files from the pages controller. I couldn't get

`````` ActionDispatch::Integration::Session.new(Rails.application)stuff to work, so I just used nokogiri on my development server. Also, the only route I had was: match ':action' => 'pages#:action.html' ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment