<commit_before>#
# Jekyll Asset Bundler
#
# Author : <NAME>
# Repo   : http://github.com/moshen/jekyll-asset_bundler
# License: MIT, see LICENSE file
#

require 'yaml'
require 'digest/md5'
require 'net/http'
require 'uri'

module Jekyll

  class BundleTag < Liquid::Block
    @@supported_types = ['js', 'css']

    def initialize(tag_name, text, tokens)
      super
      @text = text 
      @files = {}
    end

    def render(context)
      src = context.registers[:site].source
      raw_markup = super(context)
      begin
        @assets = YAML::load(raw_markup)
      rescue
        puts <<-END
Asset Bundler - Error: Problem parsing a YAML bundle
#{raw_markup}

#{$!}
END
      end

      if @assets.class != Array
        puts "Asset Bundler - Error: YAML bundle is not an Array\n#{raw_markup}"
        @assets = []
      end

      add_files_from_list(src, @assets)

      markup = ""

      @files.each {|k,v|
        markup.concat(Bundle.new(v, k, context).markup())
      }

      markup
    end

    def add_files_from_list(src, list)
      list.each {|a|
        path = File.join(src, a)
        if (File.basename(a) !~ /^\.+/ and File.file?(path)) or a =~ /^(https?:)?\/\//i
          add_file_by_type(a)
        else
          puts "Asset Bundler Error - File: #{path} not found, ignoring..."
        end
      }
    end

    def add_file_by_type(file)
      if file =~ /\.([^\.]+)$/
        type = $1.downcase()
        return if @@supported_types.index(type).nil?
        if !@files.key?(type)
          @files[type] = []
        end

        @files[type].push(file)
      end
    end

  end

  class BundleGlobTag < BundleTag
    def add_files_from_list(src, list)
      list.each {|a|
        Dir.glob(File.join(src, a)) {|f|
          if f !~ /^\.+/ and File.file?(f)
            add_file_by_type(f.sub(src,''))
          end
        }
      }
    end

  end

  class DevAssetsTag < BundleTag
    def render(context)
      if Bundle.config(context)['dev']
        super(context)
      else
        ''
      end
    end

    def add_files_from_list(src, list)
      list.each {|a|
        add_file_by_type(a)
      }
    end
  end

  class Bundle
    @@bundles = {}
    @@default_config = {
      'compile'        => { 'coffee' => false, 'less' => false },
      'compress'       => { 'js'     => false, 'css'  => false },
      'base_path'      => '/bundles/',
      'cdn'            => '',
      'remove_bundled' => false,
      'dev'            => false,
      'markup_templates' => {
        'js'     =>
          Liquid::Template.parse("<script type='text/javascript' src='{{url}}'></script>\n"),
        'coffee' =>
          Liquid::Template.parse("<script type='text/coffeescript' src='{{url}}'></script>\n"),
        'css'    =>
          Liquid::Template.parse("<link rel='stylesheet' type='text/css' href='{{url}}' />\n"),
        'less'   =>
          Liquid::Template.parse("<link rel='stylesheet/less' type='text/css' href='{{url}}' />\n")
      }
    }
    attr_reader :content, :hash, :filename, :base

    def initialize(files, type, context)
      @files    = files
      @type     = type
      @context  = context
      @content  = ''
      @hash     = ''
      @filename = ''

      @config = Bundle.config(@context)
      @base = @config['base_path']

      @filename_hash = Digest::MD5.hexdigest(@files.join())
      if @@bundles.key?(@filename_hash)
        @filename = @@bundles[@filename_hash].filename
        @base     = @@bundles[@filename_hash].base
      else
        load_content()
      end
    end

    def self.config(context)
      ret_config = nil
      if context.registers[:site].config.key?("asset_bundler")
        ret_config = @@default_config.deep_merge(context.registers[:site].config["asset_bundler"])
        ret_config['markup_templates'].keys.each {|k|
          if ret_config['markup_templates'][k].class != Liquid::Template
            if ret_config['markup_templates'][k].class == String
              ret_config['markup_templates'][k] =
                Liquid::Template.parse(ret_config['markup_templates'][k]);
            else
              puts <<-END
Asset Bundler - Error: Problem parsing _config.yml

The value for configuration option:
  asset_bundler => markup_templates => #{k}

Is not recognized as a String for use as a valid template.
Reverting to the default template.
END
              ret_config['markup_templates'][k] = @@default_config['markup_templates'][k];
            end
          end
        }
      else
        ret_config = @@default_config
      end

      # Check to make sure the base_path begins with a slash
      #   This is to make sure that the path works with a potential base CDN url
      if ret_config['base_path'] !~ /^\//
        ret_config['base_path'].insert(0,'/')
      end

      if context.registers[:site].config.key?("dev")
        ret_config['dev'] = context.registers[:site].config["dev"] ? true : false
      end

      if context.registers[:site].config['server']
        ret_config['dev'] = true
      end

      ret_config
    end<commit_msg>Modify Bundle.config to load configuration only once<commit_after>#
# Jekyll Asset Bundler
#
# Author : <NAME>
# Repo   : http://github.com/moshen/jekyll-asset_bundler
# License: MIT, see LICENSE file
#

require 'yaml'
require 'digest/md5'
require 'net/http'
require 'uri'

module Jekyll

  class BundleTag < Liquid::Block
    @@supported_types = ['js', 'css']

    def initialize(tag_name, text, tokens)
      super
      @text = text 
      @files = {}
    end

    def render(context)
      src = context.registers[:site].source
      raw_markup = super(context)
      begin
        @assets = YAML::load(raw_markup)
      rescue
        puts <<-END
Asset Bundler - Error: Problem parsing a YAML bundle
#{raw_markup}

#{$!}
END
      end

      if @assets.class != Array
        puts "Asset Bundler - Error: YAML bundle is not an Array\n#{raw_markup}"
        @assets = []
      end

      add_files_from_list(src, @assets)

      markup = ""

      @files.each {|k,v|
        markup.concat(Bundle.new(v, k, context).markup())
      }

      markup
    end

    def add_files_from_list(src, list)
      list.each {|a|
        path = File.join(src, a)
        if (File.basename(a) !~ /^\.+/ and File.file?(path)) or a =~ /^(https?:)?\/\//i
          add_file_by_type(a)
        else
          puts "Asset Bundler Error - File: #{path} not found, ignoring..."
        end
      }
    end

    def add_file_by_type(file)
      if file =~ /\.([^\.]+)$/
        type = $1.downcase()
        return if @@supported_types.index(type).nil?
        if !@files.key?(type)
          @files[type] = []
        end

        @files[type].push(file)
      end
    end

  end

  class BundleGlobTag < BundleTag
    def add_files_from_list(src, list)
      list.each {|a|
        Dir.glob(File.join(src, a)) {|f|
          if f !~ /^\.+/ and File.file?(f)
            add_file_by_type(f.sub(src,''))
          end
        }
      }
    end

  end

  class DevAssetsTag < BundleTag
    def render(context)
      if Bundle.config(context)['dev']
        super(context)
      else
        ''
      end
    end

    def add_files_from_list(src, list)
      list.each {|a|
        add_file_by_type(a)
      }
    end
  end

  class Bundle
    @@bundles = {}
    @@default_config = {
      'compile'        => { 'coffee' => false, 'less' => false },
      'compress'       => { 'js'     => false, 'css'  => false },
      'base_path'      => '/bundles/',
      'cdn'            => '',
      'remove_bundled' => false,
      'dev'            => false,
      'markup_templates' => {
        'js'     =>
          Liquid::Template.parse("<script type='text/javascript' src='{{url}}'></script>\n"),
        'coffee' =>
          Liquid::Template.parse("<script type='text/coffeescript' src='{{url}}'></script>\n"),
        'css'    =>
          Liquid::Template.parse("<link rel='stylesheet' type='text/css' href='{{url}}' />\n"),
        'less'   =>
          Liquid::Template.parse("<link rel='stylesheet/less' type='text/css' href='{{url}}' />\n")
      }
    }
    @@current_config = nil
    attr_reader :content, :hash, :filename, :base

    def initialize(files, type, context)
      @files    = files
      @type     = type
      @context  = context
      @content  = ''
      @hash     = ''
      @filename = ''

      @config = Bundle.config(@context)
      @base = @config['base_path']

      @filename_hash = Digest::MD5.hexdigest(@files.join())
      if @@bundles.key?(@filename_hash)
        @filename = @@bundles[@filename_hash].filename
        @base     = @@bundles[@filename_hash].base
      else
        load_content()
      end
    end

    def self.config(context)
      if @@current_config.nil?
        ret_config = nil
        if context.registers[:site].config.key?("asset_bundler")
          ret_config = @@default_config.deep_merge(context.registers[:site].config["asset_bundler"])
          ret_config['markup_templates'].keys.each {|k|
            if ret_config['markup_templates'][k].class != Liquid::Template
              if ret_config['markup_templates'][k].class == String
                ret_config['markup_templates'][k] =
                  Liquid::Template.parse(ret_config['markup_templates'][k]);
              else
                puts <<-END
Asset Bundler - Error: Problem parsing _config.yml

The value for configuration option:
  asset_bundler => markup_templates => #{k}

Is not recognized as a String for use as a valid template.
Reverting to the default template.
END
                ret_config['markup_templates'][k] = @@default_config['markup_templates'][k];
              end
            end
          }
        else
          ret_config = @@default_config
        end

        # Check to make sure the base_path begins with a slash
        #   This is to make sure that the path works with a potential base CDN url
        if ret_config['base_path'] !~ /^\//
          ret_config['base_path'].insert(0,'/')
        end

        if context.registers[:site].config.key?("dev")
          ret_config['dev'] = context.registers[:site].config["dev"] ? true : false
        end

        if context.registers[:site].config['server']
          ret_config['dev'] = true
        end

        @@current_config = ret_config
      end

      @@current_config
    end
{'fraction_non_alphanumeric': 0.14516441005802708, 'fraction_numerical': 0.0007736943907156673, 'mean_word_length': 2.108205590622182, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 51, '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': '1150870', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3273, 'n_words': 710}