<commit_before>require 'polyglot'

require 'deface/dsl/context'

module Deface
  module DSL
    class Loader
      def self.load(filename, options = nil, &block)
        unless File.basename(filename) =~ /^[^\.]+(.html.(erb|haml|slim)){0,1}.deface$/
          raise "Deface::DSL does not know how to read '#{filename}'. Override files should end with just .deface, .html.erb.deface, .html.haml.deface or .html.slim.deface"
        end

        unless file_in_dir_below_overrides?(filename)
          raise "Deface::DSL overrides must be in a sub-directory that matches the views virtual path. Move '#{filename}' into a sub-directory."
        end

        File.open(filename) do |file|
          context_name = File.basename(filename).gsub('.deface', '')

          file_contents = file.read

          if context_name.end_with?('.html.erb')
            dsl_commands, the_rest = extract_dsl_commands_from_erb(file_contents)

            context_name = context_name.gsub('.html.erb', '')
            context = Context.new(context_name)
            context.virtual_path(determine_virtual_path(filename))
            context.instance_eval(dsl_commands)
            context.erb(the_rest)
            context.create_override
          elsif context_name.end_with?('.html.haml')
            dsl_commands, the_rest = extract_dsl_commands_from_haml(file_contents)

            context_name = context_name.gsub('.html.haml', '')
            context = Context.new(context_name)
            context.virtual_path(determine_virtual_path(filename))
            context.instance_eval(dsl_commands)
            context.haml(the_rest)
            context.create_override
          elsif context_name.end_with?('.html.slim')
            dsl_commands, the_rest = extract_dsl_commands_from_slim(file_contents)

            context_name = context_name.gsub('.html.slim', '')
            context = Context.new(context_name)
            context.virtual_path(determine_virtual_path(filename))
            context.instance_eval(dsl_commands)
            context.slim(the_rest)
            context.create_override
          else
            context = Context.new(context_name)
            context.virtual_path(determine_virtual_path(filename))
            context.instance_eval(file_contents)
            context.create_override
          end
        end
      end

      def self.register
        Polyglot.register('deface', Deface::DSL::Loader)
      end

      def self.extract_dsl_commands_from_erb(html_file_contents)
        dsl_commands = ''

        while starts_with_html_comment?(html_file_contents)
          first_open_comment_index = html_file_contents.lstrip.index('<!--')
          first_close_comment_index = html_file_contents.index('-->')

          unless first_close_comment_index.nil?
            comment = html_file_contents[first_open_comment_index..first_close_comment_index+2]
          end

          comment.gsub('<!--', '').gsub('-->', '').strip.scan(/[^\s"']+|"[^"]*"|'[^']*'/).each do |part|

            dsl_commands =~ /('|")\z/ || part =~ /\A[^\d:='"%]/ ? dsl_commands << "\n" : dsl_commands << ' '
            dsl_commands << part
          end

          html_file_contents = html_file_contents.gsub(comment, '')
        end

        [dsl_commands, html_file_contents]
      end

      def self.extract_dsl_commands_from_haml(file_contents)
        dsl_commands = ''<commit_msg>Split load into multiple smaller methods<commit_after>require 'polyglot'

require 'deface/dsl/context'

module Deface
  module DSL
    class Loader
      def self.load(filename, options = nil, &block)
        unless File.basename(filename) =~ /^[^\.]+(.html.(erb|haml|slim)){0,1}.deface$/
          raise "Deface::DSL does not know how to read '#{filename}'. Override files should end with just .deface, .html.erb.deface, .html.haml.deface or .html.slim.deface"
        end

        unless file_in_dir_below_overrides?(filename)
          raise "Deface::DSL overrides must be in a sub-directory that matches the views virtual path. Move '#{filename}' into a sub-directory."
        end

        File.open(filename) do |file|
          context_name = File.basename(filename).gsub('.deface', '')

          file_contents = file.read

          build_context(context_name, filename, file_contents)
        end
      end

      def self.build_context(context_name, filename, file_contents)
        send build_context_method_name(context_name), context_name, filename, file_contents
      end

      def self.build_context_method_name(context_name)
        ext = File.extname(context_name).gsub(".", '')
        ext = "other" if ext.empty?
        "build_#{ext}_context"
      end

      def self.build_erb_context(context_name, filename, file_contents)
        dsl_commands, the_rest = extract_dsl_commands_from_erb(file_contents)

        context_name = context_name.gsub('.html.erb', '')
        context = Context.new(context_name)
        context.virtual_path(determine_virtual_path(filename))
        context.instance_eval(dsl_commands)
        context.erb(the_rest)
        context.create_override
      end

      def self.build_haml_context(context_name, filename, file_contents)
        dsl_commands, the_rest = extract_dsl_commands_from_haml(file_contents)

        context_name = context_name.gsub('.html.haml', '')
        context = Context.new(context_name)
        context.virtual_path(determine_virtual_path(filename))
        context.instance_eval(dsl_commands)
        context.haml(the_rest)
        context.create_override
      end

      def self.build_slim_context(context_name, filename, file_contents)
        dsl_commands, the_rest = extract_dsl_commands_from_slim(file_contents)

        context_name = context_name.gsub('.html.slim', '')
        context = Context.new(context_name)
        context.virtual_path(determine_virtual_path(filename))
        context.instance_eval(dsl_commands)
        context.slim(the_rest)
        context.create_override
      end

      def self.build_other_context(context_name, filename, file_contents)
        context = Context.new(context_name)
        context.virtual_path(determine_virtual_path(filename))
        context.instance_eval(file_contents)
        context.create_override
      end

      def self.register
        Polyglot.register('deface', Deface::DSL::Loader)
      end

      def self.extract_dsl_commands_from_erb(html_file_contents)
        dsl_commands = ''

        while starts_with_html_comment?(html_file_contents)
          first_open_comment_index = html_file_contents.lstrip.index('<!--')
          first_close_comment_index = html_file_contents.index('-->')

          unless first_close_comment_index.nil?
            comment = html_file_contents[first_open_comment_index..first_close_comment_index+2]
          end

          comment.gsub('<!--', '').gsub('-->', '').strip.scan(/[^\s"']+|"[^"]*"|'[^']*'/).each do |part|

            dsl_commands =~ /('|")\z/ || part =~ /\A[^\d:='"%]/ ? dsl_commands << "\n" : dsl_commands << ' '
            dsl_commands << part
          end

          html_file_contents = html_file_contents.gsub(comment, '')
        end

        [dsl_commands, html_file_contents]
      end

      def self.extract_dsl_commands_from_haml(file_contents)
        dsl_commands = ''
{'fraction_non_alphanumeric': 0.11189003436426116, 'fraction_numerical': 0.0008247422680412372, 'mean_word_length': 2.7758173326414117, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, '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': '1844487', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2200, 'n_words': 358}