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,244 +1,235 @@
1
- require 'active_support/core_ext/object/to_query'
2
-
3
- module Middleman
4
- module CoreExtensions
5
- # Built-in helpers
6
- module DefaultHelpers
7
-
8
- # Extension registered
9
- class << self
10
- # @private
11
- def registered(app)
12
- require 'padrino-helpers'
13
-
14
- app.helpers ::Padrino::Helpers::OutputHelpers
15
- app.helpers ::Padrino::Helpers::TagHelpers
16
- app.helpers ::Padrino::Helpers::AssetTagHelpers
17
- app.helpers ::Padrino::Helpers::FormHelpers
18
- app.helpers ::Padrino::Helpers::FormatHelpers
19
- app.helpers ::Padrino::Helpers::RenderHelpers
20
- app.helpers ::Padrino::Helpers::NumberHelpers
21
-
22
- app.helpers Helpers
23
-
24
- app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
25
- end
26
- alias :included :registered
27
- end
28
-
29
- # The helpers
30
- module Helpers
31
- # Disable Padrino cache buster
32
- def asset_stamp
33
- false
34
- end
1
+ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
35
2
 
36
- # Output a stylesheet link tag based on the current path
37
- #
38
- # @return [String]
39
- def auto_stylesheet_link_tag
40
- auto_tag(:css) do |path|
41
- stylesheet_link_tag path
42
- end
43
- end
3
+ def initialize(app, options_hash={}, &block)
4
+ super
44
5
 
45
- # Output a javascript tag based on the current path
46
- #
47
- # @return [String]
48
- def auto_javascript_include_tag
49
- auto_tag(:js) do |path|
50
- javascript_include_tag path
51
- end
52
- end
6
+ require 'active_support/core_ext/object/to_query'
7
+ require 'padrino-helpers'
53
8
 
54
- # Output a stylesheet link tag based on the current path
55
- #
56
- # @param [Symbol] asset_ext The type of asset
57
- # @param [String] asset_dir Where to look for assets
58
- # @return [void]
59
- def auto_tag(asset_ext, asset_dir=nil)
60
- if asset_dir.nil?
61
- asset_dir = case asset_ext
62
- when :js then js_dir
63
- when :css then css_dir
64
- end
65
- end
9
+ app.helpers ::Padrino::Helpers::OutputHelpers
10
+ app.helpers ::Padrino::Helpers::TagHelpers
11
+ app.helpers ::Padrino::Helpers::AssetTagHelpers
12
+ app.helpers ::Padrino::Helpers::FormHelpers
13
+ app.helpers ::Padrino::Helpers::FormatHelpers
14
+ app.helpers ::Padrino::Helpers::RenderHelpers
15
+ app.helpers ::Padrino::Helpers::NumberHelpers
16
+ # app.helpers ::Padrino::Helpers::TranslationHelpers
17
+ app.helpers ::Padrino::Helpers::Breadcrumbs
66
18
 
67
- # If the basename of the request as no extension, assume we are serving a
68
- # directory and join index_file to the path.
69
- path = File.join(asset_dir, current_path)
70
- path = path.sub(/#{File.extname(path)}$/, ".#{asset_ext}")
19
+ app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
20
+ end
71
21
 
72
- yield path if sitemap.find_resource_by_path(path)
73
- end
22
+ # The helpers
23
+ helpers do
24
+ # Disable Padrino cache buster
25
+ def asset_stamp
26
+ false
27
+ end
74
28
 
75
- # Generate body css classes based on the current path
76
- #
77
- # @return [String]
78
- def page_classes
79
- path = current_path.dup
80
- path << index_file if path.end_with?('/')
81
- path = Util.strip_leading_slash(path)
29
+ # Output a stylesheet link tag based on the current path
30
+ #
31
+ # @return [String]
32
+ def auto_stylesheet_link_tag
33
+ auto_tag(:css) do |path|
34
+ stylesheet_link_tag path
35
+ end
36
+ end
82
37
 
83
- classes = []
84
- parts = path.split('.').first.split('/')
85
- parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
38
+ # Output a javascript tag based on the current path
39
+ #
40
+ # @return [String]
41
+ def auto_javascript_include_tag
42
+ auto_tag(:js) do |path|
43
+ javascript_include_tag path
44
+ end
45
+ end
86
46
 
87
- classes.join(' ')
47
+ # Output a stylesheet link tag based on the current path
48
+ #
49
+ # @param [Symbol] asset_ext The type of asset
50
+ # @param [String] asset_dir Where to look for assets
51
+ # @return [void]
52
+ def auto_tag(asset_ext, asset_dir=nil)
53
+ if asset_dir.nil?
54
+ asset_dir = case asset_ext
55
+ when :js then js_dir
56
+ when :css then css_dir
88
57
  end
58
+ end
89
59
 
90
- # Get the path of a file of a given type
91
- #
92
- # @param [Symbol] kind The type of file
93
- # @param [String] source The path to the file
94
- # @return [String]
95
- def asset_path(kind, source)
96
- return source if source.to_s.include?('//')
97
- asset_folder = case kind
98
- when :css then css_dir
99
- when :js then js_dir
100
- when :images then images_dir
101
- when :fonts then fonts_dir
102
- else kind.to_s
103
- end
104
- source = source.to_s.tr(' ', '')
105
- ignore_extension = (kind == :images || kind == :fonts) # don't append extension
106
- source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
107
- asset_folder = "" if source.start_with?('/') # absolute path
108
-
109
- asset_url(source, asset_folder)
110
- end
60
+ # If the basename of the request as no extension, assume we are serving a
61
+ # directory and join index_file to the path.
62
+ path = File.join(asset_dir, current_path)
63
+ path = path.sub(/#{File.extname(path)}$/, ".#{asset_ext}")
111
64
 
112
- # Get the URL of an asset given a type/prefix
113
- #
114
- # @param [String] path The path (such as "photo.jpg")
115
- # @param [String] prefix The type prefix (such as "images")
116
- # @return [String] The fully qualified asset url
117
- def asset_url(path, prefix="")
118
- # Don't touch assets which already have a full path
119
- if path.include?("//")
120
- path
121
- else # rewrite paths to use their destination path
122
- path = File.join(prefix, path)
123
- if resource = sitemap.find_resource_by_path(path)
124
- resource.url
125
- else
126
- File.join(config[:http_prefix], path)
127
- end
128
- end
129
- end
65
+ yield path if sitemap.find_resource_by_path(path)
66
+ end
130
67
 
131
- # Given a source path (referenced either absolutely or relatively)
132
- # or a Resource, this will produce the nice URL configured for that
133
- # path, respecting :relative_links, directory indexes, etc.
134
- def url_for(path_or_resource, options={})
135
- # Handle Resources and other things which define their own url method
136
- url = path_or_resource.respond_to?(:url) ? path_or_resource.url : path_or_resource
137
-
138
- begin
139
- uri = URI(url)
140
- rescue URI::InvalidURIError
141
- # Nothing we can do with it, it's not really a URI
142
- return url
143
- end
68
+ # Generate body css classes based on the current path
69
+ #
70
+ # @return [String]
71
+ def page_classes
72
+ path = current_path.dup
73
+ path << index_file if path.end_with?('/')
74
+ path = ::Middleman::Util.strip_leading_slash(path)
144
75
 
145
- relative = options.delete(:relative)
146
- raise "Can't use the relative option with an external URL" if relative && uri.host
147
-
148
- # Allow people to turn on relative paths for all links with
149
- # set :relative_links, true
150
- # but still override on a case by case basis with the :relative parameter.
151
- effective_relative = relative || false
152
- effective_relative = true if relative.nil? && config[:relative_links]
153
-
154
- # Try to find a sitemap resource corresponding to the desired path
155
- this_resource = current_resource # store in a local var to save work
156
- if path_or_resource.is_a?(Sitemap::Resource)
157
- resource = path_or_resource
158
- resource_url = url
159
- elsif this_resource && uri.path
160
- # Handle relative urls
161
- url_path = Pathname(uri.path)
162
- current_source_dir = Pathname('/' + this_resource.path).dirname
163
- url_path = current_source_dir.join(url_path) if url_path.relative?
164
- resource = sitemap.find_resource_by_path(url_path.to_s)
165
- resource_url = resource.url if resource
166
- end
76
+ classes = []
77
+ parts = path.split('.').first.split('/')
78
+ parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
167
79
 
168
- if resource
169
- # Switch to the relative path between this_resource and the given resource
170
- # if we've been asked to.
171
- if effective_relative
172
- # Output urls relative to the destination path, not the source path
173
- current_dir = Pathname('/' + this_resource.destination_path).dirname
174
- relative_path = Pathname(resource_url).relative_path_from(current_dir).to_s
175
-
176
- # Put back the trailing slash to avoid unnecessary Apache redirects
177
- if resource_url.end_with?('/') && !relative_path.end_with?('/')
178
- relative_path << '/'
179
- end
180
-
181
- uri.path = relative_path
182
- else
183
- uri.path = resource_url
184
- end
185
- else
186
- # If they explicitly asked for relative links but we can't find a resource...
187
- raise "No resource exists at #{url}" if relative
188
- end
80
+ classes.join(' ')
81
+ end
189
82
 
190
- # Support a :query option that can be a string or hash
191
- if query = options.delete(:query)
192
- uri.query = query.respond_to?(:to_param) ? query.to_param : query.to_s
193
- end
83
+ # Get the path of a file of a given type
84
+ #
85
+ # @param [Symbol] kind The type of file
86
+ # @param [String] source The path to the file
87
+ # @return [String]
88
+ def asset_path(kind, source)
89
+ return source if source.to_s.include?('//')
90
+ asset_folder = case kind
91
+ when :css then css_dir
92
+ when :js then js_dir
93
+ when :images then images_dir
94
+ when :fonts then fonts_dir
95
+ else kind.to_s
96
+ end
97
+ source = source.to_s.tr(' ', '')
98
+ ignore_extension = (kind == :images || kind == :fonts) # don't append extension
99
+ source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
100
+ asset_folder = "" if source.start_with?('/') # absolute path
194
101
 
195
- # Support a :fragment or :anchor option just like Padrino
196
- fragment = options.delete(:anchor) || options.delete(:fragment)
197
- uri.fragment = fragment.to_s if fragment
102
+ asset_url(source, asset_folder)
103
+ end
198
104
 
199
- # Finally make the URL back into a string
200
- uri.to_s
105
+ # Get the URL of an asset given a type/prefix
106
+ #
107
+ # @param [String] path The path (such as "photo.jpg")
108
+ # @param [String] prefix The type prefix (such as "images")
109
+ # @return [String] The fully qualified asset url
110
+ def asset_url(path, prefix="")
111
+ # Don't touch assets which already have a full path
112
+ if path.include?("//")
113
+ path
114
+ else # rewrite paths to use their destination path
115
+ path = File.join(prefix, path)
116
+ if resource = sitemap.find_resource_by_path(path)
117
+ resource.url
118
+ else
119
+ File.join(config[:http_prefix], path)
201
120
  end
121
+ end
122
+ end
202
123
 
203
- # Overload the regular link_to to be sitemap-aware - if you
204
- # reference a source path, either absolutely or relatively,
205
- # you'll get that resource's nice URL. Also, there is a
206
- # :relative option which, if set to true, will produce
207
- # relative URLs instead of absolute URLs. You can also add
208
- #
209
- # config[:relative_links] = true
210
- #
211
- # to config.rb to have all links default to relative.
212
- #
213
- # There is also a :query option that can be used to append a
214
- # query string, which can be expressed as either a String,
215
- # or a Hash which will be turned into URL parameters.
216
- def link_to(*args, &block)
217
- url_arg_index = block_given? ? 0 : 1
218
- options_index = block_given? ? 1 : 2
219
-
220
- if block_given? && args.size > 2
221
- raise ArgumentError.new("Too many arguments to link_to(url, options={}, &block)")
222
- end
124
+ # Given a source path (referenced either absolutely or relatively)
125
+ # or a Resource, this will produce the nice URL configured for that
126
+ # path, respecting :relative_links, directory indexes, etc.
127
+ def url_for(path_or_resource, options={})
128
+ # Handle Resources and other things which define their own url method
129
+ url = path_or_resource.respond_to?(:url) ? path_or_resource.url : path_or_resource
130
+
131
+ begin
132
+ uri = URI(url)
133
+ rescue URI::InvalidURIError
134
+ # Nothing we can do with it, it's not really a URI
135
+ return url
136
+ end
223
137
 
224
- if url = args[url_arg_index]
225
- options = args[options_index] || {}
226
- raise ArgumentError.new("Options must be a hash") unless options.is_a?(Hash)
138
+ relative = options.delete(:relative)
139
+ raise "Can't use the relative option with an external URL" if relative && uri.host
140
+
141
+ # Allow people to turn on relative paths for all links with
142
+ # set :relative_links, true
143
+ # but still override on a case by case basis with the :relative parameter.
144
+ effective_relative = relative || false
145
+ effective_relative = true if relative.nil? && config[:relative_links]
146
+
147
+ # Try to find a sitemap resource corresponding to the desired path
148
+ this_resource = current_resource # store in a local var to save work
149
+ if path_or_resource.is_a?(::Middleman::Sitemap::Resource)
150
+ resource = path_or_resource
151
+ resource_url = url
152
+ elsif this_resource && uri.path
153
+ # Handle relative urls
154
+ url_path = Pathname(uri.path)
155
+ current_source_dir = Pathname('/' + this_resource.path).dirname
156
+ url_path = current_source_dir.join(url_path) if url_path.relative?
157
+ resource = sitemap.find_resource_by_path(url_path.to_s)
158
+ resource_url = resource.url if resource
159
+ end
227
160
 
228
- # Transform the url through our magic url_for method
229
- args[url_arg_index] = url_for(url, options)
161
+ if resource
162
+ # Switch to the relative path between this_resource and the given resource
163
+ # if we've been asked to.
164
+ if effective_relative
165
+ # Output urls relative to the destination path, not the source path
166
+ current_dir = Pathname('/' + this_resource.destination_path).dirname
167
+ relative_path = Pathname(resource_url).relative_path_from(current_dir).to_s
168
+
169
+ # Put back the trailing slash to avoid unnecessary Apache redirects
170
+ if resource_url.end_with?('/') && !relative_path.end_with?('/')
171
+ relative_path << '/'
230
172
  end
231
173
 
232
- super(*args, &block)
174
+ uri.path = relative_path
175
+ else
176
+ uri.path = resource_url
233
177
  end
178
+ else
179
+ # If they explicitly asked for relative links but we can't find a resource...
180
+ raise "No resource exists at #{url}" if relative
181
+ end
234
182
 
235
- # Modified Padrino form_for that uses Middleman's url_for
236
- # to transform the URL.
237
- def form_tag(url, options={}, &block)
238
- url = url_for(url, options)
239
- super
240
- end
183
+ # Support a :query option that can be a string or hash
184
+ if query = options.delete(:query)
185
+ uri.query = query.respond_to?(:to_param) ? query.to_param : query.to_s
186
+ end
187
+
188
+ # Support a :fragment or :anchor option just like Padrino
189
+ fragment = options.delete(:anchor) || options.delete(:fragment)
190
+ uri.fragment = fragment.to_s if fragment
191
+
192
+ # Finally make the URL back into a string
193
+ uri.to_s
194
+ end
195
+
196
+ # Overload the regular link_to to be sitemap-aware - if you
197
+ # reference a source path, either absolutely or relatively,
198
+ # you'll get that resource's nice URL. Also, there is a
199
+ # :relative option which, if set to true, will produce
200
+ # relative URLs instead of absolute URLs. You can also add
201
+ #
202
+ # config[:relative_links] = true
203
+ #
204
+ # to config.rb to have all links default to relative.
205
+ #
206
+ # There is also a :query option that can be used to append a
207
+ # query string, which can be expressed as either a String,
208
+ # or a Hash which will be turned into URL parameters.
209
+ def link_to(*args, &block)
210
+ url_arg_index = block_given? ? 0 : 1
211
+ options_index = block_given? ? 1 : 2
212
+
213
+ if block_given? && args.size > 2
214
+ raise ArgumentError.new("Too many arguments to link_to(url, options={}, &block)")
241
215
  end
216
+
217
+ if url = args[url_arg_index]
218
+ options = args[options_index] || {}
219
+ raise ArgumentError.new("Options must be a hash") unless options.is_a?(Hash)
220
+
221
+ # Transform the url through our magic url_for method
222
+ args[url_arg_index] = url_for(url, options)
223
+ end
224
+
225
+ super(*args, &block)
226
+ end
227
+
228
+ # Modified Padrino form_for that uses Middleman's url_for
229
+ # to transform the URL.
230
+ def form_tag(url, options={}, &block)
231
+ url = url_for(url, options)
232
+ super
242
233
  end
243
234
  end
244
235
  end