2014-04-04 19:22:34 +02:00
|
|
|
require 'padrino-helpers'
|
2014-02-19 03:30:29 +01:00
|
|
|
|
2014-04-04 19:22:34 +02:00
|
|
|
# Don't fail on invalid locale, that's not what our current
|
|
|
|
# users expect.
|
|
|
|
::I18n.enforce_available_locales = false
|
2013-09-22 23:02:51 +02:00
|
|
|
|
2013-06-09 00:36:14 +02:00
|
|
|
class Padrino::Helpers::OutputHelpers::ErbHandler
|
|
|
|
# Force Erb capture not to use safebuffer
|
2014-04-29 19:44:24 +02:00
|
|
|
# rubocop:disable UnderscorePrefixedVariableName
|
2013-06-09 00:36:14 +02:00
|
|
|
def capture_from_template(*args, &block)
|
2015-05-16 22:21:12 +02:00
|
|
|
self.output_buffer = ''
|
|
|
|
_buf_was = output_buffer
|
2014-02-19 03:30:29 +01:00
|
|
|
raw = block.call(*args)
|
|
|
|
captured = template.instance_variable_get(:@_out_buf)
|
2013-06-09 00:36:14 +02:00
|
|
|
self.output_buffer = _buf_was
|
2014-11-08 22:01:05 +01:00
|
|
|
engine_matches?(block) && !captured.empty? ? captured : raw
|
2013-06-09 00:36:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
|
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2011-06-22 21:13:35 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
require 'active_support/core_ext/object/to_query'
|
2013-06-04 18:48:01 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
app.helpers ::Padrino::Helpers::OutputHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::TagHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::AssetTagHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::FormHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::FormatHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::RenderHelpers
|
|
|
|
app.helpers ::Padrino::Helpers::NumberHelpers
|
2013-04-23 07:07:35 +02:00
|
|
|
# app.helpers ::Padrino::Helpers::TranslationHelpers
|
2012-04-13 14:55:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
|
|
|
|
end
|
2011-06-22 21:13:35 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# The helpers
|
|
|
|
helpers do
|
2013-06-12 20:19:02 +02:00
|
|
|
# Make all block content html_safe
|
2014-04-29 19:44:24 +02:00
|
|
|
# rubocop:disable Semicolon
|
2014-04-29 01:02:18 +02:00
|
|
|
def content_tag(name, content=nil, options=nil, &block)
|
2013-06-18 20:06:43 +02:00
|
|
|
if block_given?
|
|
|
|
options = content if content.is_a?(Hash)
|
|
|
|
content = capture_html(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
options = parse_data_options(name, options)
|
|
|
|
attributes = tag_attributes(options)
|
|
|
|
output = ActiveSupport::SafeBuffer.new
|
|
|
|
output.safe_concat "<#{name}#{attributes}>"
|
2014-02-19 03:30:29 +01:00
|
|
|
|
2013-06-18 20:06:43 +02:00
|
|
|
if content.respond_to?(:each) && !content.is_a?(String)
|
2013-12-29 02:43:59 +01:00
|
|
|
content.each { |c| output.safe_concat c; output.safe_concat ::Padrino::Helpers::TagHelpers::NEWLINE }
|
2013-06-18 20:06:43 +02:00
|
|
|
else
|
|
|
|
output.safe_concat "#{content}"
|
|
|
|
end
|
|
|
|
output.safe_concat "</#{name}>"
|
|
|
|
|
|
|
|
block_is_template?(block) ? concat_content(output) : output
|
2013-06-12 20:19:02 +02:00
|
|
|
end
|
|
|
|
|
2013-06-12 22:52:35 +02:00
|
|
|
def capture_html(*args, &block)
|
2014-04-04 19:22:34 +02:00
|
|
|
result = if handler = auto_find_proper_handler(&block)
|
2014-02-19 03:30:29 +01:00
|
|
|
handler.capture_from_template(*args, &block)
|
|
|
|
else
|
|
|
|
block.call(*args)
|
2013-06-12 22:52:35 +02:00
|
|
|
end
|
2014-04-04 19:22:34 +02:00
|
|
|
|
|
|
|
ActiveSupport::SafeBuffer.new.safe_concat(result)
|
2013-06-12 22:52:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def auto_find_proper_handler(&block)
|
|
|
|
engine = block_given? ? File.extname(block.source_location[0])[1..-1].to_sym : current_engine
|
2014-02-19 03:30:29 +01:00
|
|
|
handler_class = ::Padrino::Helpers::OutputHelpers.handlers[engine]
|
|
|
|
handler_class && handler_class.new(self)
|
2013-06-12 22:52:35 +02:00
|
|
|
end
|
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Disable Padrino cache buster
|
|
|
|
def asset_stamp
|
|
|
|
false
|
|
|
|
end
|
2012-04-13 14:55:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Output a stylesheet link tag based on the current path
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def auto_stylesheet_link_tag
|
|
|
|
auto_tag(:css) do |path|
|
|
|
|
stylesheet_link_tag path
|
|
|
|
end
|
|
|
|
end
|
2012-04-13 14:55:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Output a javascript tag based on the current path
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def auto_javascript_include_tag
|
|
|
|
auto_tag(:js) do |path|
|
|
|
|
javascript_include_tag path
|
|
|
|
end
|
|
|
|
end
|
2012-04-13 14:55:21 +02:00
|
|
|
|
2015-06-22 19:37:17 +02:00
|
|
|
# Override helper to add `relative` opt-out.
|
|
|
|
def stylesheet_link_tag(*sources)
|
|
|
|
options = {
|
|
|
|
:rel => 'stylesheet'
|
|
|
|
}.update(sources.extract_options!.symbolize_keys)
|
|
|
|
|
|
|
|
path_options = {}
|
|
|
|
path_options[:relative] = options.delete(:relative) if options.key?(:relative)
|
|
|
|
|
|
|
|
sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
|
|
|
|
all << tag(:link, {
|
|
|
|
href: asset_path(:css, source, path_options)
|
|
|
|
}.update(options))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Override helper to add `relative` opt-out.
|
|
|
|
def javascript_include_tag(*sources)
|
|
|
|
options = sources.extract_options!.symbolize_keys
|
|
|
|
|
|
|
|
path_options = {}
|
|
|
|
path_options[:relative] = options.delete(:relative) if options.key?(:relative)
|
|
|
|
|
|
|
|
sources.flatten.inject(::ActiveSupport::SafeBuffer.new) do |all,source|
|
|
|
|
all << content_tag(:script, nil, {
|
|
|
|
src: asset_path(:js, source, path_options)
|
|
|
|
}.update(options))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Output a stylesheet link tag based on the current path
|
|
|
|
#
|
|
|
|
# @param [Symbol] asset_ext The type of asset
|
|
|
|
# @param [String] asset_dir Where to look for assets
|
|
|
|
# @return [void]
|
|
|
|
def auto_tag(asset_ext, asset_dir=nil)
|
|
|
|
if asset_dir.nil?
|
|
|
|
asset_dir = case asset_ext
|
2014-04-29 01:02:18 +02:00
|
|
|
when :js
|
|
|
|
js_dir
|
|
|
|
when :css
|
|
|
|
css_dir
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# If the basename of the request as no extension, assume we are serving a
|
|
|
|
# directory and join index_file to the path.
|
2014-07-20 22:53:05 +02:00
|
|
|
path = File.join(asset_dir, current_resource.path)
|
2013-08-16 07:03:32 +02:00
|
|
|
path = path.sub(/#{Regexp.escape(File.extname(path))}$/, ".#{asset_ext}")
|
2012-04-28 07:05:52 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
yield path if sitemap.find_resource_by_path(path)
|
|
|
|
end
|
2013-04-10 05:21:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Generate body css classes based on the current path
|
|
|
|
#
|
|
|
|
# @return [String]
|
2014-04-29 01:02:18 +02:00
|
|
|
def page_classes(path=current_path.dup, options={})
|
2013-12-13 08:37:00 +01:00
|
|
|
if path.is_a? Hash
|
|
|
|
options = path
|
|
|
|
path = current_path.dup
|
2013-12-13 17:30:19 +01:00
|
|
|
end
|
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
path << index_file if path.end_with?('/')
|
|
|
|
path = ::Middleman::Util.strip_leading_slash(path)
|
2013-02-09 07:22:33 +01:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
classes = []
|
|
|
|
parts = path.split('.').first.split('/')
|
2014-04-29 01:02:18 +02:00
|
|
|
parts.each_with_index { |_, i| classes << parts.first(i + 1).join('_') }
|
2013-02-09 07:22:33 +01:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
prefix = options[:numeric_prefix] || 'x'
|
2013-10-20 00:31:49 +02:00
|
|
|
classes.map do |c|
|
|
|
|
# Replace weird class name characters
|
|
|
|
c = c.gsub(/[^a-zA-Z0-9\-_]/, '-')
|
|
|
|
|
|
|
|
# Class names can't start with a digit
|
2013-10-29 17:33:27 +01:00
|
|
|
c = "#{prefix}#{c}" if c =~ /\A\d/
|
2013-10-20 00:31:49 +02:00
|
|
|
c
|
|
|
|
end.join(' ')
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
2013-04-10 05:21:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Get the path of a file of a given type
|
|
|
|
#
|
|
|
|
# @param [Symbol] kind The type of file
|
|
|
|
# @param [String] source The path to the file
|
2015-06-22 19:37:17 +02:00
|
|
|
# @param [Hash] options Additional options.
|
2013-04-20 23:36:38 +02:00
|
|
|
# @return [String]
|
2015-06-22 19:37:17 +02:00
|
|
|
def asset_path(kind, source, options={})
|
2013-06-16 01:22:14 +02:00
|
|
|
return source if source.to_s.include?('//') || source.to_s.start_with?('data:')
|
2014-04-29 01:02:18 +02:00
|
|
|
asset_folder = case kind
|
|
|
|
when :css
|
|
|
|
css_dir
|
|
|
|
when :js
|
|
|
|
js_dir
|
|
|
|
when :images
|
|
|
|
images_dir
|
|
|
|
when :fonts
|
|
|
|
fonts_dir
|
|
|
|
else
|
|
|
|
kind.to_s
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
2014-04-29 01:02:18 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
source = source.to_s.tr(' ', '')
|
|
|
|
ignore_extension = (kind == :images || kind == :fonts) # don't append extension
|
|
|
|
source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
|
2013-12-28 01:26:31 +01:00
|
|
|
asset_folder = '' if source.start_with?('/') # absolute path
|
2013-02-09 07:22:33 +01:00
|
|
|
|
2015-06-22 19:37:17 +02:00
|
|
|
asset_url(source, asset_folder, options)
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
2013-04-10 05:21:21 +02:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Get the URL of an asset given a type/prefix
|
|
|
|
#
|
|
|
|
# @param [String] path The path (such as "photo.jpg")
|
|
|
|
# @param [String] prefix The type prefix (such as "images")
|
2015-06-22 19:37:17 +02:00
|
|
|
# @param [Hash] options Additional options.
|
2013-04-20 23:36:38 +02:00
|
|
|
# @return [String] The fully qualified asset url
|
2015-06-22 19:37:17 +02:00
|
|
|
def asset_url(path, prefix='', options={})
|
2013-04-20 23:36:38 +02:00
|
|
|
# Don't touch assets which already have a full path
|
2015-06-22 19:37:17 +02:00
|
|
|
if path.include?('//') || path.start_with?('data:') || !current_resource
|
2013-04-20 23:36:38 +02:00
|
|
|
path
|
|
|
|
else # rewrite paths to use their destination path
|
2015-06-22 19:37:17 +02:00
|
|
|
result = if resource = sitemap.find_resource_by_destination_path(url_for(path))
|
2013-04-20 23:36:38 +02:00
|
|
|
resource.url
|
|
|
|
else
|
2013-09-22 23:02:51 +02:00
|
|
|
path = File.join(prefix, path)
|
2015-06-22 19:37:17 +02:00
|
|
|
|
2013-09-22 23:02:51 +02:00
|
|
|
if resource = sitemap.find_resource_by_path(path)
|
|
|
|
resource.url
|
|
|
|
else
|
|
|
|
File.join(config[:http_prefix], path)
|
|
|
|
end
|
2013-02-09 07:22:33 +01:00
|
|
|
end
|
2015-06-22 19:37:17 +02:00
|
|
|
|
|
|
|
if options[:relative] != true
|
|
|
|
result
|
|
|
|
else
|
|
|
|
current_dir = Pathname('/' + current_resource.destination_path)
|
|
|
|
Pathname(result).relative_path_from(current_dir.dirname).to_s
|
|
|
|
end
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
|
|
|
end
|
2013-02-09 07:22:33 +01:00
|
|
|
|
2013-04-20 23:36:38 +02:00
|
|
|
# Given a source path (referenced either absolutely or relatively)
|
|
|
|
# or a Resource, this will produce the nice URL configured for that
|
|
|
|
# path, respecting :relative_links, directory indexes, etc.
|
|
|
|
def url_for(path_or_resource, options={})
|
2013-12-31 23:41:17 +01:00
|
|
|
options_with_resource = options.dup
|
|
|
|
options_with_resource[:current_resource] ||= current_resource
|
|
|
|
|
|
|
|
::Middleman::Util.url_for(self, path_or_resource, options_with_resource)
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Overload the regular link_to to be sitemap-aware - if you
|
|
|
|
# reference a source path, either absolutely or relatively,
|
|
|
|
# you'll get that resource's nice URL. Also, there is a
|
|
|
|
# :relative option which, if set to true, will produce
|
|
|
|
# relative URLs instead of absolute URLs. You can also add
|
|
|
|
#
|
|
|
|
# config[:relative_links] = true
|
|
|
|
#
|
|
|
|
# to config.rb to have all links default to relative.
|
|
|
|
#
|
|
|
|
# There is also a :query option that can be used to append a
|
|
|
|
# query string, which can be expressed as either a String,
|
|
|
|
# or a Hash which will be turned into URL parameters.
|
|
|
|
def link_to(*args, &block)
|
|
|
|
url_arg_index = block_given? ? 0 : 1
|
|
|
|
options_index = block_given? ? 1 : 2
|
|
|
|
|
|
|
|
if block_given? && args.size > 2
|
2014-04-29 01:02:18 +02:00
|
|
|
raise ArgumentError, 'Too many arguments to link_to(url, options={}, &block)'
|
2012-04-28 07:05:52 +02:00
|
|
|
end
|
2013-04-20 23:36:38 +02:00
|
|
|
|
|
|
|
if url = args[url_arg_index]
|
|
|
|
options = args[options_index] || {}
|
2014-04-29 01:02:18 +02:00
|
|
|
raise ArgumentError, 'Options must be a hash' unless options.is_a?(Hash)
|
2013-04-20 23:36:38 +02:00
|
|
|
|
|
|
|
# Transform the url through our magic url_for method
|
|
|
|
args[url_arg_index] = url_for(url, options)
|
2013-12-31 23:41:17 +01:00
|
|
|
|
|
|
|
# Cleanup before passing to Padrino
|
|
|
|
options.delete(:relative)
|
|
|
|
options.delete(:current_resource)
|
|
|
|
options.delete(:find_resource)
|
|
|
|
options.delete(:query)
|
|
|
|
options.delete(:anchor)
|
|
|
|
options.delete(:fragment)
|
2013-04-20 23:36:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
super(*args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Modified Padrino form_for that uses Middleman's url_for
|
|
|
|
# to transform the URL.
|
|
|
|
def form_tag(url, options={}, &block)
|
|
|
|
url = url_for(url, options)
|
|
|
|
super
|
2012-04-28 07:05:52 +02:00
|
|
|
end
|
2015-05-16 22:21:12 +02:00
|
|
|
|
2015-04-22 22:09:02 +02:00
|
|
|
# Modified Padrino image_tag so that it finds the paths for srcset
|
|
|
|
# using asset_path for the images listed in the srcset param
|
|
|
|
def image_tag(path, params={})
|
|
|
|
params.symbolize_keys!
|
2015-05-16 22:21:12 +02:00
|
|
|
|
2015-04-22 22:09:02 +02:00
|
|
|
if params.key?(:srcset)
|
2015-04-26 18:41:50 +02:00
|
|
|
images_sources = params[:srcset].split(',').map do |src_def|
|
|
|
|
if src_def.include?('//')
|
|
|
|
src_def
|
|
|
|
else
|
|
|
|
image_def, size_def = src_def.strip.split(/\s+/)
|
|
|
|
asset_path(:images, image_def) + ' ' + size_def
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params[:srcset] = images_sources.join(', ')
|
2015-04-22 22:09:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
super(path, params)
|
|
|
|
end
|
2009-10-23 02:25:15 +02:00
|
|
|
end
|
2012-05-19 22:28:59 +02:00
|
|
|
end
|