Created
May 17, 2011 16:58
-
-
Save afiore/976851 to your computer and use it in GitHub Desktop.
A nanoc helper for extracting blog post summaries through a <!--READMORE--> marker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.*"), " <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