machined 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,26 +17,26 @@ module Machined
17
17
  include Helpers::LocalsHelpers
18
18
  include Helpers::PageHelpers
19
19
  include Helpers::RenderHelpers
20
-
20
+
21
21
  # Override initialize to add helpers
22
22
  # from the Machined environment.
23
23
  def initialize(*args) # :nodoc:
24
24
  super
25
25
  add_machined_helpers
26
26
  end
27
-
27
+
28
28
  # Returns the main Machined environment instance.
29
29
  def machined
30
30
  environment.machined
31
31
  end
32
-
32
+
33
33
  # Returns the configuration of the Machined environment.
34
34
  def config
35
35
  machined.config
36
36
  end
37
-
37
+
38
38
  protected
39
-
39
+
40
40
  # Loops through the helpers added to the Machined
41
41
  # environment and adds them to the Context. Supports
42
42
  # blocks and Modules.
@@ -10,12 +10,12 @@ require 'tilt'
10
10
  module Machined
11
11
  class Environment
12
12
  include Initializable
13
-
13
+
14
14
  # Delegate some common configuration accessors
15
15
  # to the config object.
16
16
  delegate :root, :config_path, :output_path, :lib_path, :environment,
17
17
  :to => :config
18
-
18
+
19
19
  # Default options for a Machined environment.
20
20
  DEFAULT_OPTIONS = {
21
21
  # Global configuration
@@ -30,7 +30,7 @@ module Machined
30
30
  :digest_assets => false,
31
31
  :gzip_assets => false,
32
32
  :layout => 'application',
33
-
33
+
34
34
  # Sprocket paths and URLs
35
35
  :assets_path => 'assets',
36
36
  :assets_paths => %w(app/assets lib/assets vendor/assets),
@@ -40,7 +40,7 @@ module Machined
40
40
  :pages_url => '/',
41
41
  :views_path => 'views',
42
42
  :views_paths => %w(app/views),
43
-
43
+
44
44
  # Compression configuration
45
45
  :compress => false,
46
46
  :compress_css => false,
@@ -48,7 +48,7 @@ module Machined
48
48
  :css_compressor => nil,
49
49
  :js_compressor => nil
50
50
  }.freeze
51
-
51
+
52
52
  # A hash of Javascript compressors. When `config.js_compressor`
53
53
  # is set using a symbol, such as `:uglifier`, this is where
54
54
  # we check which engine to use.
@@ -59,7 +59,7 @@ module Machined
59
59
  :closure => Crush::Closure::Compiler,
60
60
  :uglifier => Crush::Uglifier
61
61
  }
62
-
62
+
63
63
  # A hash of CSS compressors. When `config.css_compressor`
64
64
  # is set using a symbol, such as `:sass`, this is where
65
65
  # we check which engine to use.
@@ -69,29 +69,29 @@ module Machined
69
69
  :yui => Crush::YUI::CssCompressor,
70
70
  :sass => Crush::Sass::Engine
71
71
  }
72
-
72
+
73
73
  # The global configuration for the Machined
74
74
  # environment.
75
75
  attr_reader :config
76
-
76
+
77
77
  # An array of the helpers added to the Context
78
78
  # through the `#helpers` method.
79
79
  attr_reader :context_helpers
80
-
80
+
81
81
  # When the Machined environment is used as a Rack server, this
82
82
  # will reference the actual `Machined::Server` instance that handles
83
83
  # the requests.
84
84
  attr_reader :server
85
-
85
+
86
86
  # An `Array` of the Sprockets environments (actually `Machined::Sprocket`
87
87
  # instances) that are the core of a Machined environment.
88
88
  attr_reader :sprockets
89
-
89
+
90
90
  # When the Machined environment is compiling static files,
91
91
  # this will reference the `Machined::StaticCompiler` which handles
92
92
  # looping through the available files and generating them.
93
93
  attr_reader :static_compiler
94
-
94
+
95
95
  # Creates a new Machined environment. It sets up three default
96
96
  # sprockets:
97
97
  #
@@ -110,15 +110,15 @@ module Machined
110
110
  config.output_path = root.join config.output_path
111
111
  config.lib_path = root.join config.lib_path
112
112
  config.environment = ActiveSupport::StringInquirer.new(config.environment)
113
-
113
+
114
114
  run_initializers self
115
115
  end
116
-
116
+
117
117
  # If bundler is used, require all the gems in the Gemfile
118
118
  # for this environment.
119
119
  initializer :require_bundle do
120
120
  next if config.skip_bundle
121
-
121
+
122
122
  ENV['BUNDLE_GEMFILE'] ||= root.join('Gemfile').to_s
123
123
  if File.exist? ENV['BUNDLE_GEMFILE']
124
124
  require 'bundler/setup'
@@ -126,37 +126,38 @@ module Machined
126
126
  Bundler.require :default, environment.to_sym
127
127
  end
128
128
  end
129
-
129
+
130
130
  # Appends the lib directory to the load path.
131
131
  # Changes to files in this directory will trigger a reload
132
132
  # of the Machined environment.
133
133
  initializer :setup_autoloading do
134
134
  next if config.skip_autoloading || !lib_path.exist?
135
-
135
+
136
136
  require 'active_support/dependencies'
137
137
  ActiveSupport::Dependencies.autoload_paths = [lib_path.to_s]
138
138
  end
139
-
139
+
140
140
  # Create and append the default `assets` sprocket.
141
141
  # This sprocket mimics the asset pipeline in Rails 3.1.
142
142
  initializer :create_assets_sprocket do
143
143
  append_sprocket :assets, :assets => true
144
144
  end
145
-
145
+
146
146
  # Create and append the default `pages` sprocket.
147
147
  # This sprocket is responsible for processing HTML pages,
148
148
  # and includes processors for wrapping pages in layouts and
149
149
  # reading YAML front matter.
150
150
  initializer :create_pages_sprocket do
151
151
  next if config.assets_only
152
-
152
+
153
153
  append_sprocket :pages do |pages|
154
154
  pages.register_mime_type 'text/html', '.html'
155
155
  pages.register_preprocessor 'text/html', Processors::FrontMatterProcessor
156
156
  pages.register_postprocessor 'text/html', Processors::LayoutProcessor
157
+ pages.clear_paths
157
158
  end
158
159
  end
159
-
160
+
160
161
  # Create and append the default `views` sprocket.
161
162
  # The files in this sprocket are not compiled. They are
162
163
  # meant to be resources for the other sprockets. For instance,
@@ -164,21 +165,22 @@ module Machined
164
165
  initializer :create_views_sprocket do
165
166
  append_sprocket :views, :compile => false do |views|
166
167
  views.register_mime_type 'text/html', '.html'
168
+ views.clear_paths
167
169
  end
168
170
  end
169
-
171
+
170
172
  # If there's a config file, execute with the scope of the
171
173
  # newly created Machined environment. The default sprockets are
172
174
  # available at this point, but not fully configured. This is so
173
175
  # you can actually configure the sprockets with this file.
174
176
  initializer :eval_config_file do
175
177
  instance_eval config_path.read if config_path.exist?
176
-
178
+
177
179
  # This could be changed in the config file
178
180
  config.output_path = root.join config.output_path
179
181
  remove_sprocket(:pages) if config.assets_only && @pages
180
182
  end
181
-
183
+
182
184
  # Register all available Tilt templates to every
183
185
  # sprocket other than the assets sprocket
184
186
  initializer :register_all_templates do
@@ -186,7 +188,7 @@ module Machined
186
188
  sprocket.use_all_templates unless sprocket.config.assets
187
189
  end
188
190
  end
189
-
191
+
190
192
  # Setup the global cache. Defaults to an in memory
191
193
  # caching for development, and a file based cache for production.
192
194
  initializer :configure_cache do
@@ -197,24 +199,24 @@ module Machined
197
199
  config.cache = :memory_store
198
200
  end
199
201
  end
200
-
202
+
201
203
  config.cache = ActiveSupport::Cache.lookup_store(*config.cache)
202
204
  end
203
-
205
+
204
206
  # Do any configuration to the assets sprockets necessary
205
207
  # after the config file has been evaled.
206
208
  initializer :configure_assets_sprocket do
207
209
  next unless @assets
208
-
210
+
209
211
  # Append the directories within the configured paths
210
212
  append_paths assets, Utils.existent_directories(root.join(config.assets_path))
211
213
  config.assets_paths.each do |asset_path|
212
214
  append_paths assets, Utils.existent_directories(root.join(asset_path))
213
215
  end
214
-
216
+
215
217
  # Append paths from Sprockets-plugin
216
218
  assets.append_plugin_paths if assets.respond_to?(:append_plugin_paths)
217
-
219
+
218
220
  # Search for Rails Engines with assets and append those
219
221
  if defined? Rails::Engine
220
222
  Rails::Engine.subclasses.each do |engine|
@@ -223,48 +225,48 @@ module Machined
223
225
  append_paths assets, engine.paths['vendor/assets'].existent_directories
224
226
  end
225
227
  end
226
-
228
+
227
229
  # Setup the base URL for the assets (like the Rails asset_prefix)
228
230
  assets.config.url = config.assets_url
229
-
231
+
230
232
  # Use the global cache store
231
233
  assets.cache = config.cache
232
234
  end
233
-
235
+
234
236
  # Do any configuration to the pages sprockets necessary
235
237
  # after the config file has been evaled.
236
238
  initializer :configure_pages_sprocket do
237
239
  next unless @pages
238
-
240
+
239
241
  # Append the configured pages paths
240
242
  append_path pages, config.pages_path
241
243
  append_paths pages, config.pages_paths
242
-
244
+
243
245
  # Setup the base URL for the pages
244
246
  pages.config.url = config.pages_url
245
-
247
+
246
248
  # Use the global cache store
247
249
  pages.cache = config.cache
248
250
  end
249
-
251
+
250
252
  # Do any configuration to the views sprockets necessary
251
253
  # after the config file has been evaled.
252
254
  initializer :configure_views_sprocket do
253
255
  next unless @views
254
-
256
+
255
257
  # Append the configured views paths
256
258
  append_path views, config.views_path
257
259
  append_paths views, config.views_paths
258
-
260
+
259
261
  # Use the global cache store
260
262
  views.cache = config.cache
261
263
  end
262
-
264
+
263
265
  # Setup the JavaScript and CSS compressors
264
266
  # for the assets based on the configuration.
265
267
  initializer :configure_assets_compression do
266
268
  next unless @assets
267
-
269
+
268
270
  if config.compress
269
271
  config.compress_js = true
270
272
  config.compress_css = true
@@ -275,12 +277,12 @@ module Machined
275
277
  assets.js_compressor = js_compressor if config.compress_js
276
278
  assets.css_compressor = css_compressor if config.compress_css
277
279
  end
278
-
280
+
279
281
  # Finally, configure Sprockets::Helpers to
280
282
  # match curernt configuration.
281
283
  initializer :configure_sprockets_helpers do
282
284
  next unless @assets
283
-
285
+
284
286
  Sprockets::Helpers.configure do |helpers|
285
287
  helpers.environment = assets
286
288
  helpers.digest = config.digest_assets
@@ -288,37 +290,37 @@ module Machined
288
290
  helpers.public_path = config.output_path.to_s
289
291
  end
290
292
  end
291
-
293
+
292
294
  # Handles Rack requests by passing the +env+ to an instance
293
295
  # of `Machined::Server`.
294
296
  def call(env)
295
297
  @server ||= Server.new self
296
298
  server.call(env)
297
299
  end
298
-
300
+
299
301
  # Loops through the available static files and generates them in
300
302
  # the output path.
301
303
  def compile
302
304
  @static_compiler ||= StaticCompiler.new self
303
305
  static_compiler.compile
304
306
  end
305
-
307
+
306
308
  # Reloads the environment. This will re-evaluate the config file &
307
309
  # clear the current cache.
308
310
  def reload
309
311
  # Make sure we have a fresh cache after we reload
310
312
  config.cache.clear if config.cache.respond_to?(:clear)
311
-
313
+
312
314
  # Reload dependencies as well, if necessary
313
315
  ActiveSupport::Dependencies.clear if defined?(ActiveSupport::Dependencies)
314
-
316
+
315
317
  # Use current configuration, but skip bundle and autoloading initializers
316
318
  current_config = config.marshal_dump.dup
317
319
  current_config.merge! :skip_bundle => true#, :skip_autoloading => true)
318
-
320
+
319
321
  initialize current_config
320
322
  end
321
-
323
+
322
324
  # Creates a Machined sprocket with the given +name+ and +options+
323
325
  # and appends it to the #sprockets list. This will also create
324
326
  # an accessor with the given name that references the created sprocket.
@@ -326,7 +328,7 @@ module Machined
326
328
  # machined.append_sprocket :updates, :url => '/news' do |updates|
327
329
  # updates.append_path 'updates'
328
330
  # end
329
- #
331
+ #
330
332
  # machined.updates # => #<Machined::Sprocket...>
331
333
  # machined.updates.config.url # => '/news'
332
334
  # machined.updates.paths # => [ '.../updates' ]
@@ -336,7 +338,7 @@ module Machined
336
338
  sprockets.push(sprocket).uniq!
337
339
  end
338
340
  end
339
-
341
+
340
342
  # Creates a Machined sprocket with the given +name+ and +options+
341
343
  # and prepends it to the #sprockets list.
342
344
  def prepend_sprocket(name, options = {}, &block)
@@ -344,7 +346,7 @@ module Machined
344
346
  sprockets.unshift(sprocket).uniq!
345
347
  end
346
348
  end
347
-
349
+
348
350
  # Removes the sprocket with the given name. This is useful if
349
351
  # you don't need one of the default Sprockets.
350
352
  def remove_sprocket(name)
@@ -353,7 +355,7 @@ module Machined
353
355
  set_sprocket(name, nil)
354
356
  end
355
357
  end
356
-
358
+
357
359
  # Adds helpers that can be used within asset files.
358
360
  # There's two supported ways to add helpers, the first of
359
361
  # which is similar to how the `#helpers` DSL works in Sinatra:
@@ -378,7 +380,7 @@ module Machined
378
380
  @context_helpers << block if block_given?
379
381
  @context_helpers.push *modules
380
382
  end
381
-
383
+
382
384
  unless method_defined?(:define_singleton_method)
383
385
  # Add define_singleton_method for Ruby 1.8.7
384
386
  # This is used to define the sprocket accessor methods.
@@ -387,19 +389,19 @@ module Machined
387
389
  singleton_class.__send__ :define_method, symbol, method || block
388
390
  end
389
391
  end
390
-
392
+
391
393
  protected
392
-
394
+
393
395
  # Returns the sprocket registered with the given name.
394
396
  def get_sprocket(name)
395
397
  instance_variable_get "@#{name}"
396
398
  end
397
-
399
+
398
400
  # Sets the sprocket with the give name.
399
401
  def set_sprocket(name, sprocket)
400
402
  instance_variable_set "@#{name}", sprocket
401
403
  end
402
-
404
+
403
405
  # Factory method for creating a `Machined::Sprocket` instance.
404
406
  # This is used in both `#append_sprocket` and `#prepend_sprocket`.
405
407
  def create_sprocket(name, options = {}, &block) # :nodoc:
@@ -410,7 +412,7 @@ module Machined
410
412
  yield sprocket if block_given?
411
413
  end
412
414
  end
413
-
415
+
414
416
  # Appends the given +path+ to the given +sprocket+
415
417
  # This makes sure the path is relative to the `root` path
416
418
  # or an absolute path pointing somewhere else. It also
@@ -419,7 +421,7 @@ module Machined
419
421
  path = Pathname.new(path).expand_path(root)
420
422
  sprocket.append_path(path) if path.exist?
421
423
  end
422
-
424
+
423
425
  # Appends the given `Array` of +paths+ to the given +sprocket+.
424
426
  def append_paths(sprocket, paths) # :nodoc:
425
427
  paths or return
@@ -427,7 +429,7 @@ module Machined
427
429
  append_path sprocket, path
428
430
  end
429
431
  end
430
-
432
+
431
433
  # Returns the Javascript compression engine, based on
432
434
  # what's set in `config.js_compressor`. If `config.js_compressor`
433
435
  # is nil, let Tilt + Crush decide which one to use.
@@ -442,7 +444,7 @@ module Machined
442
444
  Tilt['js']
443
445
  end
444
446
  end
445
-
447
+
446
448
  # Returns the CSS compression engine, based on
447
449
  # what's set in `config.css_compressor`. If `config.css_compressor`
448
450
  # is nil, let Tilt + Crush decide which one to use.