Merge pull request #1302 from mojombo/excerpt
Add support for liquid in post excerpts
This commit is contained in:
		
						commit
						17c2296ff6
					
				|  | @ -36,6 +36,7 @@ require 'jekyll/convertible' | |||
| require 'jekyll/layout' | ||||
| require 'jekyll/page' | ||||
| require 'jekyll/post' | ||||
| require 'jekyll/excerpt' | ||||
| require 'jekyll/draft' | ||||
| require 'jekyll/filters' | ||||
| require 'jekyll/static_file' | ||||
|  |  | |||
|  | @ -0,0 +1,140 @@ | |||
| module Jekyll | ||||
|   class Excerpt | ||||
| 
 | ||||
|     # 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 | ||||
| 
 | ||||
|     include Convertible | ||||
| 
 | ||||
|     attr_accessor :post | ||||
|     attr_accessor :content, :output, :ext | ||||
| 
 | ||||
|     # Initialize this Post instance. | ||||
|     # | ||||
|     # site       - The Site. | ||||
|     # base       - The String path to the dir containing the post file. | ||||
|     # name       - The String filename of the post file. | ||||
|     # | ||||
|     # Returns the new Post. | ||||
|     def initialize(post) | ||||
|       @post = post | ||||
|       @content = extract_excerpt(post.content) | ||||
|     end | ||||
| 
 | ||||
|     %w[site name data ext].each do |meth| | ||||
|       define_method(meth) do | ||||
|         post.send(meth) | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     def path | ||||
|       File.join(post.path, "#excerpt") | ||||
|     end | ||||
| 
 | ||||
|     def include?(something) | ||||
|       (output && output.include?(something)) || content.include?(something) | ||||
|     end | ||||
| 
 | ||||
|     # The UID for this post (useful in feeds). | ||||
|     # e.g. /2008/11/05/my-awesome-post | ||||
|     # | ||||
|     # Returns the String UID. | ||||
|     def id | ||||
|       File.join(post.dir, post.slug, "#excerpt") | ||||
|     end | ||||
| 
 | ||||
|     # Convert this post into a Hash for use in Liquid templates. | ||||
|     # | ||||
|     # Returns the representative Hash. | ||||
|     def to_liquid | ||||
|       further_data = Hash[ATTRIBUTES_FOR_LIQUID.map { |attribute| | ||||
|         [attribute, post.send(attribute)] | ||||
|       }] | ||||
|       further_data | ||||
|     end | ||||
| 
 | ||||
|     def to_s | ||||
|       output || content | ||||
|     end | ||||
| 
 | ||||
|     # Returns the shorthand String identifier of this Post. | ||||
|     def inspect | ||||
|       "<Excerpt: #{self.id}>" | ||||
|     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(post_content) | ||||
|       separator     = site.config['excerpt_separator'] | ||||
|       head, _, tail = post_content.partition(separator) | ||||
| 
 | ||||
|       "" << head << "\n\n" << tail.scan(/^\[[^\]]+\]:.+$/).join("\n") | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | @ -109,7 +109,7 @@ module Jekyll | |||
|       if self.data.has_key? 'excerpt' | ||||
|         self.data['excerpt'] | ||||
|       else | ||||
|         self.extracted_excerpt | ||||
|         self.extracted_excerpt.to_s | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|  | @ -158,14 +158,6 @@ module Jekyll | |||
|       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.extracted_excerpt = converter.convert(self.extracted_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 | ||||
|  | @ -260,6 +252,8 @@ module Jekyll | |||
|         "page" => self.to_liquid | ||||
|       }.deep_merge(site_payload) | ||||
| 
 | ||||
|       self.extracted_excerpt.do_layout(payload, layouts) | ||||
| 
 | ||||
|       do_layout(payload, layouts) | ||||
|     end | ||||
| 
 | ||||
|  | @ -311,45 +305,8 @@ module Jekyll | |||
| 
 | ||||
|     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") | ||||
|       Jekyll::Excerpt.new(self) | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  |  | |||
|  | @ -267,9 +267,10 @@ class TestPost < Test::Unit::TestCase | |||
|       context "#excerpt" do | ||||
|         setup do | ||||
|           file = "2013-01-02-post-excerpt.markdown" | ||||
|           @post = setup_post(file) | ||||
|           @post.process(file) | ||||
|           @post.read_yaml(@source, file) | ||||
|           @post.transform | ||||
|           do_render(@post) | ||||
|         end | ||||
| 
 | ||||
|         should "return first paragraph by default" do | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue