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
5 6 7 8 9 10 11 12 |
# File 'lib/nanoc/helpers/blogging.rb', line 5 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/nanoc/helpers/blogging.rb', line 184 def atom_feed(params = {}) require 'builder' # Create builder builder = AtomFeedBuilder.new(@config, @item) # Fill builder 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 = 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
235 236 237 238 239 240 241 |
# File 'lib/nanoc/helpers/blogging.rb', line 235 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
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/nanoc/helpers/blogging.rb', line 246 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
225 226 227 228 229 230 231 232 |
# File 'lib/nanoc/helpers/blogging.rb', line 225 def feed_url # Check attributes if @config[:base_url].nil? raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url') end @item[:feed_url] || @config[:base_url] + @item.path end |
#sorted_articles ⇒ Array
15 16 17 18 19 20 21 22 23 |
# File 'lib/nanoc/helpers/blogging.rb', line 15 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/nanoc/helpers/blogging.rb', line 208 def url_for(item) # Check attributes if @config[:base_url].nil? raise Nanoc::Int::Errors::GenericTrivial.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 |