Class: Lifer::Entry::Markdown

Inherits:
Lifer::Entry show all
Defined in:
lib/lifer/entry/markdown.rb

Overview

We should initialize each Markdown file in a Lifer project as a ‘Lifer::Entry::Markdown` object. This class contains convenience methods for parsing a Markdown file with frontmatter as a weblog post or article. Of course, all frontmatter key-values will be available for users to render as they will in their template files.

it may make sense to pull some of these methods into a separate module.

Constant Summary

Constants inherited from Lifer::Entry

DEFAULT_DATE, FILENAME_DATE_FORMAT, TAG_DELIMITER_REGEX, TRUNCATION_THRESHOLD

Instance Attribute Summary

Attributes inherited from Lifer::Entry

#collection, #file

Instance Method Summary collapse

Methods inherited from Lifer::Entry

#authors, #body, #feedable?, #frontmatter, #full_text, generate, #initialize, manifest, #path, #permalink, #published_at, supported?, #tags, #updated_at

Constructor Details

This class inherits a constructor from Lifer::Entry

Instance Method Details

#summaryString

If given a summary in the frontmatter of the entry, we can use this to provide a summary. Otherwise, we can truncate the first paragraph and use that as a summary, although that is a bit annoying. This is useful for indexes and feeds and so on.

Returns:

  • (String)

    A summary of the entry.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lifer/entry/markdown.rb', line 30

def summary
  return super if super

  return if raw_first_paragraph_text.nil?

  text = raw_first_paragraph_text
  return text if text.length <= TRUNCATION_THRESHOLD

  truncated_text = text[0..TRUNCATION_THRESHOLD]
  if (index_of_final_fullstop = text.rindex ". ")
    truncated_text[0..index_of_final_fullstop]
  else
    "%s..." % truncated_text
  end
end

#titleString

The title or a default title.

Returns:

  • (String)

    The title of the entry.



49
50
51
# File 'lib/lifer/entry/markdown.rb', line 49

def title
  frontmatter[:title] || Lifer.setting(:entries, :default_title)
end

#to_htmlString

The HTML representation of the Markdown entry as parsed by Kramdown.

Returns:

  • (String)

    The HTML for the body of the entry.



61
62
63
# File 'lib/lifer/entry/markdown.rb', line 61

def to_html
  @to_html ||= Kramdown::Document.new(body).to_html
end