Module: Nanoc::Helpers::Blogging
- Included in:
- AtomFeedBuilder
- Defined in:
- lib/nanoc/helpers/blogging.rb
Overview
Defined Under Namespace
Classes: AtomFeedBuilder
Instance Method Summary collapse
- #articles ⇒ Array
- #atom_feed(params = {}) ⇒ String
- #atom_tag_for(item) ⇒ String
- #attribute_to_time(arg) ⇒ Time
- #feed_url ⇒ String
- #sorted_articles ⇒ Array
- #url_for(item) ⇒ String
Instance Method Details
#articles ⇒ Array
7 8 9 10 11 12 13 14 |
# File 'lib/nanoc/helpers/blogging.rb', line 7 def articles blk = -> { @items.select { |item| item[:kind] == 'article' } } if @items.frozen? @article_items ||= blk.call else blk.call end end |
#atom_feed(params = {}) ⇒ String
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/nanoc/helpers/blogging.rb', line 191 def atom_feed(params = {}) require 'builder' # Create builder builder = AtomFeedBuilder.new(@config, @item) # Fill builder builder.alt_link = params[:alt_link] builder.id = params[:id] builder.limit = params[:limit] || 5 builder.relevant_articles = params[:articles] || articles || [] builder.preserve_order = params.fetch(:preserve_order, false) builder.content_proc = params[:content_proc] || ->(a) { a.compiled_content(snapshot: :pre) } builder.excerpt_proc = params[:excerpt_proc] || ->(a) { a[:excerpt] } builder.title_proc = params[:title_proc] || ->(a) { a[:title] } builder.title = params[:title] || @item[:title] || @config[:title] builder. = params[:author_name] || @item[:author_name] || @config[:author_name] builder. = params[:author_uri] || @item[:author_uri] || @config[:author_uri] builder.icon = params[:icon] builder.logo = params[:logo] # Run builder.validate builder.build end |
#atom_tag_for(item) ⇒ String
245 246 247 248 249 250 251 |
# File 'lib/nanoc/helpers/blogging.rb', line 245 def atom_tag_for(item) hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@config[:base_url])[1..2] formatted_date = attribute_to_time(item[:created_at]).__nanoc_to_iso8601_date 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier.to_s) end |
#attribute_to_time(arg) ⇒ Time
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/nanoc/helpers/blogging.rb', line 256 def attribute_to_time(arg) case arg when DateTime arg.to_time when Date Time.utc(arg.year, arg.month, arg.day) when String Time.parse(arg) else arg end end |
#feed_url ⇒ String
235 236 237 238 239 240 241 242 |
# File 'lib/nanoc/helpers/blogging.rb', line 235 def feed_url # Check attributes if @config[:base_url].nil? raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: site configuration has no base_url') end @item[:feed_url] || @config[:base_url] + @item.path end |
#sorted_articles ⇒ Array
17 18 19 20 21 22 23 24 25 |
# File 'lib/nanoc/helpers/blogging.rb', line 17 def sorted_articles blk = -> { articles.sort_by { |a| attribute_to_time(a[:created_at]) }.reverse } if @items.frozen? @sorted_article_items ||= blk.call else blk.call end end |
#url_for(item) ⇒ String
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/nanoc/helpers/blogging.rb', line 218 def url_for(item) # Check attributes if @config[:base_url].nil? raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: site configuration has no base_url') end # Build URL if item[:custom_url_in_feed] item[:custom_url_in_feed] elsif item[:custom_path_in_feed] @config[:base_url] + item[:custom_path_in_feed] elsif item.path @config[:base_url] + item.path end end |