module Jaap3 module Addthis CONFIG = {:publisher => ""} DEFAULT_OPTIONS = { :script_src => "http://s7.addthis.com/js/200/addthis_widget.js", :brand => nil, :header_color => nil, :header_background => nil, :offset_top => nil, :offset_left => nil, :hover_delay => nil, :options => nil, :language => nil, :secure => false } BOOKMARK_BUTTON_DEFAULTS = { :title => "", :button_html => 'Bookmark and Share' } FEED_BUTTON_DEFAULTS = { :title => "Subscribe using any feed reader!", :button_html => 'Subscribe' } EMAIL_BUTTON_DEFAULTS = { :title => "", :button_html => 'Email' } module Helper def addthis_bookmark_button(*args) url, options = extract_addthis_url_and_options(args) options[:button_html] = yield if block_given? options = BOOKMARK_BUTTON_DEFAULTS.merge(options) s = %Q{} addthis_tag(s, options) end alias addthis_share_button addthis_bookmark_button def addthis_email_button(*args) url, options = extract_addthis_url_and_options(args) options[:button_html] = yield if block_given? options = EMAIL_BUTTON_DEFAULTS.merge(options) s = %Q{} addthis_tag(s, options) end def addthis_feed_button(url, *args) options = FEED_BUTTON_DEFAULTS.merge(extract_addthis_options(args)) options[:button_html] = yield if block_given? s = %Q{} addthis_tag(s, options) end protected def addthis_tag(str, options = {}) s = [""] s << addthis_custom_script(options) s << "#{str}#{options[:button_html]}" s << "" s = s * "\n" options[:secure] ? s.gsub(/http:\/\/s[57]\.addthis\.com/, "https://secure.addthis.com") : s end def addthis_custom_script(options = {}) s = "" end def addthis_open(type, url, title = nil) "addthis_open(this, '#{type}', '#{url}'#{ title.nil? ? "" : ", '#{title.gsub("'", "\\u0027").gsub("\"", "\\u0022")}'"})" end def extract_addthis_url_and_options(args, options = {:page_title => "[TITLE]"}) url = args[0].is_a?(String) ? args.shift : "[URL]" return url, options = extract_addthis_options(args, options) end def extract_addthis_options(args, options = {}) page_title = args[0].is_a?(String) ? args.shift : options[:page_title] options = args[0].is_a?(Hash) ? args.shift : options options.symbolize_keys! if options.respond_to?(:symbolize_keys!) options[:page_title] = page_title options = CONFIG.merge(DEFAULT_OPTIONS).merge(options) return options end end end end