diff --git a/README.md b/README.md index 402432c..16346bf 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ Stasis ====== -Static sites made powerful. - Stasis is not your typical static site generator. Access your database. Pull data from an API. Render to any number of dynamic paths. Get crazy. Install @@ -10,10 +8,8 @@ Install gem install stasis -One-to-One Example ------------------- - -Example project: +Example Project +--------------- project/ index.html.haml @@ -60,7 +56,7 @@ Before Filters In your controller: before 'index.html.haml' do - # any class variables set here will be available to your template + # class variables created here will be available to your template @something = true end @@ -69,16 +65,16 @@ The `before` method can take any number of paths and/or regular expressions. Layouts ------- -Create a `layout.html.haml` file: +Create a `layout.html.haml` template: %html %body= yield -Set the default layout: +In your controller, set the default layout: layout 'layout.html.haml' -Set the layout for a particular file: +Set the layout for a particular template: layout 'index.html.haml' => 'layout.html.haml' @@ -86,7 +82,7 @@ Or use a regular expression: layout /.*.html.haml/ => 'layout.html.haml' -Set the layout from a before filter if you like: +Set the layout from a `before` filter if you like: before 'index.html.haml' do layout 'layout.html.haml' @@ -95,9 +91,9 @@ Set the layout from a before filter if you like: Ignore ------ -Use the `ignore` method in your controller to ignore certain files. +Use the `ignore` method in your controller to ignore certain paths. -For example, to ignore files with an underscore at the beginning (partials): +For example, to ignore paths with an underscore at the beginning (partials): ignore /_.*/ @@ -130,7 +126,7 @@ Render with a block for the template to `yield` to: Instead ------- -The `instead` method changes the output of the file being rendered: +The `instead` method changes the output of the template being rendered: before 'index.html.haml' do instead render('subdirectory/index.html.haml') @@ -139,7 +135,7 @@ The `instead` method changes the output of the file being rendered: Helpers ------- -The `helpers` method allows you to make methods available to `before` callbacks and templates: +To make methods available to `before` callbacks and templates, add a `helpers` block to your controller: helpers do def say_hello @@ -181,9 +177,9 @@ Stasis uses [Tilt](https://github.com/rtomayko/tilt) to support the following te CoffeeScript .coffee coffee-script (+node coffee) Slim .slim slim (>= 0.7) -Continuous Rendering --------------------- +Automatic Regeneration +---------------------- -To continuously render files as you change them, run: +To continuously regenerate your project as you modify files, run: - stasis -c \ No newline at end of file + stasis -a \ No newline at end of file diff --git a/bin/stasis b/bin/stasis index 59572ed..3e26553 100755 --- a/bin/stasis +++ b/bin/stasis @@ -2,4 +2,23 @@ require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis") -Stasis.new(Dir.pwd).generate \ No newline at end of file +Stasis::Gems.activate %w(slop) +require 'slop' + +opts = Slop.parse :help => true do + on :a, :auto, 'Auto-regenerate' + on :o, :only, 'Only generate specific files (comma-separated)', :as => Array +end + +args = [] +opts.parse { |arg| args << arg } + +dir = args.first || Dir.pwd + +if opts.auto? + Stasis::Auto.new(dir) +elsif opts.only? + Stasis.new(dir).generate(:only => opts[:only]) +else + Stasis.new(dir).generate +end \ No newline at end of file diff --git a/config/gemsets.yml b/config/gemsets.yml index 4ed5bcc..2026dc1 100644 --- a/config/gemsets.yml +++ b/config/gemsets.yml @@ -1,5 +1,7 @@ stasis: + directory_watcher: ~>1.4.0 rake: >=0.8.7 rocco: ~>0.6 rspec: ~>1.0 + slop: ~>1.6.0 tilt: ~>1.2 \ No newline at end of file diff --git a/config/gemspec.yml b/config/gemspec.yml index fab5ea7..7350c24 100644 --- a/config/gemspec.yml +++ b/config/gemspec.yml @@ -7,6 +7,8 @@ homepage: http://wintoni.us summary: Markup-agnostic static site generator description: A markup-agnostic static site generator. dependencies: + - directory_watcher + - slop - tilt development_dependencies: - rake diff --git a/lib/stasis.rb b/lib/stasis.rb index ae3fc1d..82e2915 100644 --- a/lib/stasis.rb +++ b/lib/stasis.rb @@ -31,16 +31,17 @@ # Require all Stasis library files. +require 'stasis/auto' require 'stasis/plugin' require 'stasis/scope' require 'stasis/scope/action' require 'stasis/scope/controller' -require 'stasis/plugins/after' require 'stasis/plugins/before' require 'stasis/plugins/helpers' require 'stasis/plugins/ignore' +require 'stasis/plugins/instead' require 'stasis/plugins/layout' require 'stasis/plugins/priority' require 'stasis/plugins/render' diff --git a/lib/stasis/auto.rb b/lib/stasis/auto.rb new file mode 100644 index 0000000..a45e72f --- /dev/null +++ b/lib/stasis/auto.rb @@ -0,0 +1,52 @@ +Stasis::Gems.activate %w(directory_watcher) +require 'directory_watcher' + +class Stasis + class Auto + + def initialize(dir) + trap("INT") { exit } + + puts "\nAuto-regenerating enabled: #{dir}" + + @stasis = Stasis.new(dir) + generate + + dw = DirectoryWatcher.new(@stasis.root) + dw.interval = 1 + + Dir.chdir(@stasis.root) do + within_public = @stasis.destination[0..@stasis.root.length-1] == @stasis.root + rel_public = @stasis.destination[@stasis.root.length+1..-1] rescue nil + dw.glob = Dir["*"].inject(["*"]) do |array, path| + if File.directory?(path) && (!within_public || path != rel_public) + array << "#{path}/**/*" + end + array + end + end + + dw.add_observer do |*events| + modified = events.detect { |e| e[:type] == :modified } + generate if modified + end + + dw.start + loop { sleep 1000 } + end + + private + + def generate + puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Auto-regenerating project..." + begin + @stasis.generate + rescue Exception => e + puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Error: #{e.message}`" + puts "\t#{e.backtrace.join("\n\t")}" + else + puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Complete" + end + end + end +end \ No newline at end of file diff --git a/lib/stasis/plugins/before.rb b/lib/stasis/plugins/before.rb index a6c363b..1a20bb3 100644 --- a/lib/stasis/plugins/before.rb +++ b/lib/stasis/plugins/before.rb @@ -12,11 +12,14 @@ def initialize # This method is bound to all controllers. Stores a block in the `@blocks` `Hash`, # where the key is a path and the value is an `Array` of blocks. - def before(controller, path=nil, &block) + def before(controller, *paths, &block) + paths = [ nil ] if paths.empty? if block - path = controller._resolve(path, true) - @blocks[path] ||= [] - @blocks[path] << block + paths.each do |path| + path = controller._resolve(path, true) + @blocks[path] ||= [] + @blocks[path] << block + end else @blocks[path] || [] end @@ -38,22 +41,11 @@ def before_render(controller, action, path) if @blocks && matches = _match_key?(@blocks, path) action._[:path] = path matches.flatten.each do |block| - capture_render(action) do - action.instance_eval(&block) - end + action.instance_eval(&block) end action._[:path] = nil end [ controller, action, path ] end - - # Lets the `action` know that we want to capture any render that occurs within the - # `block`. - def capture_render(action, &block) - old = action._[:capture_render] - action._[:capture_render] = true - yield - action._[:capture_render] = old - end end end \ No newline at end of file diff --git a/lib/stasis/plugins/instead.rb b/lib/stasis/plugins/instead.rb new file mode 100644 index 0000000..58f023f --- /dev/null +++ b/lib/stasis/plugins/instead.rb @@ -0,0 +1,11 @@ +class Stasis + class Instead < Plugin + + action_method :instead + + # This method is bound to all actions. + def instead(action, string) + action._[:render] = string + end + end +end \ No newline at end of file diff --git a/lib/stasis/plugins/render.rb b/lib/stasis/plugins/render.rb index 752cfc8..15d11b9 100644 --- a/lib/stasis/plugins/render.rb +++ b/lib/stasis/plugins/render.rb @@ -50,10 +50,6 @@ def render(action, path_or_options={}, options={}, &block) output end - if action._[:capture_render] - action._[:render] = output - end - output end end diff --git a/spec/fixtures/project/controller.rb b/spec/fixtures/project/controller.rb index 58f1b34..03f824e 100644 --- a/spec/fixtures/project/controller.rb +++ b/spec/fixtures/project/controller.rb @@ -28,15 +28,15 @@ end before 'before_render_text.html.haml' do - render :text => 'root' + instead render(:text => 'root') end before 'before_render_partial.html.haml' do - render :path => '_partial.html.haml' + instead render(:path => '_partial.html.haml') end before 'before_non_existent.html' do - render :path => '_partial.html.haml' + instead render(:path => '_partial.html.haml') end # Helpers diff --git a/spec/fixtures/project/subdirectory/controller.rb b/spec/fixtures/project/subdirectory/controller.rb index fbcea5e..a5aca27 100644 --- a/spec/fixtures/project/subdirectory/controller.rb +++ b/spec/fixtures/project/subdirectory/controller.rb @@ -24,15 +24,11 @@ end before 'before_render_text.html.haml' do - render :text => 'subdirectory' + instead render(:text => 'subdirectory') end -before 'before_render_partial.html.haml' do - render :path => '_partial.html.haml' -end - -before 'before_non_existent.html' do - render :path => '_partial.html.haml' +before 'before_render_partial.html.haml', 'before_non_existent.html' do + instead render(:path => '_partial.html.haml') end # Helpers @@ -49,8 +45,10 @@ def helper # Layout -layout 'layout_controller.html.haml' => 'layout.html.haml' -layout 'layout_controller_from_root.html.haml' => '/layout.html.haml' +layout( + 'layout_controller.html.haml' => 'layout.html.haml', + 'layout_controller_from_root.html.haml' => '/layout.html.haml' +) before 'layout_action.html.haml' do layout 'layout.html.haml' @@ -62,5 +60,7 @@ def helper # Priority -priority '/before_render_partial.html.haml' => 1 -priority 'index.html.haml' => -1 \ No newline at end of file +priority( + '/before_render_partial.html.haml' => 1, + 'index.html.haml' => -1 +) \ No newline at end of file