Module: Middleman::Blog::BlogArticle

Defined in:
lib/middleman-blog/blog_article.rb

Overview

A module that adds blog-article methods to Resources.

Instance Method Summary collapse

Instance Method Details

#blog_dataObject



12
13
14
15
16
17
18
# File 'lib/middleman-blog/blog_article.rb', line 12

def blog_data
  if self.blog_controller
    self.blog_controller.data
  else
    app.blog
  end
end

#blog_optionsObject



20
21
22
23
24
25
26
# File 'lib/middleman-blog/blog_article.rb', line 20

def blog_options
  if self.blog_controller
    self.blog_controller.options
  else
    app.blog.options
  end
end

#bodyString

The body of this article, in HTML. This is for things like RSS feeds or lists of articles - individual articles will automatically be rendered from their template.

Returns:

  • (String)


74
75
76
# File 'lib/middleman-blog/blog_article.rb', line 74

def body
  render(:layout => false)
end

#dateTimeWithZone

Attempt to figure out the date of the post. The date should be present in the source path, but users may also provide a date in the frontmatter in order to provide a time of day for sorting reasons.

Returns:

  • (TimeWithZone)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/middleman-blog/blog_article.rb', line 141

def date
  return @_date if @_date

  frontmatter_date = data["date"]

  # First get the date from frontmatter
  if frontmatter_date.is_a? Time
    @_date = frontmatter_date.in_time_zone
  else
    @_date = Time.zone.parse(frontmatter_date.to_s)
  end

  # Next figure out the date from the filename
  if blog_options.sources.include?(":year") &&
      blog_options.sources.include?(":month") &&
      blog_options.sources.include?(":day")

    filename_date = Time.zone.local(path_part("year").to_i, path_part("month").to_i, path_part("day").to_i)
    if @_date
      raise "The date in #{path}'s filename doesn't match the date in its frontmatter" unless @_date.to_date == filename_date.to_date
    else
      @_date = filename_date.to_time.in_time_zone
    end
  end

  raise "Blog post #{path} needs a date in its filename or frontmatter" unless @_date

  @_date
end

#default_summary_generator(rendered, length, ellipsis) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/middleman-blog/blog_article.rb', line 103

def default_summary_generator(rendered, length, ellipsis)
  require 'middleman-blog/truncate_html'

  if rendered =~ blog_options.summary_separator
    rendered.split(blog_options.summary_separator).first
  elsif length
    TruncateHTML.truncate_html(rendered, length, ellipsis)
  else
    rendered
  end
end

#inspectObject



199
200
201
# File 'lib/middleman-blog/blog_article.rb', line 199

def inspect
  "#<Middleman::Blog::BlogArticle: #{data.inspect}>"
end

#next_articleMiddleman::Sitemap::Resource

The next (chronologically later) article after this one or nil if this is the most recent article.

Returns:

  • (Middleman::Sitemap::Resource)


195
196
197
# File 'lib/middleman-blog/blog_article.rb', line 195

def next_article
  blog_data.articles.reverse.find {|a| a.date > self.date }
end

#path_part(part) ⇒ String

Retrieve a section of the source path

Parameters:

  • The (String)

    part of the path, e.g. “year”, “month”, “day”, “title”

Returns:

  • (String)


130
131
132
133
# File 'lib/middleman-blog/blog_article.rb', line 130

def path_part(part)
  @_path_parts ||= blog_data.path_matcher.match(path).captures
  @_path_parts[blog_data.matcher_indexes[part]]
end

#previous_articleMiddleman::Sitemap::Resource

The previous (chronologically earlier) article before this one or nil if this is the first article.

Returns:

  • (Middleman::Sitemap::Resource)


188
189
190
# File 'lib/middleman-blog/blog_article.rb', line 188

def previous_article
  blog_data.articles.find {|a| a.date < self.date }
end

#published?Boolean

Whether or not this article has been published

An article is considered published in the following scenarios:

  1. frontmatter does not set published to false and either

  2. published_future_dated is true or

  3. article date is after the current time

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/middleman-blog/blog_article.rb', line 64

def published?
  (data["published"] != false) and
    (blog_options.publish_future_dated || date <= Time.current)
end

#render(opts = {}, locs = {}, &block) ⇒ String

Render this resource

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/middleman-blog/blog_article.rb', line 30

def render(opts={}, locs={}, &block)
  if opts[:layout].nil?
    if [:options] && ![:options][:layout].nil?
      opts[:layout] = [:options][:layout]
    end
    opts[:layout] = blog_options.layout if opts[:layout].nil?
    opts[:layout] = opts[:layout].to_s if opts[:layout].is_a? Symbol
  end

  content = super(opts, locs, &block)

  unless opts[:keep_separator]
    if content.match(blog_options.summary_separator)
      content.sub!(blog_options.summary_separator, "")
    end
  end

  content
end

#slugString

The “slug” of the article that shows up in its URL.

Returns:

  • (String)


173
174
175
176
177
178
179
180
181
182
183
# File 'lib/middleman-blog/blog_article.rb', line 173

def slug
  @_slug ||= data["slug"]

  @_slug ||= if blog_options.sources.include?(":title")
    path_part("title")
  elsif title
    title.parameterize
  else
    raise "Can't generate a slug for #{path} because it has no :title in its path pattern or title/slug in its frontmatter."
  end
end

#summary(length = blog_options.summary_length, ellipsis = '...') ⇒ String

The summary for this article, in HTML. The summary is either everything before the summary separator (set via :summary_separator and defaulting to “READMORE”) or the first :summary_length characters of the post.

:summary_generator can be set to a Proc in order to provide custom summary generation. The Proc is provided a parameter which is the rendered content of the article (without layout), the desired length to trim the summary to, and the ellipsis string to use.

Parameters:

  • length (Number) (defaults to: blog_options.summary_length)

    How many characters to trim the summary to.

  • ellipsis (String) (defaults to: '...')

    The ellipsis string to use when content is trimmed.

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/middleman-blog/blog_article.rb', line 91

def summary(length=blog_options.summary_length, ellipsis='...')
  rendered = render(:layout => false, :keep_separator => true)

  if blog_options.summary_separator && rendered.match(blog_options.summary_separator)
    rendered.split(blog_options.summary_separator).first
  elsif blog_options.summary_generator
    blog_options.summary_generator.call(self, rendered, length, ellipsis)
  else
    default_summary_generator(rendered, length, ellipsis)
  end
end

#tagsArray<String>

A list of tags for this article, set from frontmatter.

Returns:

  • (Array<String>)

    (never nil)



117
118
119
120
121
122
123
124
125
# File 'lib/middleman-blog/blog_article.rb', line 117

def tags
   = data["tags"]

  if .is_a? String
    .split(',').map(&:strip)
  else
     || []
  end
end

#titleString

The title of the article, set from frontmatter

Returns:

  • (String)


52
53
54
# File 'lib/middleman-blog/blog_article.rb', line 52

def title
  data["title"]
end