Skip to content

Commit

Permalink
README, bin file working, before controller method accepts multiple…
Browse files Browse the repository at this point in the history
… parameters, Stasis::Auto for auto-regeneration, `instead` method for `before` filters, specs
  • Loading branch information
winton committed Jun 5, 2011
1 parent 0292263 commit 25ea4e2
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 55 deletions.
34 changes: 15 additions & 19 deletions README.md
@@ -1,19 +1,15 @@
Stasis 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. 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 Install
------- -------


gem install stasis gem install stasis


One-to-One Example Example Project
------------------ ---------------

Example project:


project/ project/
index.html.haml index.html.haml
Expand Down Expand Up @@ -60,7 +56,7 @@ Before Filters
In your controller: In your controller:


before 'index.html.haml' do 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 @something = true
end end


Expand All @@ -69,24 +65,24 @@ The `before` method can take any number of paths and/or regular expressions.
Layouts Layouts
------- -------


Create a `layout.html.haml` file: Create a `layout.html.haml` template:


%html %html
%body= yield %body= yield


Set the default layout: In your controller, set the default layout:


layout 'layout.html.haml' 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' layout 'index.html.haml' => 'layout.html.haml'


Or use a regular expression: Or use a regular expression:


layout /.*.html.haml/ => 'layout.html.haml' 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 before 'index.html.haml' do
layout 'layout.html.haml' layout 'layout.html.haml'
Expand All @@ -95,9 +91,9 @@ Set the layout from a before filter if you like:
Ignore 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 /_.*/ ignore /_.*/


Expand Down Expand Up @@ -130,7 +126,7 @@ Render with a block for the template to `yield` to:
Instead 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 before 'index.html.haml' do
instead render('subdirectory/index.html.haml') instead render('subdirectory/index.html.haml')
Expand All @@ -139,7 +135,7 @@ The `instead` method changes the output of the file being rendered:
Helpers 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 helpers do
def say_hello def say_hello
Expand Down Expand Up @@ -181,9 +177,9 @@ Stasis uses [Tilt](https://github.com/rtomayko/tilt) to support the following te
CoffeeScript .coffee coffee-script (+node coffee) CoffeeScript .coffee coffee-script (+node coffee)
Slim .slim slim (>= 0.7) 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
21 changes: 20 additions & 1 deletion bin/stasis
Expand Up @@ -2,4 +2,23 @@


require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis") 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
2 changes: 2 additions & 0 deletions config/gemsets.yml
@@ -1,5 +1,7 @@
stasis: stasis:
directory_watcher: ~>1.4.0
rake: >=0.8.7 rake: >=0.8.7
rocco: ~>0.6 rocco: ~>0.6
rspec: ~>1.0 rspec: ~>1.0
slop: ~>1.6.0
tilt: ~>1.2 tilt: ~>1.2
2 changes: 2 additions & 0 deletions config/gemspec.yml
Expand Up @@ -7,6 +7,8 @@ homepage: http://wintoni.us
summary: Markup-agnostic static site generator summary: Markup-agnostic static site generator
description: A markup-agnostic static site generator. description: A markup-agnostic static site generator.
dependencies: dependencies:
- directory_watcher
- slop
- tilt - tilt
development_dependencies: development_dependencies:
- rake - rake
Expand Down
3 changes: 2 additions & 1 deletion lib/stasis.rb
Expand Up @@ -31,16 +31,17 @@


# Require all Stasis library files. # Require all Stasis library files.


require 'stasis/auto'
require 'stasis/plugin' require 'stasis/plugin'


require 'stasis/scope' require 'stasis/scope'
require 'stasis/scope/action' require 'stasis/scope/action'
require 'stasis/scope/controller' require 'stasis/scope/controller'


require 'stasis/plugins/after'
require 'stasis/plugins/before' require 'stasis/plugins/before'
require 'stasis/plugins/helpers' require 'stasis/plugins/helpers'
require 'stasis/plugins/ignore' require 'stasis/plugins/ignore'
require 'stasis/plugins/instead'
require 'stasis/plugins/layout' require 'stasis/plugins/layout'
require 'stasis/plugins/priority' require 'stasis/plugins/priority'
require 'stasis/plugins/render' require 'stasis/plugins/render'
Expand Down
52 changes: 52 additions & 0 deletions 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
24 changes: 8 additions & 16 deletions lib/stasis/plugins/before.rb
Expand Up @@ -12,11 +12,14 @@ def initialize


# This method is bound to all controllers. Stores a block in the `@blocks` `Hash`, # 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. # 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 if block
path = controller._resolve(path, true) paths.each do |path|
@blocks[path] ||= [] path = controller._resolve(path, true)
@blocks[path] << block @blocks[path] ||= []
@blocks[path] << block
end
else else
@blocks[path] || [] @blocks[path] || []
end end
Expand All @@ -38,22 +41,11 @@ def before_render(controller, action, path)
if @blocks && matches = _match_key?(@blocks, path) if @blocks && matches = _match_key?(@blocks, path)
action._[:path] = path action._[:path] = path
matches.flatten.each do |block| matches.flatten.each do |block|
capture_render(action) do action.instance_eval(&block)
action.instance_eval(&block)
end
end end
action._[:path] = nil action._[:path] = nil
end end
[ controller, action, path ] [ controller, action, path ]
end 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
end end
11 changes: 11 additions & 0 deletions 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
4 changes: 0 additions & 4 deletions lib/stasis/plugins/render.rb
Expand Up @@ -50,10 +50,6 @@ def render(action, path_or_options={}, options={}, &block)
output output
end end


if action._[:capture_render]
action._[:render] = output
end

output output
end end
end end
Expand Down
6 changes: 3 additions & 3 deletions spec/fixtures/project/controller.rb
Expand Up @@ -28,15 +28,15 @@
end end


before 'before_render_text.html.haml' do before 'before_render_text.html.haml' do
render :text => 'root' instead render(:text => 'root')
end end


before 'before_render_partial.html.haml' do before 'before_render_partial.html.haml' do
render :path => '_partial.html.haml' instead render(:path => '_partial.html.haml')
end end


before 'before_non_existent.html' do before 'before_non_existent.html' do
render :path => '_partial.html.haml' instead render(:path => '_partial.html.haml')
end end


# Helpers # Helpers
Expand Down
22 changes: 11 additions & 11 deletions spec/fixtures/project/subdirectory/controller.rb
Expand Up @@ -24,15 +24,11 @@
end end


before 'before_render_text.html.haml' do before 'before_render_text.html.haml' do
render :text => 'subdirectory' instead render(:text => 'subdirectory')
end end


before 'before_render_partial.html.haml' do before 'before_render_partial.html.haml', 'before_non_existent.html' do
render :path => '_partial.html.haml' instead render(:path => '_partial.html.haml')
end

before 'before_non_existent.html' do
render :path => '_partial.html.haml'
end end


# Helpers # Helpers
Expand All @@ -49,8 +45,10 @@ def helper


# Layout # Layout


layout 'layout_controller.html.haml' => 'layout.html.haml' layout(
layout 'layout_controller_from_root.html.haml' => '/layout.html.haml' 'layout_controller.html.haml' => 'layout.html.haml',
'layout_controller_from_root.html.haml' => '/layout.html.haml'
)


before 'layout_action.html.haml' do before 'layout_action.html.haml' do
layout 'layout.html.haml' layout 'layout.html.haml'
Expand All @@ -62,5 +60,7 @@ def helper


# Priority # Priority


priority '/before_render_partial.html.haml' => 1 priority(
priority 'index.html.haml' => -1 '/before_render_partial.html.haml' => 1,
'index.html.haml' => -1
)

0 comments on commit 25ea4e2

Please sign in to comment.