From f581940c7103f079f13ab84ddf474e2d777a14f4 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 5 Nov 2013 19:46:47 -0600 Subject: [PATCH 1/7] Create a LayoutReader class to read layouts --- lib/jekyll.rb | 1 + lib/jekyll/layout_reader.rb | 21 +++++++++++++++++++++ lib/jekyll/site.rb | 10 +--------- 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 lib/jekyll/layout_reader.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index caa45e50..803b6fa4 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -48,6 +48,7 @@ require 'jekyll/errors' require 'jekyll/related_posts' require 'jekyll/cleaner' require 'jekyll/entry_filter' +require 'jekyll/layout_reader' # extensions require 'jekyll/plugin' diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb new file mode 100644 index 00000000..a44b2e06 --- /dev/null +++ b/lib/jekyll/layout_reader.rb @@ -0,0 +1,21 @@ +class LayoutReader + attr_reader :site + def initialize(site) + @site = site + @layouts = {} + end + + def read + base = File.join(site.source, site.config['layouts']) + return @layouts unless File.exists?(base) + entries = [] + Dir.chdir(base) { entries = EntryFilter.new(site).filter(Dir['**/*.*']) } + + entries.each do |f| + name = f.split(".")[0..-2].join(".") + @layouts[name] = Layout.new(site, base, f) + end + + @layouts + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 7457ca6d..a154d134 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -137,15 +137,7 @@ module Jekyll # # Returns nothing. def read_layouts - base = File.join(self.source, self.config['layouts']) - return unless File.exists?(base) - entries = [] - Dir.chdir(base) { entries = filter_entries(Dir['**/*.*'], base) } - - entries.each do |f| - name = f.split(".")[0..-2].join(".") - self.layouts[name] = Layout.new(self, base, f) - end + self.layouts = LayoutReader.new(self).read end # Recursively traverse directories to find posts, pages and static files From c5b81d580b122593c87584dd407550eba76d7306 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Sun, 10 Nov 2013 21:04:11 -0600 Subject: [PATCH 2/7] Refactor the LayoutReader class --- lib/jekyll/layout_reader.rb | 38 +++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb index a44b2e06..b09a75ca 100644 --- a/lib/jekyll/layout_reader.rb +++ b/lib/jekyll/layout_reader.rb @@ -6,16 +6,34 @@ class LayoutReader end def read - base = File.join(site.source, site.config['layouts']) - return @layouts unless File.exists?(base) - entries = [] - Dir.chdir(base) { entries = EntryFilter.new(site).filter(Dir['**/*.*']) } + layout_entries.each do |f| + @layouts[layout_name(f)] = Layout.new(site, layout_directory, f) + end - entries.each do |f| - name = f.split(".")[0..-2].join(".") - @layouts[name] = Layout.new(site, base, f) - end - - @layouts + @layouts end + + private + + def layout_directory + File.join(site.source, site.config['layouts']) + end + + def layout_entries + entries = [] + with(layout_directory) do + entries = EntryFilter.new(site).filter(Dir['**/*.*']) + end + entries + end + + def with(directory) + return unless File.exists?(directory) + Dir.chdir(directory) { yield directory } + end + + def layout_name(file) + file.split(".")[0..-2].join(".") + end + end From 70ecef0094e561326c12814968413715ad7e98d2 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Sun, 10 Nov 2013 21:04:19 -0600 Subject: [PATCH 3/7] Sort methods from most important to least important This idea is based on the concept of a newspaper. The most important things such as the headlines and the major details of the story at the top. This translates to code in that the public API and the more important private methods are at the top of the file. The more detailed information (or methods, in the code) are further down, so that if you've gotten all you need out of the code up to a certain point, you don't need to keep reading anymore. --- lib/jekyll/layout_reader.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb index b09a75ca..6181e47e 100644 --- a/lib/jekyll/layout_reader.rb +++ b/lib/jekyll/layout_reader.rb @@ -15,10 +15,6 @@ class LayoutReader private - def layout_directory - File.join(site.source, site.config['layouts']) - end - def layout_entries entries = [] with(layout_directory) do @@ -27,13 +23,18 @@ class LayoutReader entries end - def with(directory) - return unless File.exists?(directory) - Dir.chdir(directory) { yield directory } - end - def layout_name(file) file.split(".")[0..-2].join(".") end + def with(directory) + return unless File.exists?(directory) + Dir.chdir(directory) { yield } + end + + def layout_directory + File.join(site.source, site.config['layouts']) + end + + end From facf115c04186bd106b0254ae7b12ef3a3b1b599 Mon Sep 17 00:00:00 2001 From: Matt Rogers & Persa Zula Date: Thu, 6 Feb 2014 22:45:51 -0600 Subject: [PATCH 4/7] Move layout tests from site to its own file --- test/test_layout_reader.rb | 17 +++++++++++++++++ test/test_site.rb | 5 ----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/test_layout_reader.rb diff --git a/test/test_layout_reader.rb b/test/test_layout_reader.rb new file mode 100644 index 00000000..eead655a --- /dev/null +++ b/test/test_layout_reader.rb @@ -0,0 +1,17 @@ +require 'helper' + +class TestLayoutReader < Test::Unit::TestCase + context "reading layouts" do + setup do + stub(Jekyll).configuration do + Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir}) + end + @site = Site.new(Jekyll.configuration) + end + + should "read layouts" do + layouts = LayoutReader.new(@site).read + assert_equal ["default", "simple", "post/simple"].sort, layouts.keys.sort + end + end +end diff --git a/test/test_site.rb b/test/test_site.rb index 5e639f50..b1cf01d1 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -161,11 +161,6 @@ class TestSite < Test::Unit::TestCase assert_equal sorted_pages, @site.pages.map(&:name) end - should "read layouts" do - @site.read_layouts - assert_equal ["default", "simple", "post/simple"].sort, @site.layouts.keys.sort - end - should "read posts" do @site.read_posts('') posts = Dir[source_dir('_posts', '**', '*')] From fd92820b03a0d0186890789d99778420557374ae Mon Sep 17 00:00:00 2001 From: Matt Rogers & Persa Zula Date: Thu, 6 Feb 2014 22:46:09 -0600 Subject: [PATCH 5/7] Remove Site#read_layouts --- lib/jekyll/site.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index a154d134..ed55598f 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -127,19 +127,11 @@ module Jekyll # # Returns nothing. def read - self.read_layouts + self.layouts = LayoutReader.new(self).read self.read_directories self.read_data(config['data_source']) end - # Read all the files in / and create a new Layout object - # with each one. - # - # Returns nothing. - def read_layouts - self.layouts = LayoutReader.new(self).read - end - # Recursively traverse directories to find posts, pages and static files # that will become part of the site according to the rules in # filter_entries. From 833b40095fd61b62abdfd94dde1de74ee22a0d81 Mon Sep 17 00:00:00 2001 From: Matt Rogers & Persa Zula Date: Thu, 6 Feb 2014 23:07:11 -0600 Subject: [PATCH 6/7] Rename `with` to `within` Thanks @parkr --- lib/jekyll/layout_reader.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb index 6181e47e..1f5e09a2 100644 --- a/lib/jekyll/layout_reader.rb +++ b/lib/jekyll/layout_reader.rb @@ -17,7 +17,7 @@ class LayoutReader def layout_entries entries = [] - with(layout_directory) do + within(layout_directory) do entries = EntryFilter.new(site).filter(Dir['**/*.*']) end entries @@ -27,7 +27,7 @@ class LayoutReader file.split(".")[0..-2].join(".") end - def with(directory) + def within(directory) return unless File.exists?(directory) Dir.chdir(directory) { yield } end From c36a6d3e0dfd989615b1023fc88c369be8f88bf4 Mon Sep 17 00:00:00 2001 From: Matt Rogers & Persa Zula Date: Thu, 6 Feb 2014 23:07:38 -0600 Subject: [PATCH 7/7] Wrap the LayoutReader in the Jekyll module --- lib/jekyll/layout_reader.rb | 68 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/layout_reader.rb index 1f5e09a2..1b07c724 100644 --- a/lib/jekyll/layout_reader.rb +++ b/lib/jekyll/layout_reader.rb @@ -1,40 +1,40 @@ -class LayoutReader - attr_reader :site - def initialize(site) - @site = site - @layouts = {} - end - - def read - layout_entries.each do |f| - @layouts[layout_name(f)] = Layout.new(site, layout_directory, f) +module Jekyll + class LayoutReader + attr_reader :site + def initialize(site) + @site = site + @layouts = {} end - @layouts - end + def read + layout_entries.each do |f| + @layouts[layout_name(f)] = Layout.new(site, layout_directory, f) + end - private - - def layout_entries - entries = [] - within(layout_directory) do - entries = EntryFilter.new(site).filter(Dir['**/*.*']) + @layouts + end + + private + + def layout_entries + entries = [] + within(layout_directory) do + entries = EntryFilter.new(site).filter(Dir['**/*.*']) + end + entries + end + + def layout_name(file) + file.split(".")[0..-2].join(".") + end + + def within(directory) + return unless File.exists?(directory) + Dir.chdir(directory) { yield } + end + + def layout_directory + File.join(site.source, site.config['layouts']) end - entries end - - def layout_name(file) - file.split(".")[0..-2].join(".") - end - - def within(directory) - return unless File.exists?(directory) - Dir.chdir(directory) { yield } - end - - def layout_directory - File.join(site.source, site.config['layouts']) - end - - end