-
-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --drafts flag #769
Add --drafts flag #769
Changes from 10 commits
0ad623f
2df63e5
c48de6b
daa9e11
1ac46b1
642349f
78831d9
2588d68
3e164d6
538c208
1a35128
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Feature: Draft Posts | ||
As a hacker who likes to blog | ||
I want to be able to preview drafts locally | ||
In order to see if they look alright before publishing | ||
|
||
Scenario: Preview a draft | ||
Given I have a configuration file with "permalink" set to "none" | ||
And I have a _drafts directory | ||
And I have the following draft: | ||
| title | date | layout | content | | ||
| Recipe | 3/27/2009 | default | Not baked yet. | | ||
When I run jekyll with drafts | ||
Then the _site directory should exist | ||
And I should see "Not baked yet." in "_site/recipe.html" | ||
|
||
Scenario: Don't preview a draft | ||
Given I have a configuration file with "permalink" set to "none" | ||
And I have an "index.html" page that contains "Totally index" | ||
And I have a _drafts directory | ||
And I have the following draft: | ||
| title | date | layout | content | | ||
| Recipe | 3/27/2009 | default | Not baked yet. | | ||
When I run jekyll | ||
Then the _site directory should exist | ||
And the "_site/recipe.html" file should not exist |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Jekyll | ||
|
||
class Draft < Post | ||
|
||
# Valid post name regex (no date) | ||
MATCHER = /^(.*)(\.[^.]+)$/ | ||
|
||
# Draft name validator. Draft filenames must be like: | ||
# my-awesome-post.textile | ||
# | ||
# Returns true if valid, false if not. | ||
def self.valid?(name) | ||
name =~ MATCHER | ||
end | ||
|
||
# Get the full path to the directory containing the draft files | ||
def get_base(source, dir) | ||
return File.join(source, dir, '_drafts') | ||
end | ||
|
||
# Extract information from the post filename. | ||
# | ||
# name - The String filename of the post file. | ||
# | ||
# Returns nothing. | ||
def process(name) | ||
m, slug, ext = *name.match(MATCHER) | ||
self.date = File.mtime(File.join(@base, name)) | ||
self.slug = slug | ||
self.ext = ext | ||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,11 @@ def self.valid?(name) | |
# site - The Site. | ||
# base - The String path to the dir containing the post file. | ||
# name - The String filename of the post file. | ||
# categories - An Array of Strings for the categories for this post. | ||
# | ||
# Returns the new Post. | ||
def initialize(site, source, dir, name) | ||
@site = site | ||
@base = File.join(source, dir, '_posts') | ||
@base = self.get_base(source, dir) | ||
@name = name | ||
|
||
self.categories = dir.split('/').reject { |x| x.empty? } | ||
|
@@ -66,6 +65,11 @@ def initialize(site, source, dir, name) | |
end | ||
end | ||
|
||
# Get the full path to the directory containing the post files | ||
def get_base(source, dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. |
||
return File.join(source, dir, '_posts') | ||
end | ||
|
||
# Read the YAML frontmatter. | ||
# | ||
# base - The String path to the dir containing the file. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ class Site | |
attr_accessor :config, :layouts, :posts, :pages, :static_files, | ||
:categories, :exclude, :include, :source, :dest, :lsi, :pygments, | ||
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts, | ||
:keep_files | ||
:show_drafts, :keep_files | ||
|
||
attr_accessor :converters, :generators | ||
|
||
|
@@ -26,6 +26,7 @@ def initialize(config) | |
self.exclude = config['exclude'] || [] | ||
self.include = config['include'] || [] | ||
self.future = config['future'] | ||
self.show_drafts = config['drafts'] || nil | ||
self.limit_posts = config['limit_posts'] || nil | ||
self.keep_files = config['keep_files'] || [] | ||
|
||
|
@@ -138,6 +139,18 @@ def read_directories(dir = '') | |
|
||
self.read_posts(dir) | ||
|
||
if self.show_drafts | ||
self.read_drafts(dir) | ||
end | ||
|
||
self.posts.sort! | ||
|
||
# limit the posts if :limit_posts option is set | ||
if limit_posts | ||
limit = self.posts.length < limit_posts ? self.posts.length : limit_posts | ||
self.posts = self.posts[-limit, limit] | ||
end | ||
|
||
entries.each do |f| | ||
f_abs = File.join(base, f) | ||
f_rel = File.join(dir, f) | ||
|
@@ -180,13 +193,28 @@ def read_posts(dir) | |
end | ||
end | ||
end | ||
end | ||
|
||
self.posts.sort! | ||
# Read all the files in <source>/<dir>/_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) | ||
base = File.join(self.source, dir, '_drafts') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this is used here and in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should be a setting. I've attempted to refactor things to avoid the duplication, but it gets hairy and dilutes the focus of this pull request. Besides, it's the same problem with '_posts'. |
||
return unless File.exists?(base) | ||
entries = Dir.chdir(base) { filter_entries(Dir['**/*']) } | ||
|
||
# limit the posts if :limit_posts option is set | ||
if limit_posts | ||
limit = self.posts.length < limit_posts ? self.posts.length : limit_posts | ||
self.posts = self.posts[-limit, limit] | ||
# first pass processes, but does not yet render draft content | ||
entries.each do |f| | ||
if Draft.valid?(f) | ||
draft = Draft.new(self, self.source, dir, f) | ||
|
||
self.posts << draft | ||
draft.categories.each { |c| self.categories[c] << draft } | ||
draft.tags.each { |c| self.tags[c] << draft } | ||
end | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍