diff --git a/middleman-cli/lib/middleman-cli/console.rb b/middleman-cli/lib/middleman-cli/console.rb index 51ddb28e..829e9901 100644 --- a/middleman-cli/lib/middleman-cli/console.rb +++ b/middleman-cli/lib/middleman-cli/console.rb @@ -29,12 +29,17 @@ module Middleman::Cli ::Middleman::Logger.singleton(opts[:debug] ? 0 : 1, opts[:instrumenting] || false) end - # TODO: get file watcher / reload! working in console + interact_with @app + end + # Start an interactive console in the context of the provided object. + # @param [Object] context + # @return [void] + def interact_with(context) IRB.setup nil IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context require 'irb/ext/multi-irb' - IRB.irb nil, @app + IRB.irb nil, context end # Add to CLI diff --git a/middleman-cli/lib/middleman-cli/server.rb b/middleman-cli/lib/middleman-cli/server.rb index 47ec23d5..75a8cbfa 100644 --- a/middleman-cli/lib/middleman-cli/server.rb +++ b/middleman-cli/lib/middleman-cli/server.rb @@ -11,11 +11,9 @@ module Middleman::Cli class_option :host, type: :string, aliases: '-h', - default: '0.0.0.0', desc: 'Bind to HOST address' class_option :port, aliases: '-p', - default: '4567', desc: 'The port Middleman will listen on' class_option :verbose, type: :boolean, @@ -40,7 +38,7 @@ module Middleman::Cli class_option :latency, type: :numeric, aliases: '-l', - default: 0.25, + default: 0.5, desc: 'Set file watcher latency, in seconds' # Start the server diff --git a/middleman-core/features/asset_hash.feature b/middleman-core/features/asset_hash.feature index e64c9208..ea3b9d67 100644 --- a/middleman-core/features/asset_hash.feature +++ b/middleman-core/features/asset_hash.feature @@ -79,6 +79,13 @@ Feature: Assets get file hashes appended to them and references to them are upda And I should see 'images/100px-5fd6fb90.jpg' And I should see 'images/100px-1242c368.png' + Scenario: Hashed assets work with Slim + Given the Server is running at "asset-hash-app" + When I go to "/slim.html" + And I should see 'src="images/300px-59adce76.jpg"' + And I should see 'src="images/100px-5fd6fb90.jpg"' + And I should see 'srcset="images/100px-5fd6fb90.jpg 1x, images/200px-c11eb203.jpg 2x, images/300px-59adce76.jpg 3x"' + Scenario: Enabling an asset host still produces hashed files and references Given the Server is running at "asset-hash-host-app" When I go to "/" diff --git a/middleman-core/features/helpers_link_to.feature b/middleman-core/features/helpers_link_to.feature index 18d0c497..90b6f08a 100644 --- a/middleman-core/features/helpers_link_to.feature +++ b/middleman-core/features/helpers_link_to.feature @@ -5,6 +5,24 @@ Feature: link_to helper When I go to "/link_to_erb.html" Then I should see "erb with html tags" + Scenario: link_to works with absolute URLs (where the relative part matches a local path) + Given a fixture app "link-to-app" + And a file named "config.rb" with: + """ + set :relative_links, true + """ + And a file named "source/test.html.erb" with: + """ + Hello + """ + And a file named "source/link_to_absolute.html.erb" with: + """ + <%= link_to "test", "http://google.com/test.html" %> + """ + And the Server is running at "link-to-app" + When I go to "/link_to_absolute.html" + Then I should see 'test' + Scenario: link_to works with blocks (slim) Given the Server is running at "link-to-app" When I go to "/link_to_slim.html" diff --git a/middleman-core/features/image_srcset_paths.feature b/middleman-core/features/image_srcset_paths.feature new file mode 100644 index 00000000..b6b1046c --- /dev/null +++ b/middleman-core/features/image_srcset_paths.feature @@ -0,0 +1,7 @@ +Feature: Support srcset property as params for image_tag helper + This lets you specify responsive image sizes + + Scenario: Rendering an image with the feature enabled + Given the Server is running at "image-srcset-paths-app" + When I go to "/image-srcset-paths.html" + Then I should see '//example.com/remote-image.jpg 2x, /images/blank_3x.jpg 3x' \ No newline at end of file diff --git a/middleman-core/features/markdown_kramdown_in_haml.feature b/middleman-core/features/markdown_kramdown_in_haml.feature index 5b5a5871..5980e1b7 100644 --- a/middleman-core/features/markdown_kramdown_in_haml.feature +++ b/middleman-core/features/markdown_kramdown_in_haml.feature @@ -33,9 +33,10 @@ Feature: Markdown support in Haml (Kramdown) :markdown [A link](/link_target.html) - ![image](blank.gif) + ![image](blank.gif){: srcset="image_2x.jpg 2x"} """ Given the Server is running at "markdown-in-haml-app" When I go to "/link_and_image/" Then I should see "/link_target/" + Then I should see "/images/image_2x.jpg 2x" Then I should see 'src="/images/blank.gif"' diff --git a/middleman-core/fixtures/asset-hash-app/source/slim.html.slim b/middleman-core/fixtures/asset-hash-app/source/slim.html.slim new file mode 100644 index 00000000..dc74a5ee --- /dev/null +++ b/middleman-core/fixtures/asset-hash-app/source/slim.html.slim @@ -0,0 +1,8 @@ +--- +directory_index: false +layout: false +--- + += image_tag '100px.jpg', srcset: "100px.jpg 1x, 200px.jpg 2x, 300px.jpg 3x" + += image_tag "300px.jpg" \ No newline at end of file diff --git a/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb b/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb new file mode 100755 index 00000000..b5fba613 --- /dev/null +++ b/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb @@ -0,0 +1 @@ +<%= image_tag 'blank.jpg', srcset: '//example.com/remote-image.jpg 2x, blank_3x.jpg 3x, http://example.com/remoteimage.jpg 4x' %> \ No newline at end of file diff --git a/middleman-core/fixtures/image-srcset-paths-app/images/blank.gif b/middleman-core/fixtures/image-srcset-paths-app/images/blank.gif new file mode 100755 index 00000000..2498f1aa Binary files /dev/null and b/middleman-core/fixtures/image-srcset-paths-app/images/blank.gif differ diff --git a/middleman-core/fixtures/large-build-app/config.rb b/middleman-core/fixtures/large-build-app/config.rb index ae596127..590f9d7f 100644 --- a/middleman-core/fixtures/large-build-app/config.rb +++ b/middleman-core/fixtures/large-build-app/config.rb @@ -1 +1,3 @@ page "/spaces in file.html", layout: false + +config[:port] = 5555 diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 210f2c16..02c2ef20 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -81,6 +81,14 @@ module Middleman define_hook :before_render define_hook :after_render + # Which host preview should start on. + # @return [Fixnum] + config.define_setting :host, '0.0.0.0', 'The preview server host' + + # Which port preview should start on. + # @return [Fixnum] + config.define_setting :port, 4567, 'The preview server port' + # Name of the source directory # @return [String] config.define_setting :source, 'source', 'Name of the source directory' diff --git a/middleman-core/lib/middleman-core/cli/server.rb b/middleman-core/lib/middleman-core/cli/server.rb new file mode 100644 index 00000000..cf3afafc --- /dev/null +++ b/middleman-core/lib/middleman-core/cli/server.rb @@ -0,0 +1,86 @@ +# CLI Module +module Middleman::Cli + # Server thor task + class Server < Thor + check_unknown_options! + + namespace :server + + desc 'server [options]', 'Start the preview server' + method_option :environment, + aliases: '-e', + default: ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development', + desc: 'The environment Middleman will run under' + method_option :host, + type: :string, + aliases: '-h', + desc: 'Bind to HOST address' + method_option :port, + aliases: '-p', + desc: 'The port Middleman will listen on' + method_option :verbose, + type: :boolean, + default: false, + desc: 'Print debug messages' + method_option :instrument, + type: :string, + default: false, + desc: 'Print instrument messages' + method_option :disable_watcher, + type: :boolean, + default: false, + desc: 'Disable the file change and delete watcher process' + method_option :profile, + type: :boolean, + default: false, + desc: 'Generate profiling report for server startup' + method_option :reload_paths, + type: :string, + default: false, + desc: 'Additional paths to auto-reload when files change' + method_option :force_polling, + type: :boolean, + default: false, + desc: 'Force file watcher into polling mode' + method_option :latency, + type: :numeric, + aliases: '-l', + default: 0.25, + desc: 'Set file watcher latency, in seconds' + + # Start the server + def server + require 'middleman-core' + require 'middleman-core/preview_server' + + unless ENV['MM_ROOT'] + puts '== Could not find a Middleman project config.rb' + puts '== Treating directory as a static site to be served' + ENV['MM_ROOT'] = Dir.pwd + ENV['MM_SOURCE'] = '' + end + + params = { + port: options['port'], + host: options['host'], + environment: options['environment'], + debug: options['verbose'], + instrumenting: options['instrument'], + disable_watcher: options['disable_watcher'], + reload_paths: options['reload_paths'], + force_polling: options['force_polling'], + latency: options['latency'] + } + + puts '== The Middleman is loading' + ::Middleman::PreviewServer.start(params) + end + end + + def self.exit_on_failure? + true + end + + # Map "s" to "server" + Base.map('s' => 'server') +end diff --git a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb index 95801c20..e66c1739 100644 --- a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb @@ -221,5 +221,26 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension url = url_for(url, options) super end + + # 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! + + if params.key?(:srcset) + 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(', ') + end + + super(path, params) + end end end diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 0514c58b..e89e552f 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -6,8 +6,6 @@ require 'middleman-core/rack' # rubocop:disable GlobalVars module Middleman module PreviewServer - DEFAULT_PORT = 4567 - class << self extend Forwardable @@ -17,9 +15,7 @@ module Middleman # Start an instance of Middleman::Application # @return [void] def start(opts={}) - @options = opts.dup.freeze - @host = @options[:host] || '0.0.0.0' - @port = @options[:port] || DEFAULT_PORT + @options = opts mount_instance(new_app) logger.info "== The Middleman is standing watch at #{uri}" @@ -90,7 +86,7 @@ module Middleman private def new_app - opts = @options + opts = @options.dup ::Middleman::Logger.singleton( opts[:debug] ? 0 : 1, @@ -103,6 +99,9 @@ module Middleman config[:watcher_force_polling] = opts[:force_polling] config[:watcher_latency] = opts[:latency] + config[:host] = opts[:host] if opts[:host] + config[:port] = opts[:port] if opts[:port] + ready do match_against = [ %r{^config\.rb$}, @@ -118,6 +117,9 @@ module Middleman end end + @host = app.config[:host] + @port = app.config[:port] + app.files.on_change :reload do $mm_reload = true @webrick.stop diff --git a/middleman-core/lib/middleman-core/sources/source_watcher.rb b/middleman-core/lib/middleman-core/sources/source_watcher.rb index 6722727f..d2bbbe92 100644 --- a/middleman-core/lib/middleman-core/sources/source_watcher.rb +++ b/middleman-core/lib/middleman-core/sources/source_watcher.rb @@ -128,7 +128,11 @@ module Middleman def listen! return if @disable_watcher || @listener || @waiting_for_existence - config = { force_polling: @force_polling } + config = { + force_polling: @force_polling, + wait_for_delay: 0.5 + } + config[:latency] = @latency if @latency @listener = ::Listen.to(@directory.to_s, config, &method(:on_listener_change)) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 945c4f96..debde48e 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -283,7 +283,7 @@ module Middleman if path_or_resource.is_a?(::Middleman::Sitemap::Resource) resource = path_or_resource resource_url = url - elsif this_resource && uri.path + elsif this_resource && uri.path && !uri.host # Handle relative urls url_path = Pathname(uri.path) current_source_dir = Pathname('/' + this_resource.path).dirname @@ -299,7 +299,7 @@ module Middleman resource = app.sitemap.find_resource_by_destination_path(url_path.to_s) resource_url = resource.url if resource end - elsif options[:find_resource] && uri.path + elsif options[:find_resource] && uri.path && !uri.host resource = app.sitemap.find_resource_by_path(uri.path) resource_url = resource.url if resource end diff --git a/middleman-core/lib/middleman/rack.rb b/middleman-core/lib/middleman/rack.rb index 6fbd2129..51ddb4c4 100644 --- a/middleman-core/lib/middleman/rack.rb +++ b/middleman-core/lib/middleman/rack.rb @@ -2,3 +2,4 @@ require 'middleman-core/load_paths' ::Middleman.setup_load_paths require 'middleman-core' +require 'middleman-core/application'