<commit_before>module Yoolk
  module Liquid
    class AddThisTag < ::Liquid::Block
      attr_accessor :networks

      def initialize(tag_name, markup, options)
        super

        @tool, @style = markup.split(',').inject([]) do |props, item|
          props.push($1.delete("'")) if item =~ /('.*')/
        end

        @networks = []
        @style_size = case @style
        when 'large'
          '32x32'
        when 'medium'
          '20x20'
        when 'small'
          '16x16'
        else
          raise ArgumentError.new("Style must be one of large, medium, small")
        end

        unless @tool == 'follow'
          raise SyntaxError.new("Syntax Error - Valid syntax: {% addthis tool: 'follow' %}{% endaddthis %}")
        end

      end

      def unknown_tag(name, params, tokens)
        if name.in?(['facebook', 'twitter'])
          handle_link_tag(name, params)
        else
          super
        end
      end

      def handle_link_tag(name, params)
        args = split_params(params)
        url, size = args.inject([]) do |props, item|
          props.push($1.delete("'")) if item =~ /('.*')/
        end
        @networks << { name: name, url: url, size: size }
      end

      def split_params(params)
        params.split(",").map(&:strip)
      end

      def render(context)
        @context = context

        <<-EOF.gsub(/^\s+|$\n/, "")
          <div class="addthis_toolbox addthis_default_style addthis_#{ @style_size }_style">
          #{ link_builder }
          </div>
        EOF

        # binding.pry
        # super.concat("<input>&&&</input>")
        # '<input>&&&</input>'
      end

      # check image, addthis size style
      def link_builder
        @networks.inject("") do |links, li|
          case li[:name]
          when 'facebook'
            if li[:url]
              link = h.content_tag :a, h.image_tag(li[:url]), {"addthis:userid" => facebook_page_name(@context["listing.facebook_page.page_name"]), :class=> "addthis_button_facebook_follow"}
              links.concat link
            else
              links.concat(h.content_tag :a, "", {"addthis:userid" => facebook_page_name(@context["listing.facebook_page.page_name"]), :class=> "addthis_button_facebook_follow"})
            end
          when 'twitter'
            if li[:url]
              link = h.content_tag :a, h.image_tag(li[:url]), { "addthis:userid" => twitter_page_name(@context["listing.twitter_account.profile_url"]), :class=> "addthis_button_twitter_follow" }
              links.concat link
            else
              links.concat(h.content_tag :a, "", {"addthis:userid" => twitter_page_name(@context["listing.twitter_account.profile_url"]), :class=> "addthis_button_twitter_follow"})<commit_msg>addthis dynamic image size<commit_after>module Yoolk
  module Liquid
    class AddThisTag < ::Liquid::Block
      attr_accessor :networks

      def initialize(tag_name, markup, options)
        super

        @tool, @style = markup.split(',').inject([]) do |props, item|
          props.push($1.delete("'")) if item =~ /('.*')/
        end

        @networks = []
        @style_size = case @style
        when 'large'
          '32x32'
        when 'medium'
          '20x20'
        when 'small'
          '16x16'
        else
          raise ArgumentError.new("Style must be one of large, medium, small")
        end

        unless @tool == 'follow'
          raise SyntaxError.new("Syntax Error - Valid syntax: {% addthis tool: 'follow' %}{% endaddthis %}")
        end

      end

      def unknown_tag(name, params, tokens)
        if name.in?(['facebook', 'twitter'])
          handle_link_tag(name, params)
        else
          super
        end
      end

      def handle_link_tag(name, params)
        args = split_params(params)
        url, size = args.inject([]) do |props, item|
          props.push($1.delete("'")) if item =~ /('.*')/
        end
        @networks << { name: name, url: url, size: size }
      end

      def split_params(params)
        params.split(",").map(&:strip)
      end

      def render(context)
        @context = context

        <<-EOF.gsub(/^\s+|$\n/, "")
          <div class="addthis_toolbox addthis_default_style addthis_#{ @style_size }_style">
          #{ link_builder }
          </div>
        EOF

        # binding.pry
        # super.concat("<input>&&&</input>")
        # '<input>&&&</input>'
      end

      # check image, addthis size style
      def link_builder
        @networks.inject("") do |links, li|
          image_tag = li[:size].present? ? h.image_tag(li[:url], size: li[:size]) : h.image_tag(li[:url])
          case li[:name]
          when 'facebook'
            if li[:url]
              link = h.content_tag :a, image_tag, {"addthis:userid" => facebook_page_name(@context["listing.facebook_page.page_name"]), :class=> "addthis_button_facebook_follow"}
              links.concat link
            else
              links.concat(h.content_tag :a, "", {"addthis:userid" => facebook_page_name(@context["listing.facebook_page.page_name"]), :class=> "addthis_button_facebook_follow"})
            end
          when 'twitter'
            if li[:url]
              link = h.content_tag :a, image_tag, { "addthis:userid" => twitter_page_name(@context["listing.twitter_account.profile_url"]), :class=> "addthis_button_twitter_follow" }
              links.concat link
            else
              links.concat(h.content_tag :a, "", {"addthis:userid" => twitter_page_name(@context["listing.twitter_account.profile_url"]), :class=> "addthis_button_twitter_follow"})
{'fraction_non_alphanumeric': 0.14347125201504568, 'fraction_numerical': 0.005015224789539674, 'mean_word_length': 2.1763367463026166, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348871', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1672, 'n_words': 360}