Skip to content

Commit

Permalink
Merge pull request #2112 from jekyll/no-core-ext
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Mar 4, 2014
2 parents 1714520 + ed30920 commit 2985758
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 95 deletions.
2 changes: 1 addition & 1 deletion jekyll.gemspec
Expand Up @@ -39,7 +39,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('redcarpet', "~> 3.1")
s.add_runtime_dependency('toml', '~> 0.1.0')
s.add_runtime_dependency('jekyll-coffeescript', '~> 1.0')
s.add_runtime_dependency('jekyll-sass-converter', '~> 1.0.0.rc1')
s.add_runtime_dependency('jekyll-sass-converter', '~> 1.0.0.rc3')

s.add_development_dependency('rake', "~> 10.1")
s.add_development_dependency('rdoc', "~> 3.11")
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll.rb
Expand Up @@ -30,7 +30,7 @@ def require_all(path)

# internal requires
require 'jekyll/version'
require 'jekyll/core_ext'
require 'jekyll/utils'
require 'jekyll/stevenson'
require 'jekyll/deprecator'
require 'jekyll/configuration'
Expand Down Expand Up @@ -83,7 +83,7 @@ def self.configuration(override)
config = config.read_config_files(config.config_files(override))

# Merge DEFAULTS < _config.yml < override
config = config.deep_merge(override).stringify_keys
config = Utils.deep_merge_hashes(config, override).stringify_keys
set_timezone(config['timezone']) if config['timezone']

config
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll/configuration.rb
Expand Up @@ -159,7 +159,7 @@ def read_config_files(files)
begin
files.each do |config_file|
new_config = read_config_file(config_file)
configuration = configuration.deep_merge(new_config)
configuration = Utils.deep_merge_hashes(configuration, new_config)
end
rescue ArgumentError => err
Jekyll.logger.warn "WARNING:", "Error reading configuration. " +
Expand Down Expand Up @@ -229,7 +229,7 @@ def backwards_compatibilize
config[option] = csv_to_array(config[option])
end
end

if config.fetch('markdown', 'kramdown').to_s.downcase.eql?("maruku")
Jekyll::Deprecator.deprecation_message "You're using the 'maruku' " +
"Markdown processor. Maruku support has been deprecated and will " +
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/converters/markdown/kramdown_parser.rb
Expand Up @@ -20,7 +20,7 @@ def convert(content)
end
end

Kramdown::Document.new(content, @config["kramdown"].symbolize_keys).to_html
Kramdown::Document.new(content, Utils.symbolize_hash_keys(@config["kramdown"])).to_html
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll/convertible.rb
Expand Up @@ -107,7 +107,7 @@ def to_liquid(attrs = nil)
further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
[attribute, send(attribute)]
}]
data.deep_merge(further_data)
Utils.deep_merge_hashes(data, further_data)
end

# Recursively render layouts
Expand All @@ -123,7 +123,7 @@ def render_all_layouts(layouts, payload, info)
used = Set.new([layout])

while layout
payload = payload.deep_merge({"content" => output, "page" => layout.data})
payload = Utils.deep_merge_hashes(payload, {"content" => output, "page" => layout.data})

self.output = render_liquid(layout.content,
payload,
Expand Down
55 changes: 0 additions & 55 deletions lib/jekyll/core_ext.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/jekyll/page.rb
Expand Up @@ -109,10 +109,10 @@ def process(name)
#
# Returns nothing.
def render(layouts, site_payload)
payload = {
payload = Utils.deep_merge_hashes({
"page" => to_liquid,
'paginator' => pager.to_liquid
}.deep_merge(site_payload)
}, site_payload)

do_layout(payload, layouts)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/jekyll/post.rb
Expand Up @@ -66,13 +66,13 @@ def initialize(site, source, dir, name)

def populate_categories
if categories.empty?
self.categories = data.pluralized_array('category', 'categories').map {|c| c.to_s.downcase}
self.categories = Utils.pluralized_array_from_hash(data, 'category', 'categories').map {|c| c.to_s.downcase}
end
categories.flatten!
end

def populate_tags
self.tags = data.pluralized_array("tag", "tags").flatten
self.tags = Utils.pluralized_array_from_hash(data, "tag", "tags").flatten
end

# Get the full path to the directory containing the post files
Expand Down Expand Up @@ -241,10 +241,10 @@ def related_posts(posts)
# Returns nothing.
def render(layouts, site_payload)
# construct payload
payload = {
payload = Utils.deep_merge_hashes({
"site" => { "related_posts" => related_posts(site_payload["site"]["posts"]) },
"page" => to_liquid(EXCERPT_ATTRIBUTES_FOR_LIQUID)
}.deep_merge(site_payload)
}, site_payload)

if generate_excerpt?
extracted_excerpt.do_layout(payload, {})
Expand Down
79 changes: 79 additions & 0 deletions lib/jekyll/utils.rb
@@ -0,0 +1,79 @@
module Jekyll
module Utils
class << self

# Merges a master hash with another hash, recursively.
#
# master_hash - the "parent" hash whose values will be overridden
# other_hash - the other hash whose values will be persisted after the merge
#
# This code was lovingly stolen from some random gem:
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
#
# Thanks to whoever made it.
def deep_merge_hashes(master_hash, other_hash)
target = master_hash.dup

other_hash.keys.each do |key|
if other_hash[key].is_a? Hash and target[key].is_a? Hash
target[key] = Utils.deep_merge_hashes(target[key], other_hash[key])
next
end

target[key] = other_hash[key]
end

target
end

# Read array from the supplied hash favouring the singular key
# and then the plural key, and handling any nil entries.
#
# hash - the hash to read from
# singular_key - the singular key
# plural_key - the plural key
#
# Returns an array
def pluralized_array_from_hash(hash, singular_key, plural_key)
if hash.has_key?(singular_key)
array = [hash[singular_key]] if hash[singular_key]
elsif hash.has_key?(plural_key)
case hash[plural_key]
when String
array = hash[plural_key].split
when Array
array = hash[plural_key].compact
end
end
array || []
end

# Apply #to_sym to all keys in the hash
#
# hash - the hash to which to apply this transformation
#
# Returns a new hash with symbolized keys
def symbolize_hash_keys(hash)
target = hash.dup
target.keys.each do |key|
target[(key.to_sym rescue key) || key] = target.delete(key)
end
target
end

# Apply #to_s to all keys in the Hash
#
# hash - the hash to which to apply this transformation
#
# Returns a new hash with stringified keys
def stringify_hash_keys(hash)
target = hash.dup
target.keys.each do |key|
target[(key.to_s rescue key) || key] = target.delete(key)
end
target
end

end
end
end
11 changes: 11 additions & 0 deletions test/helper.rb
Expand Up @@ -28,6 +28,17 @@
class Test::Unit::TestCase
include RR::Adapters::TestUnit

def build_configs(overrides, base_hash = Jekyll::Configuration::DEFAULTS)
Utils.deep_merge_hashes(base_hash, overrides)
end

def site_configuration(overrides = {})
build_configs({
"source" => source_dir,
"destination" => dest_dir
}, build_configs(overrides))
end

def dest_dir(*subdirs)
test_dir('dest', *subdirs)
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_configuration.rb
Expand Up @@ -156,7 +156,7 @@ class TestConfiguration < Test::Unit::TestCase
should "load different config if specified" do
mock(SafeYAML).load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock($stdout).puts("Configuration file: #{@paths[:other]}")
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
assert_equal Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, { "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
end

should "load default config if path passed is empty" do
Expand Down Expand Up @@ -186,7 +186,7 @@ class TestConfiguration < Test::Unit::TestCase
mock(SafeYAML).load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock($stdout).puts("Configuration file: #{@paths[:default]}")
mock($stdout).puts("Configuration file: #{@paths[:other]}")
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })
assert_equal Utils.deep_merge_hashes(Jekyll::Configuration::DEFAULTS, { "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })
end
end
end
2 changes: 1 addition & 1 deletion test/test_kramdown.rb
Expand Up @@ -35,7 +35,7 @@ class TestKramdown < Test::Unit::TestCase
assert_match /<p>(&#8220;|“)Pit(&#8217;|’)hy(&#8221;|”)<\/p>/, @markdown.convert(%{"Pit'hy"}).strip

override = { 'kramdown' => { 'smart_quotes' => 'lsaquo,rsaquo,laquo,raquo' } }
markdown = Converters::Markdown.new(@config.deep_merge(override))
markdown = Converters::Markdown.new(Utils.deep_merge_hashes(@config, override))
assert_match /<p>(&#171;|«)Pit(&#8250;|›)hy(&#187;|»)<\/p>/, markdown.convert(%{"Pit'hy"}).strip
end

Expand Down
6 changes: 4 additions & 2 deletions test/test_pager.rb
Expand Up @@ -3,12 +3,14 @@
class TestPager < Test::Unit::TestCase

def build_site(config = {})
base = Jekyll::Configuration::DEFAULTS.deep_merge({
base = build_configs({
'source' => source_dir,
'destination' => dest_dir,
'paginate' => 1
})
site = Jekyll::Site.new(base.deep_merge(config))
site = Jekyll::Site.new(site_configuration(
{"paginate" => 1}.merge(config)
))
site.process
site
end
Expand Down
7 changes: 0 additions & 7 deletions test/test_sass.rb
@@ -1,13 +1,6 @@
require 'helper'

class TestSass < Test::Unit::TestCase
def site_configuration(overrides = {})
Jekyll::Configuration::DEFAULTS.deep_merge(overrides).deep_merge({
"source" => source_dir,
"destination" => dest_dir
})
end

def converter(overrides = {})
Jekyll::Converters::Sass.new(site_configuration({"sass" => overrides}))
end
Expand Down
6 changes: 4 additions & 2 deletions test/test_tags.rb
Expand Up @@ -6,7 +6,9 @@ class TestTags < Test::Unit::TestCase

def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown)
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.deep_merge({'highlighter' => 'pygments'}).deep_merge(override)
site_configuration({
"highlighter" => "pygments"
}.merge(override))
end
site = Site.new(Jekyll.configuration)

Expand Down Expand Up @@ -545,7 +547,7 @@ def fill_post(code, override = {})
context "include tag with variable and liquid filters" do
setup do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.deep_merge({'pygments' => true}).deep_merge({'source' => source_dir, 'destination' => dest_dir})
site_configuration({'pygments' => true})
end

site = Site.new(Jekyll.configuration)
Expand Down

0 comments on commit 2985758

Please sign in to comment.