Skip to content

Instantly share code, notes, and snippets.

@svileng
Created February 12, 2014 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svileng/8961859 to your computer and use it in GitHub Desktop.
Save svileng/8961859 to your computer and use it in GitHub Desktop.
Adds pinterest buttons on markup containing images
# Takes some markup (html or markdown) and wraps img elements into
# a div with a pinterest share link before image
# Params:
# markup - html or markdown that contains the images
# url - url for the shared item on pinterest
# desc - description for shared item on pinterest
# markdown - (optional) true if markup needs to be parsed as markdown (default), otherwise false
# wrap_class - (optional) css class for the img wrapper (default: pin-wrapper)
# pin_text - (optional) text for the pinterest link (default: Pin to Pinterest)
def pinterestify(markup, url, desc, options = {})
markdown = options[:markdown] || true
wrap_class = options[:wrap_class] || "pin-wrapper"
pin_text = options[:pin_text] || "Pin to Pinterest"
if markdown
markup = markdown(markup)
end
parsed_html = Nokogiri::HTML.fragment(markup)
parsed_html.css("img").wrap("<div class='" + wrap_class +"'></div>")
parsed_html.css("img").each do |img_el|
img_el.add_previous_sibling("<a target='_blank' class='auto-pin-link' href='https://pinterest.com/pin/create/button/?url=#{url}&description=#{desc}&media=#{img_el['src']}'>"+ pin_text +"</a>")
end
parsed_html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment