diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 4c1b0cc1..1b0eb519 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -50,11 +50,8 @@ Given /^I have an? (.*) directory$/ do |dir| FileUtils.mkdir_p(dir) end -Given /^I have the following (draft|post)s?(?: (.*) "(.*)")?:$/ do |type, direction, folder, table| - subdir = "_#{type}s" - +Given /^I have the following (draft|post)s?(?: (.*) "(.*)")?:$/ do |status, direction, folder, table| table.hashes.each do |post| - date = Date.strptime(post['date'], '%m/%d/%Y').strftime('%Y-%m-%d') title = post['title'].downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-') if direction && direction == "in" @@ -63,7 +60,14 @@ Given /^I have the following (draft|post)s?(?: (.*) "(.*)")?:$/ do |type, direct after = folder || '.' end - path = File.join(before || '.', subdir, after || '.', "#{date}-#{title}.#{post['type'] || 'textile'}") + ext = post['type'] || 'textile' + + if "draft" == status + path = File.join(before || '.', '_drafts', after || '.', "#{title}.#{ext}") + else + date = Date.strptime(post['date'], '%m/%d/%Y').strftime('%Y-%m-%d') + path = File.join(before || '.', '_posts', after || '.', "#{date}-#{title}.#{ext}") + end matter_hash = {} %w(title layout tag tags category categories published author).each do |key| diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 477247dd..0fa50895 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -33,6 +33,7 @@ require 'jekyll/convertible' require 'jekyll/layout' require 'jekyll/page' require 'jekyll/post' +require 'jekyll/draft' require 'jekyll/filters' require 'jekyll/static_file' require 'jekyll/errors' diff --git a/lib/jekyll/draft.rb b/lib/jekyll/draft.rb new file mode 100644 index 00000000..43a47e4c --- /dev/null +++ b/lib/jekyll/draft.rb @@ -0,0 +1,22 @@ +module Jekyll + + class Draft < Post + + # Valid post name regex (no date) + MATCHER = /^(.*)(\.[^.]+)$/ + + # Extract information from the post filename. + # + # name - The String filename of the post file. + # + # Returns nothing. + def process(name) + slug, ext = *name.match(MATCHER) + self.date = File.mtime(File.join(@base, name)) + self.slug = slug + self.ext = ext + end + + end + +end diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index f94169b0..1386c6ef 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -30,12 +30,11 @@ module Jekyll # site - The Site. # base - The String path to the dir containing the post file. # name - The String filename of the post file. - # subdir - The String path to the subdirectory. # # Returns the new Post. - def initialize(site, source, dir, name, subdir = '_posts') + def initialize(site, source, dir, name) @site = site - @base = File.join(source, dir, subdir) + @base = File.join(source, dir) @name = name self.categories = dir.split('/').reject { |x| x.empty? } diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 636045be..55964bee 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -137,10 +137,10 @@ module Jekyll base = File.join(self.source, dir) entries = Dir.chdir(base) { filter_entries(Dir.entries('.')) } - self.read_posts(dir,'_posts') + self.read_posts(dir) if self.show_drafts - self.read_posts(dir,'_drafts') + self.read_drafts(dir) end self.posts.sort! @@ -174,18 +174,46 @@ module Jekyll # object with each one. # # dir - The String relative path of the directory to read. - # subdir - The String relative path of the subdirectory to read. # # Returns nothing. - def read_posts(dir, subdir) - base = File.join(self.source, dir, subdir) + def read_posts(dir) + dir = File.join(dir, '_posts') + + base = File.join(self.source, dir) return unless File.exists?(base) entries = Dir.chdir(base) { filter_entries(Dir['**/*']) } # first pass processes, but does not yet render post content entries.each do |f| if Post.valid?(f) - post = Post.new(self, self.source, dir, f, subdir) + post = Post.new(self, self.source, dir, f) + + if post.published && (self.future || post.date <= self.time) + self.posts << post + post.categories.each { |c| self.categories[c] << post } + post.tags.each { |c| self.tags[c] << post } + end + end + 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) + dir = File.join(dir, '_drafts') + + base = File.join(self.source, dir) + return unless File.exists?(base) + entries = Dir.chdir(base) { filter_entries(Dir['**/*']) } + + # first pass processes, but does not yet render post content + entries.each do |f| + if Post.valid?(f) + post = Draft.new(self, self.source, dir, f) if post.published && (self.future || post.date <= self.time) self.posts << post