Class: Bones::Cache
Defined Under Namespace
Classes: Options
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #generate_mock_request(path, options = {}) ⇒ Object
-
#initialize(options = nil) ⇒ Cache
constructor
A new instance of Cache.
-
#normalize_url(path) ⇒ Object
Fixes the given URL path to begin at given base.
- #process_page(page) ⇒ Object
- #process_template(result) ⇒ Object
- #run ⇒ Object
Constructor Details
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
91 92 93 |
# File 'lib/bones/cache.rb', line 91 def @options end |
Class Method Details
.run(options = nil) ⇒ Object
104 105 106 |
# File 'lib/bones/cache.rb', line 104 def self.run(=nil) new().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, ={}) Rack::Request.new(Rack::MockRequest.env_for(path, )) 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] = .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 |
#run ⇒ Object
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 = .versioned? ? .release.versioned_directory_name : nil # Process each page Dir.chdir(Bones.root) do puts "** Note: No files/directories will be modified (noop flag)" if .noop puts "** Writing to: #{.destination}" puts "** Using base: #{.base}" unless .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 = .destination / page + '.html' unless .noop FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w') { |f| f.write(result) } end end puts "\n" if .release? puts "** Copying public files" .release.copy_public_directories unless .noop end end puts "** Done." end |