Class: Bones::Cache

Inherits:
Object show all
Defined in:
lib/bones/cache.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Cache

Returns a new instance of Cache.



93
94
95
96
97
98
99
100
101
102
# File 'lib/bones/cache.rb', line 93

def initialize(options=nil)
  self.options = case options
  when Hash
    Options.new.merge(options)
  when Options
    options
  else  
    Options.process(options)
  end  
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



91
92
93
# File 'lib/bones/cache.rb', line 91

def options
  @options
end

Class Method Details

.run(options = nil) ⇒ Object



104
105
106
# File 'lib/bones/cache.rb', line 104

def self.run(options=nil)
  new(options).run
end

Instance Method Details

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



188
189
190
# File 'lib/bones/cache.rb', line 188

def generate_mock_request(path, options={})
  Rack::Request.new(Rack::MockRequest.env_for(path, options))
end

#normalize_url(path) ⇒ Object

Fixes the given URL path to begin at given base. In addition, the .html extension will be added if the path is a page.

For example, if the base is /some_folder,

normalize_url('/page_path', '/some_folder')
# => /some_folder/page_path.html


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/bones/cache.rb', line 164

def normalize_url(path)
  @known_pairs ||= {}
  @public_directories_regex ||= Regexp.new(Bones.public_directories.join('|'))
  
  if v = @known_pairs[path]
    return v
  else
    value = case
    when path =~ /^(\w{3,}:\/\/|mailto)/
      # don't do anything to this type of URL
      return path
    when path =~ @public_directories_regex
      path
    when File.directory?('pages' / path)
      path
    else
      # don't add .html if there's already an extension
      path =~ /\..+/ ? path : path + '.html'
    end
    
    @known_pairs[path] = options.base / value
  end    
end

#process_page(page) ⇒ Object



143
144
145
146
147
148
# File 'lib/bones/cache.rb', line 143

def process_page(page)
  request = generate_mock_request(page)
  template = Bones::Template.new(Bones::Template.template_for_request(request))
  template.request = request
  process_template(template.compile)
end

#process_template(result) ⇒ Object



150
151
152
153
154
155
# File 'lib/bones/cache.rb', line 150

def process_template(result)
  result.gsub(/(href|src|action|url)(="|\()([-A-Za-z0-9_\.\/]+)([^:]*?)("|\))/) do |match|
    property, url, params = $1, normalize_url(original_url = $3), $4
    property =~ /url/ ? 'url(%s%s)' % [url, params] : '%s="%s%s"' % [property, url, params]
  end      
end

#runObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bones/cache.rb', line 108

def run
  version = options.versioned? ? options.release.versioned_directory_name : nil
  
  # Process each page
  Dir.chdir(Bones.root) do
    puts "** Note: No files/directories will be modified (noop flag)" if options.noop        
    puts "** Writing to: #{options.destination}"
    puts "** Using base: #{options.base}" unless options.base.blank?
    
    pages = Bones.pages
    total = pages.length
    pages.each_with_index do |page, index|
      print  "\r     %-70s (%4d/%4d)" % [[version, page].compact.join('/') + '.html', index + 1, total]
      $stdout.flush
      
      result = process_page(page)
      path   = options.destination / page + '.html'
      
      unless options.noop
        FileUtils.mkdir_p(File.dirname(path))
        File.open(path, 'w') { |f| f.write(result) }
      end  
    end
    
    puts "\n"

    if options.release?  
      puts "** Copying public files"
      options.release.copy_public_directories unless options.noop
    end
  end

  puts "** Done."      
end