lib/vitrine.rb in vitrine-0.0.6 vs lib/vitrine.rb in vitrine-0.0.7
- old
+ new
@@ -1,8 +1,9 @@
require 'sinatra/base'
require 'coffee-script'
require 'sass'
+require 'pathname'
require_relative 'version'
module Vitrine
DEFAULTS = { root: Dir.getwd, port: 4000, host: '127.0.0.1' }
@@ -18,24 +19,27 @@
$stderr.puts "ERROR: `public' directory not found under the current tree, you might want to create it"
exit 1
end
end
- # Will compile all SCSS and CoffeeScript, and also crawl the template tree and generate
- # HTML for all of the files in the template tree. The resulting files will be copied to
- # a directory of the build.
- # def self.build!
-
# Run the server, largely stolen from Serve
def self.run(options = DEFAULTS)
check_dirs_present!
app = Rack::Builder.new do
- use Rack::CommonLogger
use Rack::ShowStatus
use Rack::ShowExceptions
+ guardfile_path = options[:root] + '/Guardfile'
+ if File.exist?(guardfile_path)
+ $stderr.puts "Attaching LiveReload via Guardfile at #{guardfile_path.inspect}"
+ # Assume livereload is engaged
+ use Rack::LiveReload
+ else
+ $stderr.puts "No Guardfile found, so there won't be any livereload injection"
+ end
+
vitrine = Vitrine::App.new
vitrine.settings.set :root, options[:root]
run vitrine
end
@@ -62,10 +66,12 @@
end
end
end
end
+require 'rack-livereload'
+
# A little idiosyncrastic asset server.
# Does very simple things:
# * sensible detector for default pages (they render from Sinatra view templates)
# * automatic compilation of CoffeeScript and SASS assets - just request them with .js and .css
# and Vitrine will find them and compile them for you on the spot
@@ -74,19 +80,25 @@
set :show_exceptions, false
set :raise_errors, true
set :root, File.expand_path(File.dirname(__FILE__))
set :views, lambda { File.join(settings.root, "views") }
- # Use Rack::TryStatic to attempt to load files from public first
-# require 'rack/contrib/try_static'
-# use Rack::TryStatic,
-# :root => (settings.root + '/public'),
-# :urls => %w(/), :try => %w(.html index.html /index.html)
+
# For extensionless things try to pick out the related templates
# from the views directory, and render them with a default layout
get /^([^\.]+)$/ do | extensionless_path |
+ render_template(extensionless_path)
+ end
+
+
+ # Allow "fake" form submits
+ post /^([^\.]+)$/ do | extensionless_path |
+ render_template(extensionless_path)
+ end
+
+ def render_template(extensionless_path)
# Find the related view
specific_view = extensionless_path + ".*"
view_index = extensionless_path + "/index.*"
# Catch-all template for HTML5 apps using pushState
@@ -107,17 +119,20 @@
template_path = possibilites.shift
# If nothing is found just bail
unless template_path
err = possible_globs.map{|e| e.inspect }.join(', ')
- raise "No template found - tried #{er}"
+ raise "No template found - tried #{err}"
end
- $stderr.puts "Rendering via template #{template_path.inspect}"
+ relative_path = Pathname.new(template_path).relative_path_from(Pathname.new(settings.views))
+ $stderr.puts "-> #{extensionless_path.inspect} : Rendering via template #{relative_path.to_s.inspect}"
+
+ locals = {}
# Auto-pick the template engine out of the extension
template_engine = File.extname(template_path).gsub(/^\./, '')
- render(template_engine, File.read(template_path), :layout => get_layout)
+ render(template_engine, File.read(template_path), :layout => get_layout, :locals => locals)
end
def get_layout
layouts = Dir.glob(File.join(settings.views, 'layout.*'))
layouts.any? ? :layout : false
\ No newline at end of file