Skip to content

Instantly share code, notes, and snippets.

@1gor
Created July 18, 2012 07:21
Show Gist options
  • Save 1gor/3134795 to your computer and use it in GitHub Desktop.
Save 1gor/3134795 to your computer and use it in GitHub Desktop.
Nanoc helper to display blog post summary and a link to the full post
module My
module Helpers
# Nanoc helper to display blog post summary and a link to the full post.
# Used inside <% sorted_articles.each do |item| %>...<% end %> block etc.
#
# @example Put the following in your layout:
#
# <%= article_summary(item,'Read the full article>>') %>
#
# To customize the link text you can add 'read_more' attribute to your
# item metadata or pass the string to the helper, as above.
#
# Add <!--MORE--> separator somewhere in your item to split it. Otherwise
# the full article text is displayed.
#
# @param [String] item The blog post
#
# @param [String] read_more_text The 'Read more...' text
#
# @param [String] separator Separates item summary from item body. Defaults to <!--MORE-->
#
def article_summary(item, read_more_text="Read more...", separator="<!--MORE-->")
summary,body = item.compiled_content.split(separator)
return item.compiled_content unless body
link = link_to( (item[:read_more] || read_more_text), item.path, :class=>'readmore' )
return summary+"<p class='readmore'>#{link}</p>"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment