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
- #blog_data ⇒ Object
- #blog_options ⇒ Object
-
#body ⇒ String
The body of this article, in HTML.
-
#date ⇒ TimeWithZone
Attempt to figure out the date of the post.
- #default_summary_generator(rendered, length, ellipsis) ⇒ Object
- #inspect ⇒ Object
-
#next_article ⇒ Middleman::Sitemap::Resource
The next (chronologically later) article after this one or nil if this is the most recent article.
-
#path_part(part) ⇒ String
Retrieve a section of the source path.
-
#previous_article ⇒ Middleman::Sitemap::Resource
The previous (chronologically earlier) article before this one or nil if this is the first article.
-
#published? ⇒ Boolean
Whether or not this article has been published.
-
#render(opts = {}, locs = {}, &block) ⇒ String
Render this resource.
-
#slug ⇒ String
The “slug” of the article that shows up in its URL.
-
#summary(length = blog_options.summary_length, ellipsis = '...') ⇒ String
The summary for this article, in HTML.
-
#tags ⇒ Array<String>
A list of tags for this article, set from frontmatter.
-
#title ⇒ String
The title of the article, set from frontmatter.
Instance Method Details
#blog_data ⇒ Object
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_options ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/middleman-blog/blog_article.rb', line 20 def if self.blog_controller self.blog_controller. else app.blog. end end |
#body ⇒ String
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.
74 75 76 |
# File 'lib/middleman-blog/blog_article.rb', line 74 def body render(:layout => false) end |
#date ⇒ TimeWithZone
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.
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 .sources.include?(":year") && .sources.include?(":month") && .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 =~ .summary_separator rendered.split(.summary_separator).first elsif length TruncateHTML.truncate_html(rendered, length, ellipsis) else rendered end end |
#inspect ⇒ Object
199 200 201 |
# File 'lib/middleman-blog/blog_article.rb', line 199 def inspect "#<Middleman::Blog::BlogArticle: #{data.inspect}>" end |
#next_article ⇒ Middleman::Sitemap::Resource
The next (chronologically later) article after this one or nil if this is the most recent article.
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
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_article ⇒ Middleman::Sitemap::Resource
The previous (chronologically earlier) article before this one or nil if this is the first article.
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:
-
frontmatter does not set published to false and either
-
published_future_dated is true or
-
article date is after the current time
64 65 66 67 |
# File 'lib/middleman-blog/blog_article.rb', line 64 def published? (data["published"] != false) and (.publish_future_dated || date <= Time.current) end |
#render(opts = {}, locs = {}, &block) ⇒ String
Render this resource
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] = .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(.summary_separator) content.sub!(.summary_separator, "") end end content end |
#slug ⇒ String
The “slug” of the article that shows up in its URL.
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 .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.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/middleman-blog/blog_article.rb', line 91 def summary(length=.summary_length, ellipsis='...') rendered = render(:layout => false, :keep_separator => true) if .summary_separator && rendered.match(.summary_separator) rendered.split(.summary_separator).first elsif .summary_generator .summary_generator.call(self, rendered, length, ellipsis) else default_summary_generator(rendered, length, ellipsis) end end |
#tags ⇒ Array<String>
A list of tags for this article, set from frontmatter.
117 118 119 120 121 122 123 124 125 |
# File 'lib/middleman-blog/blog_article.rb', line 117 def = data["tags"] if .is_a? String .split(',').map(&:strip) else || [] end end |
#title ⇒ String
The title of the article, set from frontmatter
52 53 54 |
# File 'lib/middleman-blog/blog_article.rb', line 52 def title data["title"] end |