<commit_before># For instrumenting
require 'active_support/notifications'

# Core Pathname library used for traversal
require 'pathname'

# Template and Mime detection
require 'tilt'
require 'rack/mime'

# DbC
require 'middleman-core/contracts'

# Immutable Data
require 'hamster'

# For URI templating
require 'addressable/uri'
require 'addressable/template'
require 'active_support/inflector'
require 'active_support/inflector/transliterate'

module Middleman
  module Util
    include Contracts

    module_function

    # Whether the source file is binary.
    #
    # @param [String] filename The file to check.
    # @return [Boolean]
    Contract Or[String, Pathname] => Bool
    def binary?(filename)
      path = Pathname(filename)
      ext = path.extname

      # We hardcode detecting of gzipped SVG files
      return true if ext == '.svgz'

      return false if Tilt.registered?(ext.sub('.', ''))

      dot_ext = (ext.to_s[0] == '.') ? ext.dup : ".#{ext}"

      if mime = ::Rack::Mime.mime_type(dot_ext, nil)
        !nonbinary_mime?(mime)
      else
        file_contents_include_binary_bytes?(path.to_s)
      end
    end

    # Takes a matcher, which can be a literal string
    # or a string containing glob expressions, or a
    # regexp, or a proc, or anything else that responds
    # to #match or #call, and returns whether or not the
    # given path matches that matcher.
    #
    # @param [String, #match, #call] matcher A matcher String, RegExp, Proc, etc.
    # @param [String] path A path as a string
    # @return [Boolean] Whether the path matches the matcher
    Contract PATH_MATCHER, String => Bool
    def path_match(matcher, path)
      case
      when matcher.is_a?(String)
        if matcher.include? '*'
          File.fnmatch(matcher, path)
        else
          path == matcher
        end
      when matcher.respond_to?(:match)
        !matcher.match(path).nil?
      when matcher.respond_to?(:call)
        matcher.call(path)
      else
        File.fnmatch(matcher.to_s, path)
      end
    end

    class IndifferentHash < ::Hamster::Hash
      def get(key)
        key?(key.to_s) ? super(key.to_s) : super(key.to_sym)
      end

      alias_method :method_missing, :get
    end

    # Recursively convert a normal Hash into a IndifferentHash
    #
    # @private
    # @param [Hash] data Normal hash
    # @return [Middleman::Util::IndifferentHash]
    FrozenDataStructure = Frozen[Or[IndifferentHash, Array, String, TrueClass, FalseClass, Fixnum]]
    Contract Maybe[Or[String, Array, Hash, IndifferentHash]] => Maybe[FrozenDataStructure]
    def recursively_enhance(obj)
      case obj
      when ::Hash
        res = obj.map { |key, value| [recursively_enhance(key), recursively_enhance(value)] }
        IndifferentHash.new(res)
      when IndifferentHash
        obj.map { |key, value| [recursively_enhance(key), recursively_enhance(value)] }
      when ::Array
        res = obj.map { |element| recursively_enhance(element) }
        Hamster::Vector.new(res)
      when ::SortedSet
        # This clause must go before ::Set clause, since ::SortedSet is a ::Set.
        res = obj.map { |element| recursively_enhance(element) }
        Hamster::SortedSet.new(res)
      when ::Set<commit_msg>Better indiff logic<commit_after># For instrumenting
require 'active_support/notifications'

# Core Pathname library used for traversal
require 'pathname'

# Template and Mime detection
require 'tilt'
require 'rack/mime'

# DbC
require 'middleman-core/contracts'

# Immutable Data
require 'hamster'

# For URI templating
require 'addressable/uri'
require 'addressable/template'
require 'active_support/inflector'
require 'active_support/inflector/transliterate'

module Middleman
  module Util
    include Contracts

    module_function

    # Whether the source file is binary.
    #
    # @param [String] filename The file to check.
    # @return [Boolean]
    Contract Or[String, Pathname] => Bool
    def binary?(filename)
      path = Pathname(filename)
      ext = path.extname

      # We hardcode detecting of gzipped SVG files
      return true if ext == '.svgz'

      return false if Tilt.registered?(ext.sub('.', ''))

      dot_ext = (ext.to_s[0] == '.') ? ext.dup : ".#{ext}"

      if mime = ::Rack::Mime.mime_type(dot_ext, nil)
        !nonbinary_mime?(mime)
      else
        file_contents_include_binary_bytes?(path.to_s)
      end
    end

    # Takes a matcher, which can be a literal string
    # or a string containing glob expressions, or a
    # regexp, or a proc, or anything else that responds
    # to #match or #call, and returns whether or not the
    # given path matches that matcher.
    #
    # @param [String, #match, #call] matcher A matcher String, RegExp, Proc, etc.
    # @param [String] path A path as a string
    # @return [Boolean] Whether the path matches the matcher
    Contract PATH_MATCHER, String => Bool
    def path_match(matcher, path)
      case
      when matcher.is_a?(String)
        if matcher.include? '*'
          File.fnmatch(matcher, path)
        else
          path == matcher
        end
      when matcher.respond_to?(:match)
        !matcher.match(path).nil?
      when matcher.respond_to?(:call)
        matcher.call(path)
      else
        File.fnmatch(matcher.to_s, path)
      end
    end

    class IndifferentHash < ::Hamster::Hash
      def [](key)
        if key?(key.to_sym)
          super(key.to_sym)
        elsif key?(key.to_s)
          super(key.to_s)
        else
          super
        end
      end

      def method_missing(key, *args)
        if key?(key.to_sym)
          self[key.to_sym]
        elsif key?(key.to_s)
          self[key.to_s]
        else
          super
        end
      end
    end

    # Recursively convert a normal Hash into a IndifferentHash
    #
    # @private
    # @param [Hash] data Normal hash
    # @return [Middleman::Util::IndifferentHash]
    FrozenDataStructure = Frozen[Or[IndifferentHash, Array, String, TrueClass, FalseClass, Fixnum]]
    Contract Maybe[Or[String, Array, Hash, IndifferentHash]] => Maybe[FrozenDataStructure]
    def recursively_enhance(obj)
      case obj
      when ::Hash
        res = obj.map { |key, value| [recursively_enhance(key), recursively_enhance(value)] }
        IndifferentHash.new(res)
      when IndifferentHash
        obj.map { |key, value| [recursively_enhance(key), recursively_enhance(value)] }
      when ::Array
        res = obj.map { |element| recursively_enhance(element) }
        Hamster::Vector.new(res)
      when ::SortedSet
        # This clause must go before ::Set clause, since ::SortedSet is a ::Set.
        res = obj.map { |element| recursively_enhance(element) }
        Hamster::SortedSet.new(res)
      when ::Set
{'fraction_non_alphanumeric': 0.10121517486662715, 'fraction_numerical': 0.0002963841138114997, 'mean_word_length': 2.7494444444444444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5702289', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2097, 'n_words': 591}