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.



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

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.



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

def pipeline
  @pipeline
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

Class Method Details

.instanceObject



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

def self.instance
  @instance
end

.instance=(engine) ⇒ Object



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

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

Instance Method Details

#adjust_load_pathObject



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

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

#configObject



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

def config
  site.config
end

#configure_compassObject



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

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



250
251
252
# File 'lib/awestruct/engine.rb', line 250

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

#execute_pipelineObject



166
167
168
# File 'lib/awestruct/engine.rb', line 166

def execute_pipeline
  pipeline.execute( site )
end

#find_and_load_site_page(simple_path) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/awestruct/engine.rb', line 239

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



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

def generate_output
  @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
      puts "Up-to-date: #{generated_path}"
    end
  end
end

#generate_page(page, generated_path) ⇒ Object



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

def generate_page(page, generated_path)
  puts "Generating: #{generated_path}"
  FileUtils.mkdir_p( File.dirname( generated_path ) )
  File.open( generated_path, 'w' ) do |file|
    file << page.rendered_content
  end
end

#generate_page_by_output_path(path) ⇒ Object



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

def generate_page_by_output_path(path)
  full_path = File.join( '', path )
  #puts "regen path #{full_path}"
  page = site.pages.find{|p| p.relative_source_path.to_s==full_path}
  if ( page.nil? )
    #puts "Unable to locate page for #{path}"
  else
    generated_path = File.join( site.config.output_dir, page.output_path )
    generate_page(page, generated_path)
  end
end

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

compat with awestruct 0.2.x



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

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



184
185
186
187
# File 'lib/awestruct/engine.rb', line 184

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

#load_pipelineObject



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

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



235
236
237
# File 'lib/awestruct/engine.rb', line 235

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

#load_site_yaml(profile) ⇒ Object



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

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



103
104
105
106
107
# File 'lib/awestruct/engine.rb', line 103

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



97
98
99
100
101
# File 'lib/awestruct/engine.rb', line 97

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



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

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



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

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

#set_base_url(base_url, default_base_url) ⇒ Object



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/awestruct/engine.rb', line 134

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