diff --git a/benchmark/flat-map b/benchmark/flat-map new file mode 100644 index 00000000..b10c5c9a --- /dev/null +++ b/benchmark/flat-map @@ -0,0 +1,17 @@ +require 'benchmark/ips' + +enum = (0..50).to_a +nested = enum.map { |i| [i] } + +def do_thing(blah) + blah * 1 +end + +Benchmark.ips do |x| + x.report('.map.flatten with nested arrays') { nested.map { |i| do_thing(i) }.flatten(1) } + x.report('.flat_map with nested arrays') { nested.flat_map { |i| do_thing(i) } } + + x.report('.map.flatten with no nested arrays') { enum.map { |i| do_thing(i) }.flatten(1) } + x.report('.flat_map with no nested arrays') { enum.flat_map { |i| do_thing(i) } } +end + diff --git a/benchmark/hash-fetch b/benchmark/hash-fetch new file mode 100644 index 00000000..35708357 --- /dev/null +++ b/benchmark/hash-fetch @@ -0,0 +1,9 @@ +require 'benchmark/ips' + +h = {:bar => 'uco'} + +Benchmark.ips do |x| + x.report('fetch with no block') { h.fetch(:bar, (0..9).to_a) } + x.report('fetch with a block') { h.fetch(:bar) { (0..9).to_a } } + x.report('brackets with an ||') { h[:bar] || (0..9).to_a } +end diff --git a/benchmark/proc-call-vs-yield b/benchmark/proc-call-vs-yield new file mode 100644 index 00000000..3d55979b --- /dev/null +++ b/benchmark/proc-call-vs-yield @@ -0,0 +1,14 @@ +require 'benchmark/ips' + +def fast + yield +end + +def slow(&block) + block.call +end + +Benchmark.ips do |x| + x.report('yield') { fast { (0..9).to_a } } + x.report('block.call') { slow { (0..9).to_a } } +end diff --git a/benchmark/sequential-assignment b/benchmark/sequential-assignment new file mode 100644 index 00000000..945e982b --- /dev/null +++ b/benchmark/sequential-assignment @@ -0,0 +1,11 @@ +require 'benchmark/ips' + +Benchmark.ips do |x| + x.report('parallel assignment') do + a, b = 1, 2 + end + x.report('multi-line assignment') do + a = 1 + b = 2 + end +end diff --git a/benchmark/string-concat b/benchmark/string-concat new file mode 100644 index 00000000..c4a91748 --- /dev/null +++ b/benchmark/string-concat @@ -0,0 +1,8 @@ +require 'benchmark/ips' + +url = "http://jekyllrb.com" + +Benchmark.ips do |x| + x.report('+=') { url += '/' } + x.report('<<') { url << '/' } +end diff --git a/benchmark/string-replacement b/benchmark/string-replacement new file mode 100644 index 00000000..13715376 --- /dev/null +++ b/benchmark/string-replacement @@ -0,0 +1,13 @@ +require 'benchmark/ips' + +def str + 'http://baruco.org/2014/some-talk-with-some-amount-of-value' +end + +Benchmark.ips do |x| + x.report('#tr') { str.tr('some', 'a') } + x.report('#gsub') { str.gsub('some', 'a') } + x.report('#gsub!') { str.gsub!('some', 'a') } + x.report('#sub') { str.sub('some', 'a') } + x.report('#sub!') { str.sub!('some', 'a') } +end diff --git a/benchmark/symbol-to-proc b/benchmark/symbol-to-proc new file mode 100644 index 00000000..bc08b2f2 --- /dev/null +++ b/benchmark/symbol-to-proc @@ -0,0 +1,6 @@ +require 'benchmark/ips' + +Benchmark.ips do |x| + x.report('block') { (1..100).map { |i| i.to_s } } + x.report('&:to_s') { (1..100).map(&:to_s) } +end diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index 0aa3cdfd..1786a317 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -76,7 +76,7 @@ module Jekyll # # Returns a Set with the directory paths def keep_dirs - site.keep_files.map{|file| parent_dirs(File.join(site.dest, file))}.flatten.to_set + site.keep_files.map { |file| parent_dirs(File.join(site.dest, file)) }.flatten.to_set end # Private: Creates a regular expression from the config's keep_files array diff --git a/lib/jekyll/page.rb b/lib/jekyll/page.rb index d045b7c2..dcbd3ac5 100644 --- a/lib/jekyll/page.rb +++ b/lib/jekyll/page.rb @@ -126,7 +126,7 @@ module Jekyll # # Returns the path to the source file def path - data.fetch('path', relative_path.sub(/\A\//, '')) + data.fetch('path') { relative_path.sub(/\A\//, '') } end # The path to the page source file, relative to the site source diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 293e2c5d..68b85acb 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -108,14 +108,14 @@ module Jekyll # # Returns excerpt string. def excerpt - data.fetch('excerpt', extracted_excerpt.to_s) + data.fetch('excerpt') { extracted_excerpt.to_s } end # Public: the Post title, from the YAML Front-Matter or from the slug # # Returns the post title def title - data.fetch("title", titleized_slug) + data.fetch('title') { titleized_slug } end # Turns the post slug into a suitable title @@ -130,7 +130,7 @@ module Jekyll # # Returns the path to the file relative to the site source def path - data.fetch('path', relative_path.sub(/\A\//, '')) + data.fetch('path') { relative_path.sub(/\A\//, '') } end # The path to the post source file, relative to the site source diff --git a/lib/jekyll/url.rb b/lib/jekyll/url.rb index f4ea4d33..777ae3d1 100644 --- a/lib/jekyll/url.rb +++ b/lib/jekyll/url.rb @@ -46,24 +46,23 @@ module Jekyll # Returns the _unsanitizied_ String URL def generate_url @placeholders.inject(@template) do |result, token| + break result if result.index(':').nil? result.gsub(/:#{token.first}/, self.class.escape_path(token.last)) end end # Returns a sanitized String URL def sanitize_url(in_url) - - # Remove all double slashes - url = in_url.gsub(/\/\//, "/") - - # Remove every URL segment that consists solely of dots - url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/') + url = in_url \ + # Remove all double slashes + .gsub(/\/\//, '/') \ + # Remove every URL segment that consists solely of dots + .split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \ + # Always add a leading slash + .gsub(/\A([^\/])/, '/\1') # Append a trailing slash to the URL if the unsanitized URL had one - url += "/" if in_url =~ /\/$/ - - # Always add a leading slash - url.gsub!(/\A([^\/])/, '/\1') + url << "/" if in_url[-1].eql?('/') url end diff --git a/test/test_url.rb b/test/test_url.rb index 0576eda2..dcbe8bed 100644 --- a/test/test_url.rb +++ b/test/test_url.rb @@ -16,6 +16,13 @@ class TestURL < Test::Unit::TestCase ).to_s end + should "handle multiple of the same key in the template" do + assert_equal '/foo/bar/foo/', URL.new( + :template => "/:x/:y/:x/", + :placeholders => {:x => "foo", :y => "bar"} + ).to_s + end + should "return permalink if given" do assert_equal "/le/perma/link", URL.new( :template => "/:x/:y",