Class: Awestruct::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/engine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Awestruct::Config.new) ⇒ Engine

Returns a new instance of Engine.



43
44
45
46
47
48
49
50
# File 'lib/awestruct/engine.rb', line 43

def initialize(config=Awestruct::Config.new)
  Engine.instance = self
  @site = Site.new( self, config)
  @pipeline = Pipeline.new
  @site_page_loader = PageLoader.new( @site )
  @layout_page_loader = PageLoader.new( @site, :layouts )
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/awestruct/engine.rb', line 33

def config
  @config
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



32
33
34
# File 'lib/awestruct/engine.rb', line 32

def pipeline
  @pipeline
end

#siteObject (readonly)

Returns the value of attribute site.



31
32
33
# File 'lib/awestruct/engine.rb', line 31

def site
  @site
end

Class Method Details

.instanceObject



35
36
37
# File 'lib/awestruct/engine.rb', line 35

def self.instance
  @instance
end

.instance=(engine) ⇒ Object



39
40
41
# File 'lib/awestruct/engine.rb', line 39

def self.instance=(engine)
  @instance = engine
end

Instance Method Details

#adjust_load_pathObject



223
224
225
226
227
228
# File 'lib/awestruct/engine.rb', line 223

def adjust_load_path
  ext_dir = File.join( site.config.extension_dir )
  if ( $LOAD_PATH.index( ext_dir ).nil? )
    $LOAD_PATH << ext_dir
  end
end

#build_page_indexObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/awestruct/engine.rb', line 89

def build_page_index
  site.pages_by_relative_source_path = {}
  site.pages_by_output_path = {}
  site.pages.each do |p|
    # Add the layout to the set of dependencies
    p.dependencies.add_dependency(site.layouts.find_matching(p.layout, p.output_extension))
    if ( p.relative_source_path )
      site.pages_by_relative_source_path[ p.relative_source_path ] = p
    end
    site.pages_by_output_path[ p.output_path ] = p
  end
  site.layouts.each do |p|
    # Add the layout to the set of dependencies
    p.dependencies.add_dependency(site.layouts.find_matching(p.layout, p.output_extension))

    if ( p.relative_source_path )
      site.pages_by_relative_source_path[ p.relative_source_path ] = p
    end
  end
end

#configure_compassObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/awestruct/engine.rb', line 277

def configure_compass 
  site.images_dir      = File.join( site.config.dir, 'images' )
  site.fonts_dir       = File.join( site.config.dir, 'fonts' )
  site.sass_dir        = File.join( site.config.dir, 'stylesheets' )
  site.css_dir         = File.join( site.config.output_dir, 'stylesheets' )
  site.javascripts_dir = File.join( site.config.dir, 'javascripts' )

  ::Compass.configuration do |config|
    config.project_type = :stand_alone
    config.environment = site.profile
    config.project_path = site.config.dir
    config.sass_path = File.join(config.project_path, 'stylesheets')
    config.http_path = site.base_url || site.config.options.base_url || '/'
    config.css_path = File.join(site.output_dir, 'stylesheets')
    config.javascripts_path = File.join(site.output_dir, 'javascripts')
    config.http_javascripts_dir = File.join(config.http_path, 'javascripts')
    config.http_stylesheets_dir = File.join(config.http_path, 'stylesheets')
    config.sprite_load_path = [config.images_path]
    config.http_images_dir = File.join(config.http_path, 'images')
    config.images_path = File.join(config.project_path, 'images')
    config.fonts_dir = 'fonts'
    config.fonts_path = File.join(config.project_path, 'fonts')
    config.http_fonts_dir = File.join(config.http_path, 'fonts')

    if config.generated_images_dir == config.default_for('generated_images_dir')
      config.generated_images_dir = File.join(site.output_dir, 'images')
      config.http_generated_images_dir = File.join(config.http_path, 'images')
    end

    config.line_comments = lambda do
      if site.profile.eql? 'production'
        return false
      else
        if site.key? :compass_line_comments
          return site.compass_line_comments 
        end
        if site.key?(:scss) && site.scss.key?(:line_comments)
          return site.scss.line_comments
        end
        if site.key?(:sass) && site.sass.key?(:line_comments)
          return site.sass.line_comments
        end
        true
      end
    end.call

    config.output_style = lambda do
      if site.profile.eql? 'production'
        return :compressed
      else
        if site.key? :compass_output_style
          return site.compass_output_style
        end
        if (site.key? :scss) && (site.scss.key? :style)
          return site.scss.style
        end
        if (site.key? :sass) && (site.sass.key? :style)
          return site.sass.style
        end
      end
      :expanded
    end.call

    config.relative_assets = false
  end

  compass_config_file = File.join(site.config.config_dir, 'compass.rb')
  if (File.exists? compass_config_file)
    ::Compass.add_configuration ::Compass::Configuration::FileData.new_from_file(compass_config_file) 
  end 
  ::Compass.configuration # return for use elsewhere

  # TODO: Should we add an on_stylesheet_error block?  
end

#create_context(page, content = '') ⇒ Object



505
506
507
# File 'lib/awestruct/engine.rb', line 505

def create_context(page, content='')
  page.create_context( content )
end

#execute_pipeline(on_reload = false) ⇒ Object



271
272
273
274
275
# File 'lib/awestruct/engine.rb', line 271

def execute_pipeline(on_reload = false)
  FileUtils.mkdir_p( site.config.output_dir )
  FileUtils.mkdir_p( site.config.tmp_dir )
  pipeline.execute( site, on_reload )
end

#find_and_load_site_page(simple_path) ⇒ Object



494
495
496
497
498
499
500
501
502
503
# File 'lib/awestruct/engine.rb', line 494

def find_and_load_site_page(simple_path)
  path_glob = File.join( site.config.input_dir, simple_path + '.*' )
  candidates = Dir[ path_glob ]
  return nil if candidates.empty?
  throw Exception.new( "too many choices for #{simple_path}" ) if candidates.size != 1
  dir_pathname = Pathname.new( site.config.dir )
  path_name = Pathname.new( candidates[0] )
  path_name.relative_path_from( dir_pathname ).to_s
  load_page( candidates[0] )
end

#find_transitive_dependents(page) ⇒ Object



509
510
511
512
513
514
515
516
517
518
519
# File 'lib/awestruct/engine.rb', line 509

def find_transitive_dependents(page)
  deps = Set.new 
  deps << page
  if page.dependencies.dependents.size > 0
    page.dependencies.dependents.to_a.inject(deps) do |set, p| 
      set.merge find_transitive_dependents(p)
      set
    end
  end
  deps
end

#generate_outputObject



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/awestruct/engine.rb', line 359

def generate_output
  FileUtils.mkdir_p( site.config.output_dir )
  return_value = [Awestruct::ExceptionHelper::EXITCODES[:success]]
  begin
    return_value = Parallel.map(@site.pages, site.generation) do |page|
      generate_page( page )
    end
  rescue Exception => e
    return_value = [Awestruct::ExceptionHelper::EXITCODES[:generation_error]]
  end

  if return_value.nil? || return_value.include?(Awestruct::ExceptionHelper::EXITCODES[:generation_error])
    $LOG.error 'An error occurred during output generation, all pages may not have completed during generation'
    exit Awestruct::ExceptionHelper::EXITCODES[:generation_error]
  end
  site.engine.pipeline.execute_after_generation(site)
end

#generate_page(page, produce_output = true) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/awestruct/engine.rb', line 377

def generate_page(page, produce_output=true)
  if ( produce_output )
    $LOG.debug "Generating: #{generated_path}" if $LOG.debug? && config.verbose

    c = page.rendered_content
    c = site.engine.pipeline.apply_transformers( site, page, c )

    generated_path = File.join( site.config.output_dir, page.output_path )
    FileUtils.mkdir_p( File.dirname( generated_path ) )

    File.open( generated_path, 'wb' ) do |file|
      file << c
    end
    return Awestruct::ExceptionHelper::EXITCODES[:generation_error] if c.include? 'Backtrace:'
  elsif ( site.config.track_dependencies )
    if page.dependencies.load!
      $LOG.debug "Cached:     #{generated_path}" if $LOG.debug?
    else
      $LOG.debug "Analyzing:  #{generated_path}" if $LOG.debug?
      page.rendered_content
    end
    return Awestruct::ExceptionHelper::EXITCODES[:success]
  end
end

#generate_page_and_dependencies(page) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/awestruct/engine.rb', line 413

def generate_page_and_dependencies(page)

  if page.nil?
    return
  end

  if !page.output_path.nil? && !page.is_partial? && !page.__is_layout
    generate_page_internal(page)
  end

  regen_pages = page_dependencies( page )

  $LOG.debug "Starting regeneration of content dependent pages:" if regen_pages.size > 0 && $LOG.debug?

  regen_pages.each do |p|
    $LOG.info "Regenerating page #{p.output_path}" if $LOG.info?
    generate_page_internal(p)
  end

  regen_pages
end

#generate_page_internal(p) ⇒ Object



471
472
473
474
475
# File 'lib/awestruct/engine.rb', line 471

def generate_page_internal(p)
  unless ( p.output_path.nil? || p.__is_layout || !p.stale_output?(p.output_path) )
    generate_page( p )
  end
end

#load_default_site_yaml(profile = nil) ⇒ Object



127
128
129
130
# File 'lib/awestruct/engine.rb', line 127

def load_default_site_yaml(profile = nil)
  default_site_yaml_path = File.join( File.dirname( __FILE__ ), 'config', 'default-site.yml' )
  load_site_yaml( default_site_yaml_path, profile )
end

#load_page(path, options = {}) ⇒ Object

compat with awestruct 0.2.x



481
482
483
484
485
486
487
488
# File 'lib/awestruct/engine.rb', line 481

def load_page(path, options={})
  page = @site_page_loader.load_page( path )
  if ( options[:relative_path] )
    fixed_relative_path = ( options[:relative_path].nil? ? nil : File.join( '', options[:relative_path] ) )
    page.relative_path = fixed_relative_path
  end
  page
end

#load_pagesObject



352
353
354
355
356
357
# File 'lib/awestruct/engine.rb', line 352

def load_pages
  $LOG.debug "layout_page_loader.load_all :post" if $LOG.debug?
  @layout_page_loader.load_all( :post )
  $LOG.debug "site_page_loader.load_all :inline" if $LOG.debug?
  @site_page_loader.load_all( :inline )
end

#load_pipelineObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/awestruct/engine.rb', line 245

def load_pipeline
  ext_dir = File.join( site.config.extension_dir )
  pipeline_file = File.join( ext_dir, 'pipeline.rb' )
  if ( File.exists?( pipeline_file ) )
    p = eval(File.read( pipeline_file ), nil, pipeline_file, 1)
    p.before_all_extensions.each do |e|
      pipeline.add_before_extension( e )
    end
    p.extensions.each do |e|
      pipeline.extension( e )
    end
    p.after_all_extensions.each do |e|
      pipeline.add_after_extension( e )
    end
    p.helpers.each do |h|
      pipeline.helper( h )
    end
    p.transformers.each do |t|
      pipeline.transformer( t )
    end
    p.after_generation_extensions.each do |e|
      pipeline.add_after_generation_extension( e )
    end
  end
end

#load_site_page(relative_path) ⇒ Object



490
491
492
# File 'lib/awestruct/engine.rb', line 490

def load_site_page(relative_path)
  load_page( File.join( site.config.dir, relative_path ) )
end

#load_site_yaml(yaml_path, profile = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/awestruct/engine.rb', line 143

def load_site_yaml(yaml_path, profile = nil)
  if ( File.exist?( yaml_path ) )
    begin
      data = YAML.load( ERB.new(File.read( yaml_path, :encoding => 'bom|utf-8' )).result )
      if ( profile )
        # JP: Interpolation now turned off by default, turn it per page if needed
        site.interpolate = false
        profile_data = {}
        data.each do |k,v|
          if ( ( k == 'profiles' ) && ( ! profile.nil? ) )
            profile_data = ( v[profile] || {} )
          else
            site.send( "#{k}=", merge_data( site.send( "#{k}" ), v ) )
          end
        end if data
        site.profile = profile
        profile_data.each do |k,v|
          site.send( "#{k}=", merge_data( site.send( "#{k}" ), v ) )
        end
      else
        data.each do |k,v|
          site.send( "#{k}=", v )
        end if data
      end
    rescue Exception => e
      ExceptionHelper.log_building_error e, yaml_path
      ExceptionHelper.mark_failed
    end
  end
end

#load_user_site_yaml(profile = nil) ⇒ Object



132
133
134
135
# File 'lib/awestruct/engine.rb', line 132

def load_user_site_yaml(profile = nil)
  site_yaml_path = File.join( site.config.config_dir, 'site.yml' )
  load_site_yaml( site_yaml_path, profile )
end

#load_yaml(yaml_path) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/awestruct/engine.rb', line 174

def load_yaml(yaml_path)
  begin
    data = YAML.load( ERB.new(File.read( yaml_path )).result )
  rescue Exception => e
    ExceptionHelper.log_building_error e, yaml_path
    ExceptionHelper.mark_failed
  end
  name = File.basename( yaml_path, '.yml' )
  site.send( "#{name}=", massage_yaml( data ) )
end

#load_yamlsObject



137
138
139
140
141
# File 'lib/awestruct/engine.rb', line 137

def load_yamls
  Dir[ File.join( site.config.config_dir, '*.yml' ) ].each do |yaml_path|
    load_yaml( yaml_path ) unless ( File.basename( yaml_path ) == 'site.yml' )
  end
end

#massage_yaml(obj) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/awestruct/engine.rb', line 205

def massage_yaml(obj)
  result = obj
  case ( obj )
    when Hash
      result = {}
      obj.each do |k,v|
        result[k] = massage_yaml(v)
      end
      result = AStruct.new( result ).cascade_for_nils!
    when Array
      result = []
      obj.each do |v|
        result << massage_yaml(v)
      end
  end
  result
end

#merge_data(existing, new) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/awestruct/engine.rb', line 185

def merge_data(existing, new)
  if existing.kind_of? Hash
    result = existing.inject({}) do |merged, (k,v)|
      if new.has_key? k
        if v.kind_of? Hash
          merged[k] = merge_data(v, new.delete(k))
        else
          merged[k] = new.delete(k)
        end
      else
        merged[k] = v
      end
      merged
    end
    result.merge new
  else
    new
  end
end

#page_by_source_path(path) ⇒ Object

path - relative to output dir



403
404
405
406
407
408
409
410
411
# File 'lib/awestruct/engine.rb', line 403

def page_by_source_path(path)
  if (path.include? '_layout')
    site.layouts.find { |p| p.source_path.to_s.include? path }
  elsif (path.include? '_partial')
    site.partials.find { |p| p.source_path.to_s.include? path }
  else
    site.pages.find { |p| p.source_path.to_s.include? path }
  end
end

#page_dependencies(page) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/awestruct/engine.rb', line 435

def page_dependencies(page)
  regen_pages = Set.new [ page ] 
  regen_pages.merge page.dependencies.dependents

  # doing this in case someone has used key dependencies
  page.dependencies.key_dependents.each do |kd|
    if kd.is_a? Page
      regen_pages << kd
    end    
  end

  temp_set = Set.new
  regen_pages.each do |p|
    temp_set.merge find_transitive_dependents(page) 
  end

  regen_pages.merge temp_set
end

#run(profile, base_url, default_base_url, force = false, generate = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/awestruct/engine.rb', line 56

def run(profile, base_url, default_base_url, force=false, generate=true)
  $LOG.debug 'adjust_load_path' if $LOG.debug?
  adjust_load_path
  $LOG.debug 'load_default_site_yaml' if $LOG.debug?
  load_default_site_yaml( profile )
  $LOG.debug 'load_user_site_yaml -- profile' if $LOG.debug?
  load_user_site_yaml( profile )
  $LOG.debug 'set_base_url' if $LOG.debug?
  set_base_url( base_url, default_base_url )
  $LOG.debug 'load_yamls' if $LOG.debug?
  load_yamls
  $LOG.debug 'load_pipeline' if $LOG.debug?
  load_pipeline
  $LOG.debug 'load_pages' if $LOG.debug?
  load_pages
  $LOG.debug 'execute_pipeline' if $LOG.debug?
  $LOG.info 'Excecuting pipeline...' if $LOG.info?
  execute_pipeline(false)
  $LOG.debug 'configure_compass' if $LOG.debug?
  configure_compass
  $LOG.debug 'set_urls' if $LOG.debug?
  set_urls( site.pages )
  $LOG.debug 'build_page_index' if $LOG.debug?
  build_page_index

  if ( generate )
    $LOG.debug 'generate_output' if $LOG.debug?
    $LOG.info 'Generating pages...' if $LOG.info?
    generate_output
  end
  return Awestruct::ExceptionHelper::EXITCODES[:success]
end

#run_auto_for_non_page(file, generate = true) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/awestruct/engine.rb', line 454

def run_auto_for_non_page(file, generate = true)
  if File.extname(file) == '.rb'
    load file
  end
  @pipeline = Pipeline.new
  load_yamls
  load_pipeline
  execute_pipeline(true)

  if ( generate )
    site.pages.each do |p|
      generate_page_internal(p)
    end
  end
  site.pages
end

#set_base_url(base_url, default_base_url) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/awestruct/engine.rb', line 110

def set_base_url(base_url, default_base_url)
  if ( base_url )
    site.base_url = base_url
  end

  if ( site.base_url.nil? )
    site.base_url = default_base_url
  end

  if ( site.base_url )
    if ( site.base_url =~ /^(.*)\/$/ )
      site.base_url = $1
    end
  end

end

#set_urls(pages) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/awestruct/engine.rb', line 230

def set_urls(pages)
  pages.each do |page|
    $LOG.debug "relative_source_path #{page.relative_source_path}" if $LOG.debug?
    page_path = page.output_path
    if ( page_path =~ /^\// )
      page.url = page_path
    else
      page.url = "/#{page_path}"
    end
    if ( page.url =~ /^(.*\/)index.html$/ )
      page.url = $1
    end
  end
end