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.



36
37
38
39
40
41
42
# File 'lib/awestruct/engine.rb', line 36

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 )
end

Instance Attribute Details

#pipelineObject (readonly)

Returns the value of attribute pipeline.



26
27
28
# File 'lib/awestruct/engine.rb', line 26

def pipeline
  @pipeline
end

#siteObject (readonly)

Returns the value of attribute site.



25
26
27
# File 'lib/awestruct/engine.rb', line 25

def site
  @site
end

Class Method Details

.instanceObject



28
29
30
# File 'lib/awestruct/engine.rb', line 28

def self.instance
  @instance
end

.instance=(engine) ⇒ Object



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

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

Instance Method Details

#adjust_load_pathObject



187
188
189
190
191
192
# File 'lib/awestruct/engine.rb', line 187

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



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/awestruct/engine.rb', line 75

def build_page_index
  site.pages_by_relative_source_path = {}
  site.pages.each do |p|
    if ( p.relative_source_path )
      site.pages_by_relative_source_path[ p.relative_source_path ] = p
    end
  end
  site.layouts.each do |p|
    if ( p.relative_source_path )
      site.pages_by_relative_source_path[ p.relative_source_path ] = p
    end
  end
end

#compress_css?Boolean

Returns:

  • (Boolean)


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

def compress_css?
  site.key?(:compass_output_style) ? site.compass_output_style.to_sym : site.profile.eql?('production') ? :compressed : :expanded
end

#configObject



44
45
46
# File 'lib/awestruct/engine.rb', line 44

def config
  site.config
end

#configure_compassObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/awestruct/engine.rb', line 232

def configure_compass
  Compass.configuration.project_type    = :standalone
  Compass.configuration.project_path    = site.config.dir
  Compass.configuration.sass_dir        = 'stylesheets'
  
  site.images_dir      = File.join( site.config.output_dir, 'images' )
  site.fonts_dir       = File.join( site.config.output_dir, 'fonts' )
  site.stylesheets_dir = File.join( site.config.output_dir, 'stylesheets' )
  site.javascripts_dir = File.join( site.config.output_dir, 'javascripts' )

  Compass.configuration.css_dir         = site.css_dir
  Compass.configuration.javascripts_dir = 'javascripts'
  Compass.configuration.images_dir      = 'images'
  Compass.configuration.fonts_dir       = 'fonts'
  Compass.configuration.line_comments   = include_line_comments?
  Compass.configuration.output_style    = compress_css?

  # port old style configuration to new Tilt-based configuration
  # TODO consider deprecating the old config mechanism and move to default-site.yml
  [:scss, :sass].each do |sass_ext|
    if !site.key? sass_ext
      sass_config = {}
      site[sass_ext] = sass_config
    else
      sass_config = site[sass_ext]
    end

    if !sass_config.has_key?(:line_numbers) || site.profile.eql?('production')
      sass_config[:line_numbers] = Compass.configuration.line_comments
    end

    if !sass_config.has_key?(:style) || site.profile.eql?('production')
      sass_config[:style] = Compass.configuration.output_style
    end
  end
end

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



405
406
407
# File 'lib/awestruct/engine.rb', line 405

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

#execute_pipelineObject



226
227
228
229
230
# File 'lib/awestruct/engine.rb', line 226

def execute_pipeline
  FileUtils.mkdir_p( site.config.output_dir )
  FileUtils.mkdir_p( site.config.tmp_dir )
  pipeline.execute( site )
end

#find_and_load_site_page(simple_path) ⇒ Object



394
395
396
397
398
399
400
401
402
403
# File 'lib/awestruct/engine.rb', line 394

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] )
  relative_path = path_name.relative_path_from( dir_pathname ).to_s
  load_page( candidates[0] )
end

#generate_outputObject



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/awestruct/engine.rb', line 284

def generate_output
  FileUtils.mkdir_p( site.config.output_dir )
  @site.pages.each do |page|
    generated_path = File.join( site.config.output_dir, page.output_path )
    if ( page.stale_output?( generated_path ) )
      generate_page( page, generated_path )
    else
      generate_page( page, generated_path, false )
    end
  end
end

#generate_page(page, generated_path, produce_output = true) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/awestruct/engine.rb', line 296

def generate_page(page, generated_path, produce_output=true)
  if ( produce_output )
    $LOG.debug "Generating: #{generated_path}" if $LOG.debug?
    FileUtils.mkdir_p( File.dirname( generated_path ) )

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

    File.open( generated_path, 'wb' ) do |file|
      file << c
    end
  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
  end
end

#generate_page_by_output_path(path) ⇒ Object



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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/awestruct/engine.rb', line 317

def generate_page_by_output_path(path)
  full_path = File.join( '', path )
  page = site.pages.find{ |p| p.relative_source_path.to_s == full_path } ||
         site.layouts.find{ |p| p.relative_source_path.to_s == full_path }
  return if page.nil?

  if !page.output_path.nil?
    generate_page_internal(page)
  end

  pages = [] << page

  pages.each{|page|
    $LOG.debug "--------------------" if $LOG.debug?
    $LOG.debug "Page: #{page.output_path} #{page.relative_source_path} #{page.__is_layout ? 'Layout':''}" if $LOG.debug?
    $LOG.debug "Detected change in content (#{page.dependencies.content_hash})" if page.dependencies.has_changed_content if $LOG.debug?
    $LOG.debug "!! Detected change in front matter. To fully reflect the change you'll need to restart Awestruct (#{page.dependencies.key_hash})" if page.dependencies.has_changed_keys if $LOG.debug?
    $LOG.debug "No changes detected" unless page.dependencies.has_changed_content or page.dependencies.has_changed_keys if $LOG.debug?
    $LOG.debug "Dependencies Matrix: (non unique source path)" if $LOG.debug?
    $LOG.debug "\t Outgoing dependencies:" if $LOG.debug?
    $LOG.debug "\t\t Content -> #{page.dependencies.dependencies.size}" if $LOG.debug?
    $LOG.debug "\t\t Key     -> #{page.dependencies.key_dependencies.size}" if $LOG.debug?
    $LOG.debug "\t Incoming dependencies:" if $LOG.debug?
    $LOG.debug "\t\t Content <- #{page.dependencies.dependents.size}" if $LOG.debug?
    $LOG.debug "\t\t Key     <- #{page.dependencies.key_dependents.size}" if $LOG.debug?
    $LOG.debug "--------------------" if $LOG.debug?
  }

  regen_pages = Set.new
  if page.dependencies.has_changed_content or page.__is_layout
    regen_pages += page.dependencies.dependents
  end
  regen_pages = regen_pages.sort do |x, y|
    xf = "#{@site.dir}#{x.relative_source_path}"
    yf = "#{@site.dir}#{y.relative_source_path}"
    xt = 0
    yt = 0
    xt = File.mtime(xf).to_i if File.exist? xf
    yt = File.mtime(yf).to_i if File.exist? yf

    yt <=> xt
  end

  $LOG.debug "Starting regeneration of content dependent pages:" if regen_pages.size > 0 && $LOG.debug?
  regen_pages.each{|x| $LOG.info x.output_path if $LOG.info?}

  regen_pages.each do |p|
    generate_page_internal(p)
  end
end

#generate_page_internal(p) ⇒ Object



368
369
370
371
372
373
# File 'lib/awestruct/engine.rb', line 368

def generate_page_internal(p)
    unless ( p.output_path.nil? || p.__is_layout )
      generated_path = File.join( site.config.output_dir, p.output_path )
      generate_page( p, generated_path )
    end
end

#include_line_comments?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/awestruct/engine.rb', line 269

def include_line_comments?
  site.key?(:compass_line_comments) ? !!site.compass_line_comments : !site.profile.eql?('production')
end

#load_default_site_yamlObject



106
107
108
109
110
111
112
113
114
# File 'lib/awestruct/engine.rb', line 106

def load_default_site_yaml
  default_site_yaml = File.join( File.dirname( __FILE__ ), 'config', 'default-site.yml' )
  if ( File.exist?( default_site_yaml ) )
    data = YAML.load( File.read( default_site_yaml ) )
    data.each do |k,v|
      site.send( "#{k}=", v )
    end if data
  end
end

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

compat with awestruct 0.2.x



381
382
383
384
385
386
387
388
# File 'lib/awestruct/engine.rb', line 381

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



277
278
279
280
281
282
# File 'lib/awestruct/engine.rb', line 277

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/awestruct/engine.rb', line 209

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.extensions.each do |e|
      pipeline.extension( e )
    end
    p.helpers.each do |h|
      pipeline.helper( h )
    end
    p.transformers.each do |t|
      pipeline.transformer( t )
    end
  end
end

#load_site_page(relative_path) ⇒ Object



390
391
392
# File 'lib/awestruct/engine.rb', line 390

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

#load_site_yaml(profile) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/awestruct/engine.rb', line 116

def load_site_yaml(profile)
  site_yaml = File.join( site.config.config_dir, 'site.yml' )
  if ( File.exist?( site_yaml ) )
    data = YAML.load( File.read( site_yaml ) )
    site.interpolate = true
    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
  end
end

#load_yaml(yaml_path) ⇒ Object



143
144
145
146
147
# File 'lib/awestruct/engine.rb', line 143

def load_yaml(yaml_path)
  data = YAML.load( File.read( yaml_path ) )
  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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/awestruct/engine.rb', line 169

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/awestruct/engine.rb', line 149

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/awestruct/engine.rb', line 48

def run(profile, base_url, default_base_url, force=false)
  $LOG.debug "adjust_load_path" if $LOG.debug?
  adjust_load_path
  $LOG.debug "load_default_site_yaml" if $LOG.debug?
  load_default_site_yaml
  $LOG.debug "load_site_yaml -- profile" if $LOG.debug?
  load_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?
  execute_pipeline
  $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
  $LOG.debug "generate_output" if $LOG.debug?
  generate_output
end

#set_base_url(base_url, default_base_url) ⇒ Object



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

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/awestruct/engine.rb', line 194

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