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



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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/awestruct/engine.rb', line 62

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

#configObject



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

def config
  site.config
end

#configure_compassObject



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

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.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'
end

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



290
291
292
# File 'lib/awestruct/engine.rb', line 290

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

#execute_pipelineObject



182
183
184
185
# File 'lib/awestruct/engine.rb', line 182

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

#find_and_load_site_page(simple_path) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'lib/awestruct/engine.rb', line 279

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



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/awestruct/engine.rb', line 206

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/awestruct/engine.rb', line 218

def generate_page(page, generated_path, produce_output=true)
  if ( produce_output )
    puts "Generating: #{generated_path}"
    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!
      puts "Cached:     #{generated_path}"
    else
      puts "Analyzing:  #{generated_path}"
      page.rendered_content
    end
  end
end

#generate_page_by_output_path(path) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/awestruct/engine.rb', line 239

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?

  #puts "regen page #{page.inspect}"
  #puts "page.dependents #{page.dependencies.dependents.inspect}"

  regen_pages = Set.new
  regen_pages << page unless ( page.output_path.nil? )
  regen_pages += page.dependencies.dependents

  #puts regen_pages.collect{|e|e.output_path}.inspect
  regen_pages.each do |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
end

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

compat with awestruct 0.2.x



266
267
268
269
270
271
272
273
# File 'lib/awestruct/engine.rb', line 266

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



201
202
203
204
# File 'lib/awestruct/engine.rb', line 201

def load_pages
  @layout_page_loader.load_all( :post )
  @site_page_loader.load_all( :inline )
end

#load_pipelineObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/awestruct/engine.rb', line 165

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 |e|
      pipeline.transformer( e )
    end
  end
end

#load_site_page(relative_path) ⇒ Object



275
276
277
# File 'lib/awestruct/engine.rb', line 275

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

#load_site_yaml(profile) ⇒ Object



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

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}=", v )
      end
    end if data
    site.profile = profile

    profile_data.each do |k,v|
      site.send( "#{k}=", v )
    end
  end
end

#load_yaml(yaml_path) ⇒ Object



119
120
121
122
123
# File 'lib/awestruct/engine.rb', line 119

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



113
114
115
116
117
# File 'lib/awestruct/engine.rb', line 113

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/awestruct/engine.rb', line 125

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/awestruct/engine.rb', line 48

def run(profile, base_url, default_base_url, force=false)
  adjust_load_path
  load_site_yaml(profile)
  set_base_url( base_url, default_base_url )
  load_yamls
  load_pipeline
  load_pages
  execute_pipeline
  configure_compass
  set_urls( site.pages )
  build_page_index
  generate_output
end

#set_base_url(base_url, default_base_url) ⇒ Object



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

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



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

def set_urls(pages)
  pages.each do |page|
    #puts "relative_source_path #{page.relative_source_path}"
    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