<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.


    # ==== Parameters
    # none
    #
    # ==== Returns
    # html<String>
    #    
    # ==== Examples
    #  We want all possible matches in the FileSys up to the action name
    #     Given:  controller_name = "namespace/controller"
    #             action_name     = "action"
    #  If all files are present should generate link/script tags for:
    #     namespace.(css|js)
    #     namespace/controller.(css|js)
    #     namespace/controller/action.(css|js)
    #
    def auto_link
      html    = "" 
      prefix  = ""
      (controller_name / action_name).split("/").each do |path|
        path = prefix + path

        css_path  = path + ".css"
        if File.exists? Merb.root / "public" / "stylesheets" / css_path
          html << %{<link rel="stylesheet" type="text/css" href="/stylesheets/#{css_path}" /> }
        end

        js_path   = path + ".js"
        if File.exists? Merb.root / "public" / "javascripts" / js_path
          html << %{<script type="text/javascript" language="javascript" src="/javascripts/#{js_path}"></script>}
        end

        #Update the prefix for the next iteration
        prefix += path / ""
      end

      #Return the generated HTML
      html
    end


    # ==== Parameters
    # name<~to_s>:: The text of the link.
    # url<~to_s>:: The URL to link to. Defaults to an empty string.
    # opts<Hash>:: Additional HTML options for the link.
    #    
    # ==== 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
  
    # ==== Parameters
    # img<~to_s>:: The image path.
    # opts<Hash>:: Additional options for the image tag (see below).
    #
    # ==== Options (opts)
    # :path<String>::
    #   Sets the path prefix for the image. Defaults to "/images/" or whatever
    #   is specified in Merb::Config. This is ignored if img is an absolute
    #   path or full URL.
    # 
    # All other options 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
    #
    
    # ==== Parameters
    # javascript<String>:: Text to escape for use in JavaScript.
    #
    # ==== 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
    
    # ==== Parameters
    # name<~to_s>:: The text in the link.
    # function<~to_s>:: The onClick event in JavaScript.
    #
    # ==== Examples
    #   link_to_function('Click me', "alert('hi!')")
    #     # => <a href="#" onclick="alert('hi!'); return false;">Click me</a>
    #
    #   link_to_function('Add to cart', "item_total += 1; alert('Item added!');")
    #     # => <a href="#" onclick="item_total += 1; alert('Item added!'); return false;">Add to cart</a>
    def link_to_function(name, function, opts)
      %{<a href="##{opts.delete(:anchor)}" onclick="#{function.chomp(";")}; return false;" #{ opts.to_xml_attributes }>#{name}</a>}
    end
    
    # ==== Parameters
    # data<Object>::
    #   Object to translate into JSON. If the object does not respond to
    #   :to_json, then data.inspect.to_json is returned instead.
    #
    # ==== Examples
    #   js({'user' => 'Lewis', 'page' => 'home'})
    #    # => "{\"user\":\"Lewis\",\"page\":\"home\"}"
    #
    #   js([ 1, 2, {"a"=>3.141}, false, true, nil, 4..10 ])
    #     # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
    def js(data)
      if data.respond_to? :to_json
        data.to_json
      else
        data.inspect.to_json
      end
    end<commit_msg>Made the options hash optional.<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.


    # ==== Parameters
    # none
    #
    # ==== Returns
    # html<String>
    #    
    # ==== Examples
    #  We want all possible matches in the FileSys up to the action name
    #     Given:  controller_name = "namespace/controller"
    #             action_name     = "action"
    #  If all files are present should generate link/script tags for:
    #     namespace.(css|js)
    #     namespace/controller.(css|js)
    #     namespace/controller/action.(css|js)
    #
    def auto_link
      html    = "" 
      prefix  = ""
      (controller_name / action_name).split("/").each do |path|
        path = prefix + path

        css_path  = path + ".css"
        if File.exists? Merb.root / "public" / "stylesheets" / css_path
          html << %{<link rel="stylesheet" type="text/css" href="/stylesheets/#{css_path}" /> }
        end

        js_path   = path + ".js"
        if File.exists? Merb.root / "public" / "javascripts" / js_path
          html << %{<script type="text/javascript" language="javascript" src="/javascripts/#{js_path}"></script>}
        end

        #Update the prefix for the next iteration
        prefix += path / ""
      end

      #Return the generated HTML
      html
    end


    # ==== Parameters
    # name<~to_s>:: The text of the link.
    # url<~to_s>:: The URL to link to. Defaults to an empty string.
    # opts<Hash>:: Additional HTML options for the link.
    #    
    # ==== 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
  
    # ==== Parameters
    # img<~to_s>:: The image path.
    # opts<Hash>:: Additional options for the image tag (see below).
    #
    # ==== Options (opts)
    # :path<String>::
    #   Sets the path prefix for the image. Defaults to "/images/" or whatever
    #   is specified in Merb::Config. This is ignored if img is an absolute
    #   path or full URL.
    # 
    # All other options 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
    #
    
    # ==== Parameters
    # javascript<String>:: Text to escape for use in JavaScript.
    #
    # ==== 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
    
    # ==== Parameters
    # name<~to_s>:: The text in the link.
    # function<~to_s>:: The onClick event in JavaScript.
    #
    # ==== Examples
    #   link_to_function('Click me', "alert('hi!')")
    #     # => <a href="#" onclick="alert('hi!'); return false;">Click me</a>
    #
    #   link_to_function('Add to cart', "item_total += 1; alert('Item added!');")
    #     # => <a href="#" onclick="item_total += 1; alert('Item added!'); return false;">Add to cart</a>
    def link_to_function(name, function, opts = {})
      %{<a href="##{opts.delete('anchor')}" onclick="#{function.chomp(";")}; return false;" #{ opts.to_xml_attributes }>#{name}</a>}
    end
    
    # ==== Parameters
    # data<Object>::
    #   Object to translate into JSON. If the object does not respond to
    #   :to_json, then data.inspect.to_json is returned instead.
    #
    # ==== Examples
    #   js({'user' => 'Lewis', 'page' => 'home'})
    #    # => "{\"user\":\"Lewis\",\"page\":\"home\"}"
    #
    #   js([ 1, 2, {"a"=>3.141}, false, true, nil, 4..10 ])
    #     # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
    def js(data)
      if data.respond_to? :to_json
        data.to_json
      else
        data.inspect.to_json
      end
    end
{'fraction_non_alphanumeric': 0.18050892383813394, 'fraction_numerical': 0.004417741650468281, 'mean_word_length': 2.1476640711902113, 'pattern_counts': {'":': 6, '<': 79, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 6}, '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': '2639816', 'n_tokens_mistral': 3809, 'n_tokens_neox': 3713, 'n_words': 907}