Skip to content

Commit

Permalink
Merge pull request #837 from ixti/feature-excerpt
Browse files Browse the repository at this point in the history
Adds excerpt to posts
  • Loading branch information
parkr committed Mar 17, 2013
2 parents 43213c2 + 66c5ef2 commit 936ed1f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/jekyll.rb
Expand Up @@ -74,6 +74,8 @@ module Jekyll
'markdown_ext' => 'markdown,mkd,mkdn,md',
'textile_ext' => 'textile',

'excerpt_separator' => "\n\n",

'maruku' => {
'use_tex' => false,
'use_divs' => false,
Expand Down
58 changes: 55 additions & 3 deletions lib/jekyll/post.rb
Expand Up @@ -19,7 +19,7 @@ def self.valid?(name)
end

attr_accessor :site
attr_accessor :data, :content, :output, :ext
attr_accessor :data, :excerpt, :content, :output, :ext
attr_accessor :date, :slug, :published, :tags, :categories

attr_reader :name
Expand Down Expand Up @@ -77,8 +77,8 @@ def containing_dir(source, dir)
# Returns nothing.
def read_yaml(base, name)
super(base, name)
self.excerpt = self.extract_excerpt
self.data['layout'] = 'post' unless self.data.has_key?('layout')
self.data
end

# Compares Post objects. First compares the Post date. If the dates are
Expand Down Expand Up @@ -109,6 +109,14 @@ def process(name)
raise FatalException.new("Post #{name} does not have a valid date.")
end

# Transform the contents and excerpt based on the content type.
#
# Returns nothing.
def transform
super
self.excerpt = converter.convert(self.excerpt)
end

# The generated directory into which the post will be placed
# upon generation. This is derived from the permalink or, if
# permalink is absent, set to the default date
Expand Down Expand Up @@ -257,7 +265,8 @@ def to_liquid
"next" => self.next,
"previous" => self.previous,
"tags" => self.tags,
"content" => self.content })
"content" => self.content,
"excerpt" => self.excerpt })
end

# Returns the shorthand String identifier of this Post.
Expand All @@ -283,5 +292,48 @@ def previous
nil
end
end

protected

# Internal: Extract excerpt from the content
#
# By default excerpt is your first paragraph of a post: everything before
# the first two new lines:
#
# ---
# title: Example
# ---
#
# First paragraph with [link][1].
#
# Second paragraph.
#
# [1]: http://example.com/
#
# This is fairly good option for Markdown and Textile files. But might cause
# problems for HTML posts (which is quite unusual for Jekyll). If default
# excerpt delimiter is not good for you, you might want to set your own via
# configuration option `excerpt_separator`. For example, following is a good
# alternative for HTML posts:
#
# # file: _config.yml
# excerpt_separator: "<!-- more -->"
#
# Notice that all markdown-style link references will be appended to the
# excerpt. So the example post above will have this excerpt source:
#
# First paragraph with [link][1].
#
# [1]: http://example.com/
#
# Excerpts are rendered same time as content is rendered.
#
# Returns excerpt String
def extract_excerpt
separator = self.site.config['excerpt_separator']
head, _, tail = self.content.partition(separator)

"" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n")
end
end
end
14 changes: 14 additions & 0 deletions test/source/_posts/2013-01-02-post-excerpt.markdown
@@ -0,0 +1,14 @@
---
layout: ~
title: Post Excerpt
---

First paragraph with [link ref][link].

Second paragraph

---

Third paragraph

[link]: http://www.jekyllrb.com/
2 changes: 1 addition & 1 deletion test/test_generated_site.rb
Expand Up @@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
end

should "ensure post count is as expected" do
assert_equal 30, @site.posts.size
assert_equal 31, @site.posts.size
end

should "insert site.posts into the index" do
Expand Down
46 changes: 46 additions & 0 deletions test/test_post.rb
Expand Up @@ -252,6 +252,52 @@ def do_render(post)

assert_equal "<h1>{{ page.title }}</h1>\n<p>Best <strong>post</strong> ever</p>", @post.content
end

context "#excerpt" do
setup do
file = "2013-01-02-post-excerpt.markdown"
@post.process(file)
@post.read_yaml(@source, file)
@post.transform
end

should "return first paragraph by default" do
assert @post.excerpt.include?("First paragraph"), "contains first paragraph"
assert !@post.excerpt.include?("Second paragraph"), "does not contains second paragraph"
assert !@post.excerpt.include?("Third paragraph"), "does not contains third paragraph"
end

should "correctly resolve link references" do
assert @post.excerpt.include?("www.jekyllrb.com"), "contains referenced link URL"
end

should "return rendered HTML" do
assert_equal "<p>First paragraph with <a href='http://www.jekyllrb.com/'>link ref</a>.</p>",
@post.excerpt
end

context "with excerpt_separator setting" do
setup do
file = "2013-01-02-post-excerpt.markdown"

@post.site.config['excerpt_separator'] = "\n---\n"

@post.process(file)
@post.read_yaml(@source, file)
@post.transform
end

should "respect given separator" do
assert @post.excerpt.include?("First paragraph"), "contains first paragraph"
assert @post.excerpt.include?("Second paragraph"), "contains second paragraph"
assert !@post.excerpt.include?("Third paragraph"), "does not contains third paragraph"
end

should "replace separator with new-lines" do
assert !@post.excerpt.include?("---"), "does not contains separator"
end
end
end
end

context "when in a site" do
Expand Down

0 comments on commit 936ed1f

Please sign in to comment.