Skip to content

Commit

Permalink
Generating by project directory instead of file list, allowing render…
Browse files Browse the repository at this point in the history
… to be used from any view, multi-directory files rendering to appropriate places, can now define plugin methods with different names than those that are exposed, automatically ignoring layouts, allowing use of regular expressions with ignore calls
  • Loading branch information
winton committed Apr 16, 2011
1 parent 61c114e commit c7aa205
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -6,9 +6,9 @@ A general-purpose static site generator.
Philosophy
----------

My preferred stack = static markup/assets (Nginx) + services (Node.js).
I like static web sites + services (Node.js, Sinatra, or Rails).

Stasis helps with the first part of the equation. It can even get fairly dynamic if you need it to.
Stasis is a static site framework that is almost as powerful as the dynamic frameworks.

Requirements
------------
Expand Down
2 changes: 1 addition & 1 deletion bin/stasis
Expand Up @@ -2,4 +2,4 @@

require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis")

Stasis.generate Dir.pwd, '**/*'
Stasis.new(Dir.pwd).generate
47 changes: 38 additions & 9 deletions lib/stasis.rb
Expand Up @@ -25,9 +25,10 @@

class Stasis

attr_reader :controllers, :plugins, :root
attr_reader :controllers, :destination, :plugins, :root

def initialize(root)
def initialize(root, destination=root+'/public')
@destination = destination
@root = root
@plugins = self.class.constants.collect { |klass|
klass = klass.to_s
Expand All @@ -43,21 +44,49 @@ def initialize(root)
end
end

def generate(*paths)
paths = paths.collect { |p| Dir["#{root}/#{p}"] }.flatten
paths.reject! { |p| File.basename(p) == 'controller.rb' }
def generate
FileUtils.rm_rf(@destination)
paths = Dir["#{root}/**/*"]
paths.reject! { |p| File.basename(p) == 'controller.rb' || !File.file?(p) }
@controllers, paths = trigger(:before_all, '*', controllers, paths)
paths.each do |path|
@action = Context::Action.new(@plugins)
trigger(:before_render, path, @action, path)
layout_controller = @controllers[File.dirname(@action._[:layout])]
path_controller = @controllers[File.dirname(path)]
if @action._[:layout]
view = @action.render(@action._[:layout]) { @action.render(path) }
@action._[:controller] = layout_controller
view = @action.render(@action._[:layout]) do
@action._[:controller] = path_controller
output = @action.render(path)
@action._[:controller] = layout_controller
output
end
else
@action._[:controller] = path_controller
view = @action.render(path)
end
@action._[:controller] = nil
trigger(:after_render, path, @action, path)
puts @action._[:layout].inspect
puts view.inspect
if @action._[:destination]
if @action._[:destination][0..0] == '/'
dest = @action._[:destination]
else
dest = File.dirname(path[root.length..-1]) + @action._[:destination]
end
else
dest = path[root.length..-1]
end
dest = "#{@destination}#{dest}"
Tilt.mappings.keys.each do |ext|
if File.extname(dest)[1..-1] == ext
dest = dest[0..-1*ext.length-2]
end
end
FileUtils.mkdir_p(File.dirname(dest))
File.open(dest, 'w') do |f|
f.write(view)
end
end
end

Expand All @@ -83,6 +112,6 @@ def trigger(type, path, *args, &block)
end
@action._[:controller] = nil
end
args[1..-1]
args
end
end
2 changes: 1 addition & 1 deletion lib/stasis/context/controller.rb
Expand Up @@ -22,7 +22,7 @@ def resolve(path)
nil
elsif path.is_a?(Regexp)
path
elsif File.file?(p = "#{_[:root]}#{_[:rel_dir]}/#{path}")
elsif File.file?(p = "#{_[:dir]}/#{path}")
p
elsif File.file?(p = "#{_[:root]}/#{path}")
p
Expand Down
6 changes: 5 additions & 1 deletion lib/stasis/plugin/helpers.rb
Expand Up @@ -40,7 +40,11 @@ def _send_to_plugin_by_type(type, *args, &block)
end
end
end
arg
if arg.length > args.length
arg[1..-1]
else
arg
end
end
end
end
Expand Down
19 changes: 13 additions & 6 deletions lib/stasis/plugins/destination.rb
@@ -1,22 +1,29 @@
class Stasis
class Destination < Plugin

action_method :destination => :destination_action
before_render :before_render
controller_method :destination
controller_method :destination => :destination_controller

def before_render(controller, action, path)
if @destinations && match = match_key?(@destinations, path)[0]
action.instance_eval <<-RUBY
@destination = #{match}
RUBY
action._[:destination] = match
else
action._[:destination] = nil
end
[ controller, action, path ]
end

def destination(controller, hash)
def destination_action(action, path)
@destinations ||= {}
@destinations[action._[:path]] = path
before_render(nil, action, action._[:path])
end

def destination_controller(controller, hash)
@destinations ||= {}
@destinations.merge! hash.inject({}) { |hash, (key, value)|
hash[key] = controller.resolve(value)
hash[controller.resolve(key)] = value
hash
}
end
Expand Down
13 changes: 12 additions & 1 deletion lib/stasis/plugins/ignore.rb
Expand Up @@ -5,7 +5,18 @@ class Ignore < Plugin
controller_method :ignore

def before_all(controller, controllers, paths)
[ controller, controllers, paths - @ignore ]
(@ignore || []).each do |ignore|
paths.reject! do |path|
if ignore.is_a?(::String)
ignore == path
elsif ignore.is_a?(::Regexp)
ignore =~ path
else
false
end
end
end
[ controller, controllers, paths ]
end

def ignore(controller, *array)
Expand Down
1 change: 1 addition & 0 deletions lib/stasis/plugins/layout.rb
Expand Up @@ -32,6 +32,7 @@ def layout_controller(controller, hash_or_string)
@layouts ||= {}
@layouts.merge! hash.inject({}) { |hash, (key, value)|
hash[key] = controller.resolve(value)
controller.ignore(hash[key])
hash
}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/stasis/plugins/priority.rb
Expand Up @@ -5,7 +5,7 @@ class Priority < Plugin
controller_method :priority

def before_all(controller, controllers, paths)
priorities = @priorities.sort { |a, b| b[1] <=> a[1] }
priorities = (@priorities || []).sort { |a, b| b[1] <=> a[1] }
priorities = priorities.inject({}) do |hash, (path_or_regexp, priority)|
paths.each do |path|
if path_or_regexp.is_a?(::Regexp) && path =~ path_or_regexp
Expand Down
14 changes: 4 additions & 10 deletions spec/fixtures/project/controller.rb
@@ -1,14 +1,8 @@
ignore 'layout.html.erb'
layout 'layout.html.erb'
priority /.*erb/ => 1

before 'view.html.erb' do
#layout 'layout.html.erb'
@title = 'My Site'
end
# Ignore partials
ignore /\/_.*/

helpers do
def blah
'!!!'
def link_to(href, value)
'<a href="' + href + '">' + value + '</a>'
end
end
6 changes: 0 additions & 6 deletions spec/fixtures/project/layout.html.erb

This file was deleted.

2 changes: 0 additions & 2 deletions spec/fixtures/project/view.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/stasis_spec.rb
Expand Up @@ -4,6 +4,6 @@

it "should" do
stasis = Stasis.new("#{$root}/spec/fixtures/project")
stasis.generate("**/*")
stasis.generate
end
end

0 comments on commit c7aa205

Please sign in to comment.