<commit_before>module Merb
  # The AssetsMixin module provides a number of helper methods to views for
  # linking to assets and other pages, dealing with JavaScript and CSS.
  module AssetsMixin
    include Merb::Assets::AssetHelpers
    # :section: Accessing Assets
    # Merb provides views with convenience methods for links images and other assets.
    
    # Creates a link for the URL given in +url+ with the text in +name+; HTML options are given in the +opts+
    # hash.
    #
    # ==== Options
    # The +opts+ hash is used to set HTML attributes on the tag.
    #    
    # ==== Examples
    #   link_to("The Merb home page", "http://www.merbivore.com/")
    #   # => <a href="http://www.merbivore.com/">The Merb home page</a>
    #
    #   link_to("The Ruby home page", "http://www.ruby-lang.org", {'class' => 'special', 'target' => 'blank'})
    #   # => <a href="http://www.ruby-lang.org" class="special" target="blank">The Ruby home page</a>
    #
    #   link_to p.title, "/blog/show/#{p.id}"
    #   # => <a href="blog/show/13">The Entry Title</a>
    #
    def link_to(name, url='', opts={})
      opts[:href] ||= url
      %{<a #{ opts.to_xml_attributes }>#{name}</a>}
    end
  
    # Creates an image tag with the +src+ attribute set to the +img+ argument.  The path
    # prefix defaults to <tt>/images/</tt>.  The path prefix can be overriden by setting a +:path+
    # parameter in the +opts+ hash.  The rest of the +opts+ hash sets HTML attributes.
    #
    # ==== Options
    # path:: Sets the path prefix for the image (defaults to +/images/+)
    # 
    # All other options in +opts+ set HTML attributes on the tag.
    #
    # ==== Examples
    #   image_tag('foo.gif') 
    #   # => <img src='/images/foo.gif' />
    #   
    #   image_tag('foo.gif', :class => 'bar') 
    #   # => <img src='/images/foo.gif' class='bar' />
    #
    #   image_tag('foo.gif', :path => '/files/') 
    #   # => <img src='/files/foo.gif' />
    #
    #   image_tag('http://test.com/foo.gif')
    #   # => <img src="http://test.com/foo.gif">
    def image_tag(img, opts={})
      opts[:path] ||= 
        if img =~ %r{^https?://}
          ''
        else
          if Merb::Config[:path_prefix]
            Merb::Config[:path_prefix] + '/images/'
          else
            '/images/'
          end
        end
      opts[:src] ||= opts.delete(:path) + img
      %{<img #{ opts.to_xml_attributes } />}    
    end

    # :section: JavaScript related functions
    #
    
    # Escapes text for use in JavaScript, replacing unsafe strings with their
    # escaped equivalent.
    #
    # ==== Examples
    #   escape_js("'Lorem ipsum!' -- Some guy")
    #   # => "\\'Lorem ipsum!\\' -- Some guy"
    #
    #   escape_js("Please keep text\nlines as skinny\nas possible.")
    #   # => "Please keep text\\nlines as skinny\\nas possible."
    def escape_js(javascript)
      (javascript || '').gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
    end
    
    # Creates a link tag with the text in +name+ and the <tt>onClick</tt> handler set to a JavaScript 
    # string in +function+.
    #
    # ==== Examples<commit_msg>Absolute path in image_tag now works like in RoR<commit_after>module Merb
  # The AssetsMixin module provides a number of helper methods to views for
  # linking to assets and other pages, dealing with JavaScript and CSS.
  module AssetsMixin
    include Merb::Assets::AssetHelpers
    # :section: Accessing Assets
    # Merb provides views with convenience methods for links images and other assets.
    
    # Creates a link for the URL given in +url+ with the text in +name+; HTML options are given in the +opts+
    # hash.
    #
    # ==== Options
    # The +opts+ hash is used to set HTML attributes on the tag.
    #    
    # ==== Examples
    #   link_to("The Merb home page", "http://www.merbivore.com/")
    #   # => <a href="http://www.merbivore.com/">The Merb home page</a>
    #
    #   link_to("The Ruby home page", "http://www.ruby-lang.org", {'class' => 'special', 'target' => 'blank'})
    #   # => <a href="http://www.ruby-lang.org" class="special" target="blank">The Ruby home page</a>
    #
    #   link_to p.title, "/blog/show/#{p.id}"
    #   # => <a href="blog/show/13">The Entry Title</a>
    #
    def link_to(name, url='', opts={})
      opts[:href] ||= url
      %{<a #{ opts.to_xml_attributes }>#{name}</a>}
    end
  
    # Creates an image tag with the +src+ attribute set to the +img+ argument.  The path
    # prefix defaults to <tt>/images/</tt>.  The path prefix can be overriden by setting a +:path+
    # parameter in the +opts+ hash.  The rest of the +opts+ hash sets HTML attributes.
    #
    # ==== Options
    # path:: Sets the path prefix for the image (defaults to +/images/+)
    # 
    # All other options in +opts+ set HTML attributes on the tag.
    #
    # ==== Examples
    #   image_tag('foo.gif') 
    #   # => <img src='/images/foo.gif' />
    #   
    #   image_tag('foo.gif', :class => 'bar') 
    #   # => <img src='/images/foo.gif' class='bar' />
    #
    #   image_tag('foo.gif', :path => '/files/') 
    #   # => <img src='/files/foo.gif' />
    #
    #   image_tag('http://test.com/foo.gif')
    #   # => <img src="http://test.com/foo.gif">
    #
    #   image_tag('charts', :path => '/dynamic/')
    #   or 
    #   image_tag('/dynamic/charts')
    #   # => <img src="/dynamic/charts">
    #
    def image_tag(img, opts={})
      if img[0].chr == '/'
        opts[:src] = img
      else
        opts[:path] ||= 
          if img =~ %r{^https?://}
            ''
          else
            if Merb::Config[:path_prefix]
              Merb::Config[:path_prefix] + '/images/'
            else
              '/images/'
            end
          end
        opts[:src] ||= opts.delete(:path) + img
      end
      %{<img #{ opts.to_xml_attributes } />}    
    end

    # :section: JavaScript related functions
    #
    
    # Escapes text for use in JavaScript, replacing unsafe strings with their
    # escaped equivalent.
    #
    # ==== Examples
    #   escape_js("'Lorem ipsum!' -- Some guy")
    #   # => "\\'Lorem ipsum!\\' -- Some guy"
    #
    #   escape_js("Please keep text\nlines as skinny\nas possible.")
    #   # => "Please keep text\\nlines as skinny\\nas possible."
    def escape_js(javascript)
      (javascript || '').gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
    end
    
    # Creates a link tag with the text in +name+ and the <tt>onClick</tt> handler set to a JavaScript 
    # string in +function+.
    #
    # ==== Examples
{'fraction_non_alphanumeric': 0.1703030303030303, 'fraction_numerical': 0.0013636363636363637, 'mean_word_length': 2.2090422946037918, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 4}, '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': '4813040', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2172, 'n_words': 612}