Skip to content

Instantly share code, notes, and snippets.

@afiore
Created May 17, 2011 16:58
Show Gist options
  • Save afiore/976851 to your computer and use it in GitHub Desktop.
Save afiore/976851 to your computer and use it in GitHub Desktop.
A nanoc helper for extracting blog post summaries through a <!--READMORE--> marker
require 'rubygems'
require 'nokogiri'
module Helpers
def blog_excerpt(item, read_more='<!--READMORE-->')
post_dom = Nokogiri::HTML::DocumentFragment.parse(item.compiled_content)
more_tag_found = false
post_dom.css('*').each do |p, index|
if p.inner_html.match(read_more)
p.inner_html = p.inner_html.gsub(Regexp.new("#{read_more}.*"), "&nbsp; <a href=\"#{item.identifier}\" title=\"#{item[:title]}\" class=\"read-more\">Read more...</a>")
more_tag_found = true
next
end
p.remove if more_tag_found
end
more_tag_found ?
post_dom.to_s :
item.compiled_content
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment