From 54d0bf47e88f8a3ae789b330b78c425a27ca7b08 Mon Sep 17 00:00:00 2001 From: Malo Skrylevo Date: Thu, 8 Jan 2015 15:10:53 +0300 Subject: [PATCH 1/3] Added per post excerpt_separator functionality, so you are able to specify :excerpt_separator (as well as just :excerpt) key direct inside the post YAML, to make an excerpt based on the value in the post. Tests were also added. --- .gitignore | 1 + lib/jekyll/excerpt.rb | 4 +++- lib/jekyll/post.rb | 10 +++++++++- site/_docs/posts.md | 10 ++++++++++ .../2015-01-08-post-excerpt-separator.markdown | 15 +++++++++++++++ test/test_generated_site.rb | 2 +- test/test_post.rb | 16 ++++++++++++++++ 7 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 test/source/_posts/2015-01-08-post-excerpt-separator.markdown diff --git a/.gitignore b/.gitignore index d9effd5b..8c1da722 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ gh-pages/ site/_site/ coverage .ruby-version +.ruby-gemset .sass-cache tmp/stackprof-* .jekyll-metadata diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 41d4976c..6e328a30 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -105,7 +105,9 @@ module Jekyll # # Returns excerpt String def extract_excerpt(post_content) - separator = site.config['excerpt_separator'] + separator = !post.excerpt_separator.empty? && + post.excerpt_separator || + site.config['excerpt_separator'] head, _, tail = post_content.to_s.partition(separator) "" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n") diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index c377e376..89d1c01a 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -23,6 +23,7 @@ module Jekyll ATTRIBUTES_FOR_LIQUID = EXCERPT_ATTRIBUTES_FOR_LIQUID + %w[ content excerpt + excerpt_separator ] # Post name validator. Post filenames must be like: @@ -118,6 +119,13 @@ module Jekyll data.fetch('title') { titleized_slug } end + # Public: the Post excerpt_separator, from the YAML Front-Matter or empty string + # + # Returns the post excerpt_separator + def excerpt_separator + data.fetch('excerpt_separator') { "" } + end + # Turns the post slug into a suitable title def titleized_slug slug.split('-').select {|w| w.capitalize! || w }.join(' ') @@ -307,7 +315,7 @@ module Jekyll end def generate_excerpt? - !(site.config['excerpt_separator'].to_s.empty?) + !site.config['excerpt_separator'].to_s.empty? || !excerpt_separator.empty? end end end diff --git a/site/_docs/posts.md b/site/_docs/posts.md index 504ea2fb..7afd88dc 100644 --- a/site/_docs/posts.md +++ b/site/_docs/posts.md @@ -171,6 +171,16 @@ your `excerpt_separator` to `""`. Also, as with any output generated by Liquid tags, you can pass the `| strip_html` flag to remove any html tags in the output. This is particularly helpful if you wish to output a post excerpt as a `meta="description"` tag within the post `head`, or anywhere else having html tags along with the content is not desirable. +Additionally you are able to specify per-post `excerpt_separator` value if it is required just only the the selected post. Just specify the `excerpt_separator` with the same way as `excerpt` in the post's YAML head: + + --- + excerpt_separator: + --- + + Excerpt + + Out-of-excerpt + ## Highlighting code snippets Jekyll also has built-in support for syntax highlighting of code snippets using diff --git a/test/source/_posts/2015-01-08-post-excerpt-separator.markdown b/test/source/_posts/2015-01-08-post-excerpt-separator.markdown new file mode 100644 index 00000000..e8b7ddd9 --- /dev/null +++ b/test/source/_posts/2015-01-08-post-excerpt-separator.markdown @@ -0,0 +1,15 @@ +--- +layout: ~ +title: Post Excerpt Separator +excerpt_separator: "\n---\n" +--- + +First paragraph with [link ref][link]. + +Second paragraph + +--- + +Third paragraph + +[link]: http://www.jekyllrb.com/ diff --git a/test/test_generated_site.rb b/test/test_generated_site.rb index 0b157e85..03669b90 100644 --- a/test/test_generated_site.rb +++ b/test/test_generated_site.rb @@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase end should "ensure post count is as expected" do - assert_equal 44, @site.posts.size + assert_equal 45, @site.posts.size end should "insert site.posts into the index" do diff --git a/test/test_post.rb b/test/test_post.rb index 2592b918..1826674d 100644 --- a/test/test_post.rb +++ b/test/test_post.rb @@ -397,6 +397,22 @@ class TestPost < Test::Unit::TestCase end end + context "with page's excerpt_separator setting" do + setup do + file = "2015-01-08-post-excerpt-separator.markdown" + + @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 + end + context "with custom excerpt" do setup do file = "2013-04-11-custom-excerpt.markdown" From a0f2b5f944cee9648e5931bd6d2502ce74af6d84 Mon Sep 17 00:00:00 2001 From: Malo Skrylevo Date: Sat, 10 Jan 2015 03:58:02 +0300 Subject: [PATCH 2/3] get procedure for default excerpt separator for both cases site and page was moved to the post's specific method :excerpt_separator. --- lib/jekyll/excerpt.rb | 5 +---- lib/jekyll/post.rb | 7 ++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/excerpt.rb b/lib/jekyll/excerpt.rb index 6e328a30..347be217 100644 --- a/lib/jekyll/excerpt.rb +++ b/lib/jekyll/excerpt.rb @@ -105,10 +105,7 @@ module Jekyll # # Returns excerpt String def extract_excerpt(post_content) - separator = !post.excerpt_separator.empty? && - post.excerpt_separator || - site.config['excerpt_separator'] - head, _, tail = post_content.to_s.partition(separator) + head, _, tail = post_content.to_s.partition(post.excerpt_separator) "" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n") end diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index 89d1c01a..c94d66b6 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -119,11 +119,12 @@ module Jekyll data.fetch('title') { titleized_slug } end - # Public: the Post excerpt_separator, from the YAML Front-Matter or empty string + # Public: the Post excerpt_separator, from the YAML Front-Matter or site default + # excerpt_separator value # # Returns the post excerpt_separator def excerpt_separator - data.fetch('excerpt_separator') { "" } + data.fetch('excerpt_separator') { site.config['excerpt_separator'].to_s } end # Turns the post slug into a suitable title @@ -315,7 +316,7 @@ module Jekyll end def generate_excerpt? - !site.config['excerpt_separator'].to_s.empty? || !excerpt_separator.empty? + !excerpt_separator.empty? end end end From 18f3d7660357d352b20e9e20484f77a9b3e3659e Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Sat, 17 Jan 2015 14:59:44 -0800 Subject: [PATCH 3/3] Ensure Post#excerpt_separator always returns a string. --- lib/jekyll/post.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/post.rb b/lib/jekyll/post.rb index c94d66b6..384a10ee 100644 --- a/lib/jekyll/post.rb +++ b/lib/jekyll/post.rb @@ -124,7 +124,7 @@ module Jekyll # # Returns the post excerpt_separator def excerpt_separator - data.fetch('excerpt_separator') { site.config['excerpt_separator'].to_s } + (data['excerpt_separator'] || site.config['excerpt_separator']).to_s end # Turns the post slug into a suitable title