Skip to content

Instantly share code, notes, and snippets.

@michaelminter
Created September 10, 2013 15:28
Show Gist options
  • Select an option

  • Save michaelminter/6511096 to your computer and use it in GitHub Desktop.

Select an option

Save michaelminter/6511096 to your computer and use it in GitHub Desktop.
Inject ad container into content after n paragraphs
module PagesHelper
def content_with_ad(content, ad_count=3)
p_count = 0
new_content = content.gsub(/(<p(>|\s+[^>]*>).*?<\/p>)/) { |p|
p_count = p_count + 1
if p_count == ad_count
p + '<div class="above_fold_ad">Ad</div>'
else
p
end
}
new_content.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment