class Jekyll::Excerpt

Constants

LIQUID_TAG_REGEX

Internal: Extract excerpt from the content

By default excerpt is your first paragraph of a doc: 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 docs (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 docs:

# file: _config.yml
excerpt_separator: "<!-- more -->"

Notice that all markdown-style link references will be appended to the excerpt. So the example doc 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

Attributes

content[RW]
doc[RW]
ext[RW]
output[W]

Public Class Methods

new(doc) click to toggle source

Initialize this Excerpt instance.

doc - The Document.

Returns the new Excerpt.

# File lib/jekyll/excerpt.rb, line 23
def initialize(doc)
  self.doc = doc
  self.content = extract_excerpt(doc.content)
end

Public Instance Methods

data() click to toggle source

Fetch YAML front-matter data from related doc, without layout key

Returns Hash of doc data

# File lib/jekyll/excerpt.rb, line 31
def data
  @data ||= doc.data.dup
  @data.delete("layout")
  @data.delete("excerpt")
  @data
end
id() click to toggle source

The UID for this doc (useful in feeds). e.g. /2008/11/05/my-awesome-doc

Returns the String UID.

# File lib/jekyll/excerpt.rb, line 65
def id
  "#{doc.id}#excerpt"
end
include?(something) click to toggle source

Check if excerpt includes a string

Returns true if the string passed in

# File lib/jekyll/excerpt.rb, line 57
def include?(something)
  output&.include?(something) || content.include?(something)
end
inspect() click to toggle source

Returns the shorthand String identifier of this doc.

# File lib/jekyll/excerpt.rb, line 78
def inspect
  "<#{self.class} id=#{id}>"
end
output() click to toggle source
# File lib/jekyll/excerpt.rb, line 82
def output
  @output ||= Renderer.new(doc.site, self, site.site_payload).run
end
path() click to toggle source

‘Path’ of the excerpt.

Returns the path for the doc this excerpt belongs to with excerpt appended

# File lib/jekyll/excerpt.rb, line 43
def path
  File.join(doc.path, "#excerpt")
end
place_in_layout?() click to toggle source
# File lib/jekyll/excerpt.rb, line 86
def place_in_layout?
  false
end
relative_path() click to toggle source

‘Relative Path’ of the excerpt.

Returns the relative_path for the doc this excerpt belongs to with excerpt appended

# File lib/jekyll/excerpt.rb, line 50
def relative_path
  @relative_path ||= File.join(doc.relative_path, "#excerpt")
end
render_with_liquid?() click to toggle source
# File lib/jekyll/excerpt.rb, line 90
def render_with_liquid?
  return false if data["render_with_liquid"] == false

  !(coffeescript_file? || yaml_file? || !Utils.has_liquid_construct?(content))
end
to_liquid() click to toggle source
# File lib/jekyll/excerpt.rb, line 73
def to_liquid
  Jekyll::Drops::ExcerptDrop.new(self)
end
to_s() click to toggle source
# File lib/jekyll/excerpt.rb, line 69
def to_s
  output || content
end
trigger_hooks(*) click to toggle source
# File lib/jekyll/excerpt.rb, line 38
def trigger_hooks(*); end

Protected Instance Methods

extract_excerpt(doc_content) click to toggle source
# File lib/jekyll/excerpt.rb, line 136
def extract_excerpt(doc_content)
  head, _, tail = doc_content.to_s.partition(doc.excerpt_separator)
  return head if tail.empty?

  head = sanctify_liquid_tags(head) if head.include?("{%")
  definitions = extract_markdown_link_reference_definitions(head, tail)
  return head if definitions.empty?

  head << "\n\n" << definitions.join("\n")
end

Private Instance Methods

endtag_regex_stash(tag_name) click to toggle source
# File lib/jekyll/excerpt.rb, line 175
def endtag_regex_stash(tag_name)
  @endtag_regex_stash ||= {}
  @endtag_regex_stash[tag_name] ||= %r!{%-?\s*end#{tag_name}.*?\s*-?%}!m
end
liquid_block?(tag_name) click to toggle source
# File lib/jekyll/excerpt.rb, line 180
def liquid_block?(tag_name)
  return false unless tag_name.is_a?(String)
  return false unless Liquid::Template.tags[tag_name]

  Liquid::Template.tags[tag_name].ancestors.include?(Liquid::Block)
rescue NoMethodError
  Jekyll.logger.error "Error:",
                      "A Liquid tag in the excerpt of #{doc.relative_path} couldn't be parsed."
  raise
end
print_build_warning() click to toggle source
sanctify_liquid_tags(head) click to toggle source

append appropriate closing tag(s) (for each Liquid block), to the ‘head` if the partitioning resulted in leaving the closing tag somewhere in the `tail` partition.

# File lib/jekyll/excerpt.rb, line 151
def sanctify_liquid_tags(head)
  modified  = false
  tag_names = head.scan(LIQUID_TAG_REGEX)
  tag_names.flatten!
  tag_names.reverse_each do |tag_name|
    next unless liquid_block?(tag_name)
    next if endtag_regex_stash(tag_name).match?(head)

    modified = true
    head << "\n{% end#{tag_name} %}"
  end

  print_build_warning if modified
  head
end