middleman-more 3.1.0.beta.1 → 3.1.0.beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,58 +1,42 @@
1
- # Extensions namespace
2
- module Middleman
3
- module Extensions
1
+ # Automatic Image Sizes extension
2
+ class Middleman::Extensions::AutomaticImageSizes < ::Middleman::Extension
4
3
 
5
- # Automatic Image Sizes extension
6
- module AutomaticImageSizes
4
+ def initialize(app, options_hash={}, &block)
5
+ super
7
6
 
8
- # Setup extension
9
- class << self
10
-
11
- # Once registered
12
- def registered(app)
13
- # Include 3rd-party fastimage library
14
- require "middleman-more/extensions/automatic_image_sizes/fastimage"
15
-
16
- # Include methods
17
- app.send :include, InstanceMethods
18
- end
19
-
20
- alias :included :registered
21
- end
22
-
23
- # Automatic Image Sizes Instance Methods
24
- module InstanceMethods
25
-
26
- # Override default image_tag helper to automatically calculate and include
27
- # image dimensions.
28
- #
29
- # @param [String] path
30
- # @param [Hash] params
31
- # @return [String]
32
- def image_tag(path, params={})
33
- if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
34
- params[:alt] ||= ""
35
-
36
- real_path = path
37
- real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
38
- full_path = File.join(source_dir, real_path)
7
+ # Include 3rd-party fastimage library
8
+ require "middleman-more/extensions/automatic_image_sizes/fastimage"
9
+ end
39
10
 
40
- if File.exists?(full_path)
41
- begin
42
- width, height = ::FastImage.size(full_path, :raise_on_failure => true)
43
- params[:width] = width
44
- params[:height] = height
45
- rescue FastImage::UnknownImageType
46
- # No message, it's just not supported
47
- rescue
48
- warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
49
- end
50
- end
11
+ helpers do
12
+ # Override default image_tag helper to automatically calculate and include
13
+ # image dimensions.
14
+ #
15
+ # @param [String] path
16
+ # @param [Hash] params
17
+ # @return [String]
18
+ def image_tag(path, params={})
19
+ if !params.has_key?(:width) && !params.has_key?(:height) && !path.include?("://")
20
+ params[:alt] ||= ""
21
+
22
+ real_path = path
23
+ real_path = File.join(images_dir, real_path) unless real_path.start_with?('/')
24
+ full_path = File.join(source_dir, real_path)
25
+
26
+ if File.exists?(full_path)
27
+ begin
28
+ width, height = ::FastImage.size(full_path, :raise_on_failure => true)
29
+ params[:width] = width
30
+ params[:height] = height
31
+ rescue FastImage::UnknownImageType
32
+ # No message, it's just not supported
33
+ rescue
34
+ warn "Couldn't determine dimensions for image #{path}: #{$!.message}"
51
35
  end
52
-
53
- super(path, params)
54
36
  end
55
37
  end
38
+
39
+ super(path, params)
56
40
  end
57
41
  end
58
42
  end
@@ -1,70 +1,55 @@
1
- # Extension namespace
2
- module Middleman
3
- module Extensions
4
-
5
- # The Cache Buster extension
6
- module CacheBuster
7
-
8
- # Setup extension
9
- class << self
10
-
11
- # Once registered
12
- def registered(app)
13
- # Add instance methods to context
14
- app.send :include, InstanceMethods
15
-
16
- # After compass is setup, make it use the registered cache buster
17
- app.compass_config do |config|
18
- config.asset_cache_buster do |path, real_path|
19
- real_path = real_path.path if real_path.is_a? File
20
- real_path = real_path.gsub(File.join(root, build_dir), source)
21
- if File.readable?(real_path)
22
- File.mtime(real_path).strftime("%s")
23
- else
24
- logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
25
- end
26
- end
27
- end
1
+ # The Cache Buster extension
2
+ class Middleman::Extensions::CacheBuster < ::Middleman::Extension
3
+
4
+ def initialize(app, options_hash={}, &block)
5
+ super
6
+
7
+ # After compass is setup, make it use the registered cache buster
8
+ app.compass_config do |config|
9
+ config.asset_cache_buster do |path, real_path|
10
+ real_path = real_path.path if real_path.is_a? File
11
+ real_path = real_path.gsub(File.join(root, build_dir), source)
12
+ if File.readable?(real_path)
13
+ File.mtime(real_path).strftime("%s")
14
+ else
15
+ logger.warn "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
28
16
  end
29
- alias :included :registered
30
17
  end
18
+ end
19
+ end
31
20
 
32
- # Cache buster instance methods
33
- module InstanceMethods
21
+ helpers do
22
+ # asset_url override if we're using cache busting
23
+ # @param [String] path
24
+ # @param [String] prefix
25
+ def asset_url(path, prefix="")
26
+ http_path = super
27
+
28
+ if http_path.include?("://") || !%w(.css .png .jpg .jpeg .svg .svgz .js .gif).include?(File.extname(http_path))
29
+ http_path
30
+ else
31
+ if respond_to?(:http_images_path) && prefix == http_images_path
32
+ prefix = images_dir
33
+ end
34
34
 
35
- # asset_url override if we're using cache busting
36
- # @param [String] path
37
- # @param [String] prefix
38
- def asset_url(path, prefix="")
39
- http_path = super
35
+ real_path_static = File.join(prefix, path)
40
36
 
41
- if http_path.include?("://") || !%w(.css .png .jpg .jpeg .svg .svgz .js .gif).include?(File.extname(http_path))
42
- http_path
37
+ if build?
38
+ real_path_dynamic = File.join(build_dir, prefix, path)
39
+ real_path_dynamic = File.expand_path(real_path_dynamic, root)
40
+ http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
41
+ elsif resource = sitemap.find_resource_by_path(real_path_static)
42
+ if !resource.template?
43
+ http_path << "?" + File.mtime(resource.source_file).strftime("%s")
43
44
  else
44
- if respond_to?(:http_images_path) && prefix == http_images_path
45
- prefix = images_dir
46
- end
47
-
48
- real_path_static = File.join(prefix, path)
49
-
50
- if build?
51
- real_path_dynamic = File.join(build_dir, prefix, path)
52
- real_path_dynamic = File.expand_path(real_path_dynamic, root)
53
- http_path << "?" + File.mtime(real_path_dynamic).strftime("%s") if File.readable?(real_path_dynamic)
54
- elsif resource = sitemap.find_resource_by_path(real_path_static)
55
- if !resource.template?
56
- http_path << "?" + File.mtime(resource.source_file).strftime("%s")
57
- else
58
- # It's a template, possible with partials. We can't really
59
- # know when it's updated, so generate fresh cache buster every
60
- # time during developement
61
- http_path << "?" + Time.now.strftime("%s")
62
- end
63
- end
64
-
65
- http_path
45
+ # It's a template, possible with partials. We can't really
46
+ # know when it's updated, so generate fresh cache buster every
47
+ # time during developement
48
+ http_path << "?" + Time.now.strftime("%s")
66
49
  end
67
50
  end
51
+
52
+ http_path
68
53
  end
69
54
  end
70
55
  end
@@ -1,54 +1,24 @@
1
- # Extensions namespace
2
- module Middleman
3
- module Extensions
4
-
5
- # Directory Indexes extension
6
- module DirectoryIndexes
7
-
8
- # Setup extension
9
- class << self
10
-
11
- # Once registered
12
- def registered(app)
13
- app.after_configuration do
14
- sitemap.register_resource_list_manipulator(
15
- :directory_indexes,
16
- DirectoryIndexManager.new(self)
17
- )
18
- end
19
- end
20
-
21
- alias :included :registered
22
- end
23
-
24
- # Central class for managing the directory indexes extension
25
- class DirectoryIndexManager
26
- def initialize(app)
27
- @app = app
28
- end
29
-
30
- # Update the main sitemap resource list
31
- # @return [void]
32
- def manipulate_resource_list(resources)
33
- index_file = @app.index_file
34
- new_index_path = "/#{index_file}"
35
-
36
- resources.each do |resource|
37
- # Check if it would be pointless to reroute
38
- next if resource.destination_path == index_file ||
39
- resource.destination_path.end_with?(new_index_path) ||
40
- File.extname(index_file) != resource.ext
41
-
42
- # Check if frontmatter turns directory_index off
43
- next if resource.data[:directory_index] == false
44
-
45
- # Check if file metadata (options set by "page" in config.rb) turns directory_index off
46
- next if resource.metadata[:options][:directory_index] == false
47
-
48
- resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path
49
- end
50
- end
51
- end
1
+ # Directory Indexes extension
2
+ class Middleman::Extensions::DirectoryIndexes < ::Middleman::Extension
3
+ # Update the main sitemap resource list
4
+ # @return [void]
5
+ def manipulate_resource_list(resources)
6
+ index_file = app.index_file
7
+ new_index_path = "/#{index_file}"
8
+
9
+ resources.each do |resource|
10
+ # Check if it would be pointless to reroute
11
+ next if resource.destination_path == index_file ||
12
+ resource.destination_path.end_with?(new_index_path) ||
13
+ File.extname(index_file) != resource.ext
14
+
15
+ # Check if frontmatter turns directory_index off
16
+ next if resource.data[:directory_index] == false
17
+
18
+ # Check if file metadata (options set by "page" in config.rb) turns directory_index off
19
+ next if resource.metadata[:options][:directory_index] == false
20
+
21
+ resource.destination_path = resource.destination_path.chomp(File.extname(index_file)) + new_index_path
52
22
  end
53
23
  end
54
24
  end
@@ -1,73 +1,70 @@
1
- require 'zlib'
2
- require 'stringio'
3
- require 'find'
1
+ # This extension Gzips assets and pages when building.
2
+ # Gzipped assets and pages can be served directly by Apache or
3
+ # Nginx with the proper configuration, and pre-zipping means that we
4
+ # can use a more agressive compression level at no CPU cost per request.
5
+ #
6
+ # Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache
7
+ # to serve your Gzipped files whenever the normal (non-.gz) filename is requested.
8
+ #
9
+ # Pass the :exts options to customize which file extensions get zipped (defaults
10
+ # to .html, .htm, .js and .css.
11
+ #
12
+ class Middleman::Extensions::Gzip < ::Middleman::Extension
13
+ option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.'
4
14
 
5
- module Middleman::Extensions
15
+ def initialize(app, options_hash={})
16
+ super
17
+
18
+ require 'zlib'
19
+ require 'stringio'
20
+ require 'find'
6
21
 
7
- # This extension Gzips assets and pages when building.
8
- # Gzipped assets and pages can be served directly by Apache or
9
- # Nginx with the proper configuration, and pre-zipping means that we
10
- # can use a more agressive compression level at no CPU cost per request.
11
- #
12
- # Use Nginx's gzip_static directive, or AddEncoding and mod_rewrite in Apache
13
- # to serve your Gzipped files whenever the normal (non-.gz) filename is requested.
14
- #
15
- # Pass the :exts options to customize which file extensions get zipped (defaults
16
- # to .html, .htm, .js and .css.
17
- #
18
- class Gzip < ::Middleman::Extension
19
- option :exts, %w(.js .css .html .htm), 'File extensions to Gzip when building.'
22
+ gzip_ext = self
20
23
 
21
- def initialize(app, options_hash={})
22
- super
24
+ app.after_build do |builder|
25
+ paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir)
26
+ paths.each do |path|
27
+ next unless gzip_ext.options.exts.include? path.extname
23
28
 
24
- gzip_ext = self
29
+ output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s)
25
30
 
26
- app.after_build do |builder|
27
- paths = ::Middleman::Util.all_files_under(self.class.inst.build_dir)
28
- paths.each do |path|
29
- next unless gzip_ext.options.exts.include? path.extname
30
-
31
- output_filename, old_size, new_size = gzip_ext.gzip_file(path.to_s)
32
-
33
- if output_filename
34
- size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
35
- old_locale = I18n.locale
36
- I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
37
- builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
38
- I18n.locale = old_locale
39
- end
31
+ if output_filename
32
+ size_change_word = (old_size - new_size) > 0 ? 'smaller' : 'larger'
33
+ old_locale = I18n.locale
34
+ I18n.locale = :en # use the english localizations for printing out file sizes to make sure the localizations exist
35
+ builder.say_status :gzip, "#{output_filename} (#{number_to_human_size((old_size - new_size).abs)} #{size_change_word})"
36
+ I18n.locale = old_locale
40
37
  end
41
38
  end
42
39
  end
40
+ end
43
41
 
44
- def gzip_file(path)
45
- input_file = File.open(path, 'rb').read
46
- output_filename = path + '.gz'
47
- input_file_time = File.mtime(path)
42
+ def gzip_file(path)
43
+ input_file = File.open(path, 'rb').read
44
+ output_filename = path + '.gz'
45
+ input_file_time = File.mtime(path)
48
46
 
49
- # Check if the right file's already there
50
- if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time
51
- return
52
- end
47
+ # Check if the right file's already there
48
+ if File.exist?(output_filename) && File.mtime(output_filename) == input_file_time
49
+ return
50
+ end
53
51
 
54
- File.open(output_filename, 'wb') do |f|
55
- gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
56
- gz.mtime = input_file_time.to_i
57
- gz.write input_file
58
- gz.close
59
- end
52
+ File.open(output_filename, 'wb') do |f|
53
+ gz = Zlib::GzipWriter.new(f, Zlib::BEST_COMPRESSION)
54
+ gz.mtime = input_file_time.to_i
55
+ gz.write input_file
56
+ gz.close
57
+ end
60
58
 
61
- # Make the file times match, both for Nginx's gzip_static extension
62
- # and so we can ID existing files. Also, so even if the GZ files are
63
- # wiped out by build --clean and recreated, we won't rsync them over
64
- # again because they'll end up with the same mtime.
65
- File.utime(File.atime(output_filename), input_file_time, output_filename)
59
+ # Make the file times match, both for Nginx's gzip_static extension
60
+ # and so we can ID existing files. Also, so even if the GZ files are
61
+ # wiped out by build --clean and recreated, we won't rsync them over
62
+ # again because they'll end up with the same mtime.
63
+ File.utime(File.atime(output_filename), input_file_time, output_filename)
66
64
 
67
- old_size = File.size(path)
68
- new_size = File.size(output_filename)
65
+ old_size = File.size(path)
66
+ new_size = File.size(output_filename)
69
67
 
70
- [output_filename, old_size, new_size]
71
- end
68
+ [output_filename, old_size, new_size]
72
69
  end
73
70
  end