give sprockets access to middleman environment scope (variables, data, etc)

This commit is contained in:
Thomas Reynolds 2011-08-31 13:15:59 -07:00
parent af9e4989e3
commit cc577c0c73
10 changed files with 53 additions and 29 deletions

View file

@ -1,3 +1,3 @@
source "http://www.rubygems.org" source :rubygems
gemspec gemspec

View file

@ -9,3 +9,8 @@ Feature: Sprockets
Given the Server is running at "sprockets-app" Given the Server is running at "sprockets-app"
When I go to "/library/javascripts/sprockets_base.js" When I go to "/library/javascripts/sprockets_base.js"
Then I should see "sprockets_sub_function" Then I should see "sprockets_sub_function"
Scenario: Sprockets should have access to yaml data
Given the Server is running at "test-app"
When I go to "/javascripts/multiple_engines.js"
Then I should see "Hello One"

View file

@ -2,6 +2,6 @@
Feature: Sprockets Gems Feature: Sprockets Gems
Scenario: Sprockets can pull jQuery from gem Scenario: Sprockets can pull jQuery from gem
Given the Server is running at "test-app" Given the Server is running at "sprockets-app"
When I go to "/javascripts/jquery_base.js" When I go to "/javascripts/jquery_include.js"
# Then I should see "sprockets_sub_function" Then I should see "var jQuery ="

View file

@ -0,0 +1 @@
//= require "jquery"

View file

@ -0,0 +1 @@
alert "Hello <%= data.test[0].title %>"

View file

@ -13,7 +13,7 @@ module Middleman::CoreExtensions::Data
module Helpers module Helpers
def data def data
@@data ||= DataObject.new(self) self.class.data
end end
end end
@ -33,7 +33,7 @@ module Middleman::CoreExtensions::Data
elsif @@callback_sources.has_key?(path.to_s) elsif @@callback_sources.has_key?(path.to_s)
response = @@callback_sources[path.to_s].call() response = @@callback_sources[path.to_s].call()
else else
file_path = File.join(@app.class.root, @app.class.data_dir, "#{path}.yml") file_path = File.join(@app.root, @app.data_dir, "#{path}.yml")
if File.exists? file_path if File.exists? file_path
response = YAML.load_file(file_path) response = YAML.load_file(file_path)
end end
@ -74,6 +74,10 @@ module Middleman::CoreExtensions::Data
end end
module ClassMethods module ClassMethods
def data
@data ||= DataObject.new(self)
end
# Makes a hash available on the data var with a given name # Makes a hash available on the data var with a given name
def data_content(name, content) def data_content(name, content)
DataObject.data_content(name, content) DataObject.data_content(name, content)

View file

@ -6,23 +6,23 @@ module Middleman::CoreExtensions::FrontMatter
def registered(app) def registered(app)
app.extend ClassMethods app.extend ClassMethods
Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md' ::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md'
Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md' ::Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md'
Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md' ::Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md'
Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md' ::Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md'
app.set :markdown_engine_prefix, Middleman::CoreExtensions::FrontMatter app.set :markdown_engine_prefix, ::Middleman::CoreExtensions::FrontMatter
Tilt::register RedClothTemplate, 'textile' ::Tilt::register RedClothTemplate, 'textile'
Tilt.prefer(RedClothTemplate) ::Tilt.prefer(RedClothTemplate)
Tilt::register ERBTemplate, 'erb', 'rhtml' ::Tilt::register ERBTemplate, 'erb', 'rhtml'
Tilt.prefer(ERBTemplate) ::Tilt.prefer(ERBTemplate)
Tilt::register SlimTemplate, 'slim' ::Tilt::register SlimTemplate, 'slim'
Tilt.prefer(SlimTemplate) ::Tilt.prefer(SlimTemplate)
Tilt::register HamlTemplate, 'haml' ::Tilt::register HamlTemplate, 'haml'
Tilt.prefer(HamlTemplate) ::Tilt.prefer(HamlTemplate)
app.after_configuration do app.after_configuration do
app.before_processing do app.before_processing do
@ -82,27 +82,27 @@ module Middleman::CoreExtensions::FrontMatter
end end
end end
class RDiscountTemplate < Tilt::RDiscountTemplate class RDiscountTemplate < ::Tilt::RDiscountTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class RedcarpetTemplate < Tilt::RedcarpetTemplate class RedcarpetTemplate < ::Tilt::RedcarpetTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class MarukuTemplate < Tilt::MarukuTemplate class MarukuTemplate < ::Tilt::MarukuTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class RedClothTemplate < Tilt::RedClothTemplate class RedClothTemplate < ::Tilt::RedClothTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class KramdownTemplate < Tilt::KramdownTemplate class KramdownTemplate < ::Tilt::KramdownTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class ERBTemplate < Tilt::ERBTemplate class ERBTemplate < ::Tilt::ERBTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end
class HamlTemplate < Tilt::HamlTemplate class HamlTemplate < ::Tilt::HamlTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware include Middleman::CoreExtensions::FrontMatter::YamlAware
end end

View file

@ -36,6 +36,18 @@ module Middleman::CoreExtensions::Sprockets
full_path = File.join(app.root, app.views) unless app.views.include?(app.root) full_path = File.join(app.root, app.views) unless app.views.include?(app.root)
super File.expand_path(full_path) super File.expand_path(full_path)
# Make the app context available to Sprockets
context_class.send(:define_method, :app) { app }
context_class.class_eval do
def method_missing(name)
if app.respond_to?(name)
app.send(name)
else
super
end
end
end
end end
end end

View file

@ -8,7 +8,7 @@ module Middleman::Renderers::Markdown
app.set :markdown_engine, :maruku app.set :markdown_engine, :maruku
if !app.respond_to? :markdown_engine_prefix if !app.respond_to? :markdown_engine_prefix
app.set :markdown_engine_prefix, Tilt app.set :markdown_engine_prefix, ::Tilt
end end
app.after_configuration do app.after_configuration do
@ -18,7 +18,7 @@ module Middleman::Renderers::Markdown
engine = app.tilt_template_from_symbol(engine) engine = app.tilt_template_from_symbol(engine)
end end
Tilt.prefer(engine) ::Tilt.prefer(engine)
end end
end end
alias :included :registered alias :included :registered

View file

@ -73,6 +73,7 @@ eos
s.add_dependency("middleman-livereload", ["~> 0.2.0"]) s.add_dependency("middleman-livereload", ["~> 0.2.0"])
# Development and test # Development and test
# s.add_development_dependency("jquery-rails")
s.add_development_dependency("coffee-filter", ["~> 0.1.1"]) s.add_development_dependency("coffee-filter", ["~> 0.1.1"])
s.add_development_dependency("cucumber", ["~> 1.0.2"]) s.add_development_dependency("cucumber", ["~> 1.0.2"])
s.add_development_dependency("rake", ["0.8.7"]) s.add_development_dependency("rake", ["0.8.7"])