From bebd80342e4f17bb9bff7240519a192cf06973bd Mon Sep 17 00:00:00 2001 From: Martin Jorn Rogalla Date: Fri, 6 Mar 2015 12:39:50 +0100 Subject: [PATCH] Extracted draft, post reader into external classes. Organized the draft, post and layout reader into the *readers* classes. Fixed all references and ran tests. Signed-off-by: Martin Jorn Rogalla --- lib/jekyll.rb | 4 ++- lib/jekyll/reader.rb | 39 ++++------------------- lib/jekyll/readers/draft_reader.rb | 20 ++++++++++++ lib/jekyll/{ => readers}/layout_reader.rb | 0 lib/jekyll/readers/post_reader.rb | 20 ++++++++++++ lib/jekyll/site.rb | 2 +- test/test_site.rb | 2 +- test/test_tags.rb | 2 +- 8 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 lib/jekyll/readers/draft_reader.rb rename lib/jekyll/{ => readers}/layout_reader.rb (100%) create mode 100644 lib/jekyll/readers/post_reader.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 5beca7a5..f524c29f 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -49,7 +49,9 @@ module Jekyll autoload :Filters, 'jekyll/filters' autoload :FrontmatterDefaults, 'jekyll/frontmatter_defaults' autoload :Layout, 'jekyll/layout' - autoload :LayoutReader, 'jekyll/layout_reader' + autoload :LayoutReader, 'jekyll/readers/layout_reader' + autoload :DraftReader, 'jekyll/readers/draft_reader' + autoload :PostReader, 'jekyll/readers/post_reader' autoload :LogAdapter, 'jekyll/log_adapter' autoload :Page, 'jekyll/page' autoload :PluginManager, 'jekyll/plugin_manager' diff --git a/lib/jekyll/reader.rb b/lib/jekyll/reader.rb index 79464d46..5e44f279 100644 --- a/lib/jekyll/reader.rb +++ b/lib/jekyll/reader.rb @@ -71,8 +71,12 @@ module Jekyll base = site.in_source_dir(dir) entries = Dir.chdir(base) { filter_entries(Dir.entries('.'), base) } - read_posts(dir) - read_drafts(dir) if site.show_drafts + site.posts += PostReader.new(site).read(dir) + + if site.show_drafts + site.posts += DraftReader.new(site).read(dir) + end + site.posts.sort! limit_posts if site.limit_posts > 0 # limit the posts if :limit_posts option is set @@ -88,41 +92,10 @@ module Jekyll site.static_files << StaticFile.new(site, site.source, dir, f) end end - site.pages.sort_by!(&:name) site.static_files.sort_by!(&:relative_path) end - # Read all the files in //_posts and create a new Post - # object with each one. - # - # dir - The String relative path of the directory to read. - # - # Returns nothing. - def read_posts(dir) - posts = read_content(dir, '_posts', Post) - - posts.each do |post| - aggregate_post_info(post) if site.publisher.publish?(post) - end - end - - # Read all the files in //_drafts and create a new Post - # object with each one. - # - # dir - The String relative path of the directory to read. - # - # Returns nothing. - def read_drafts(dir) - drafts = read_content(dir, '_drafts', Draft) - - drafts.each do |draft| - if draft.published? - aggregate_post_info(draft) - end - end - end - # Read all the content files from //magic_dir # and return them with the type klass. # diff --git a/lib/jekyll/readers/draft_reader.rb b/lib/jekyll/readers/draft_reader.rb new file mode 100644 index 00000000..ac6ea964 --- /dev/null +++ b/lib/jekyll/readers/draft_reader.rb @@ -0,0 +1,20 @@ +module Jekyll + class DraftReader + attr_reader :site, :unfiltered_content + def initialize(site) + @site = site + @unfiltered_content = Array.new + end + + # Read all the files in //_drafts and create a new Draft + # object with each one. + # + # dir - The String relative path of the directory to read. + # + # Returns nothing. + def read(dir) + @unfiltered_content = @site.reader.read_content(dir, '_drafts', Draft) + @unfiltered_content.select{ |draft| site.publisher.publish?(draft) } + end + end +end diff --git a/lib/jekyll/layout_reader.rb b/lib/jekyll/readers/layout_reader.rb similarity index 100% rename from lib/jekyll/layout_reader.rb rename to lib/jekyll/readers/layout_reader.rb diff --git a/lib/jekyll/readers/post_reader.rb b/lib/jekyll/readers/post_reader.rb new file mode 100644 index 00000000..471f1414 --- /dev/null +++ b/lib/jekyll/readers/post_reader.rb @@ -0,0 +1,20 @@ +module Jekyll + class PostReader + attr_reader :site, :unfiltered_content + def initialize(site) + @site = site + @unfiltered_content = Array.new + end + + # Read all the files in //_posts and create a new Post + # object with each one. + # + # dir - The String relative path of the directory to read. + # + # Returns nothing. + def read(dir) + @unfiltered_content = @site.reader.read_content(dir, '_posts', Post) + @unfiltered_content.select{ |post| site.publisher.publish?(post) } + end + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 1e902e2d..af54f088 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -4,7 +4,7 @@ require 'csv' module Jekyll class Site attr_reader :source, :dest, :config - attr_accessor :layouts, :posts, :pages, :static_files, + attr_accessor :layouts, :posts, :pages, :static_files, :drafts, :exclude, :include, :lsi, :highlighter, :permalink_style, :time, :future, :unpublished, :safe, :plugins, :limit_posts, :show_drafts, :keep_files, :baseurl, :data, :file_read_opts, diff --git a/test/test_site.rb b/test/test_site.rb index c132bdda..e77a5ad1 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -189,7 +189,7 @@ class TestSite < JekyllUnitTest end should "read posts" do - @site.reader.read_posts('') + @site.posts += PostReader.new(@site).read('') posts = Dir[source_dir('_posts', '**', '*')] posts.delete_if { |post| File.directory?(post) && !Post.valid?(post) } assert_equal posts.size - @num_invalid_posts, @site.posts.size diff --git a/test/test_tags.rb b/test/test_tags.rb index eb998423..72d3a5e1 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -13,7 +13,7 @@ class TestTags < JekyllUnitTest site = Site.new(Jekyll.configuration) if override['read_posts'] - site.reader.read_posts('') + site.posts += PostReader.new(site).read('') end info = { :filters => [Jekyll::Filters], :registers => { :site => site } }