Permalink
Browse files

README, bin file working, `before` controller method accepts multiple…

… parameters, Stasis::Auto for auto-regeneration, `instead` method for `before` filters, specs
  • Loading branch information...
1 parent 0292263 commit 25ea4e28683fd0bb5e1b50dbfc6ee43aecc5e95b @winton committed Jun 5, 2011
View
@@ -1,19 +1,15 @@
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
-------
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,24 +65,24 @@ 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'
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
+ stasis -a
View
@@ -2,4 +2,23 @@
require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis")
-Stasis.new(Dir.pwd).generate
+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
View
@@ -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
View
@@ -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
View
@@ -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'
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
+priority(
+ '/before_render_partial.html.haml' => 1,
+ 'index.html.haml' => -1
+)

0 comments on commit 25ea4e2

Please sign in to comment.