Permalink
Browse files

Generating by project directory instead of file list, allowing render…

… 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...
1 parent 61c114e commit c7aa205f751e7d6dc4ab900cee8deaabaf55fe9a @winton committed Apr 16, 2011
View
4 README.md
@@ -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
------------
View
2 bin/stasis
@@ -2,4 +2,4 @@
require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis")
-Stasis.generate Dir.pwd, '**/*'
+Stasis.new(Dir.pwd).generate
View
47 lib/stasis.rb
@@ -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
@@ -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
@@ -83,6 +112,6 @@ def trigger(type, path, *args, &block)
end
@action._[:controller] = nil
end
- args[1..-1]
+ args
end
end
View
2 lib/stasis/context/controller.rb
@@ -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
View
6 lib/stasis/plugin/helpers.rb
@@ -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
View
19 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
View
13 lib/stasis/plugins/ignore.rb
@@ -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)
View
1 lib/stasis/plugins/layout.rb
@@ -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
View
2 lib/stasis/plugins/priority.rb
@@ -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
View
14 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
View
6 spec/fixtures/project/layout.html.erb
@@ -1,6 +0,0 @@
-<html>
- <head>
- <title><%= @title %></title>
- </head>
- <body><%= yield %><%= blah %></body>
-</html>
View
2 spec/fixtures/project/view.html.erb
@@ -1,2 +0,0 @@
-<h1><%= @title %></h1>
-Welcome!
View
2 spec/stasis_spec.rb
@@ -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.