Class: Awestruct::Engine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Engine

Returns a new instance of Engine.



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

def initialize(config)
  @dir    = config.input_dir
  @config = config

  @site   = Site.new( config )
  @site.engine = self

  @helpers = []
  @max_yaml_mtime = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#dirObject (readonly)

Returns the value of attribute dir.



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

def dir
  @dir
end

#siteObject (readonly)

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/awestruct/engine.rb', line 122

def create_context(page, content='')
  context = OpenStruct.new( :site=>site, :content=>content )
  context.extend( Awestruct::ContextHelper )
  @helpers.each do |h|
    context.extend( h )
  end
  context.page = page
  class << context
    def interpolate_string(str)
      if site.interpolate
        str = str || ''
        str = str.gsub( /\\/, '\\\\\\\\' )
        str = str.gsub( /\\\\#/, '\\#' )
        str = str.gsub( '@', '\@' )
        str = "%@#{str}@"
        result = instance_eval( str )
        result
      else
        str || ''
      end
    end
    def evaluate_erb(erb)
      erb.result( binding )
    end
  end
  context
end

#find_and_load_site_page(simple_path) ⇒ Object



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

def find_and_load_site_page(simple_path)
  path_glob = File.join( 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( dir )
  path_name = Pathname.new( candidates[0] )
  relative_path = path_name.relative_path_from( dir_pathname ).to_s
  load_page( candidates[0], :relative_path => relative_path )
end

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



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

def generate(profile=nil, base_url=nil, default_base_url=nil, force=false)
  @base_url         = base_url
  @default_base_url = default_base_url
  @max_yaml_mtime = nil
  load_site_yaml(profile)
  load_yamls
  set_base_url
  load_layouts
  load_pages
  load_extensions
  set_urls(site.pages)
  configure_compass
  generate_files(force)
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/awestruct/engine.rb', line 92

def load_page(path, options = {})
  page = nil
  if ( options[:relative_path].nil? )
    #dir_pathname = Pathname.new( dir )
    #path_name = Pathname.new( path )
    #relative_path = path_name.relative_path_from( dir_pathname ).to_s
  end

  fixed_relative_path = ( options[:relative_path].nil? ? nil : File.join( '', options[:relative_path] ) )

  if ( path =~ /\.haml$/ )
    page = HamlFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.erb$/ )
    page = ErbFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.textile$/ )
    page = TextileFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.md$/ )
    page = MarkdownFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.sass$/ )
    page = SassFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.scss$/ )
    page = ScssFile.new( site, path, fixed_relative_path, options )
  elsif ( path =~ /\.org$/ )
    page = OrgModeFile.new( site, path, fixed_relative_path, options )
  elsif ( File.file?( path ) )
    page = VerbatimFile.new( site, path, fixed_relative_path, options )
  end
  page
end

#load_site_page(relative_path) ⇒ Object



88
89
90
# File 'lib/awestruct/engine.rb', line 88

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

#set_urls(pages) ⇒ Object



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

def set_urls(pages)
  pages.each do |page|
    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

#skin_dirObject



58
59
60
# File 'lib/awestruct/engine.rb', line 58

def skin_dir
  @site.skin_dir
end