<commit_before>require_relative 'hpricot_monkeypatches'

module HTML2Slim
  class Converter
    def to_s
      @slim
    end
  end

  class HTMLConverter < Converter
    def initialize(html)
      @slim = Hpricot(html).to_slim
    end
  end

  class ERBConverter < Converter
    def initialize(input)
      @erb = read_erb(input)
      prepare_curly_blocks!
      prepare_control_flow_statements!
      prepare_else_statements!
      prepare_elsif_statements!
      prepare_when_statements!
      prepare_end_statements!
      prepare_regular_ruby_code!
      @slim ||= Hpricot(@erb).to_slim
    end

    private

    # input may be either string or IO.
    # open.read makes it works for files & IO
    def read_erb(input)
      if File.exists?(input)
        open(input).read
      else
        input
      end
    end

    def prepare_curly_blocks!
      @erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/) {
        %(<%#{$1} do #{$2}%>)
      }
    end

    def prepare_control_flow_statements!
      @erb.gsub!(/<%(-\s+)?((\s*(case|if|for|unless|until|while) .+?)|.+?do\s*(\|.+?\|)?\s*)-?%>/) {
        %(<ruby code="#{$2.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_else_statements!
      @erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
    end

    def prepare_elsif_statements!
      @erb.gsub!(/<%-?\s*(elsif .+?)\s*-?%>/) {
        %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_when_statements!
      @erb.gsub!(/<%-?\s*(when .+?)\s*-?%>/) {
        %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_end_statements!
      @erb.gsub!(/<%\s*(end|}|end\s+-)\s*%>/, %(</ruby>))
    end

    def prepare_regular_ruby_code!
      @erb.gsub!(/<%(.+?)\s*-?%>/m) {
        %(<ruby code="#{$1.gsub(/"/, '&quot;')}"></ruby>)
      }
    end
  end
end<commit_msg>fix missing dash<commit_after>require_relative 'hpricot_monkeypatches'

module HTML2Slim
  class Converter
    def to_s
      @slim
    end
  end

  class HTMLConverter < Converter
    def initialize(html)
      @slim = Hpricot(html).to_slim
    end
  end

  class ERBConverter < Converter
    def initialize(input)
      @erb = read_erb(input)
      prepare_curly_blocks!
      prepare_control_flow_statements!
      prepare_else_statements!
      prepare_elsif_statements!
      prepare_when_statements!
      prepare_end_statements!
      prepare_regular_ruby_code!
      @slim ||= Hpricot(@erb).to_slim
    end

    private

    # input may be either string or IO.
    # open.read makes it works for files & IO
    def read_erb(input)
      if File.exists?(input)
        open(input).read
      else
        input
      end
    end

    def prepare_curly_blocks!
      @erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/) {
        %(<%#{$1} do #{$2}%>)
      }
    end

    def prepare_control_flow_statements!
      @erb.gsub!(/<%(-\s+)?((\s*(case|if|for|unless|until|while) .+?)|.+?do\s*(\|.+?\|)?\s*)-?%>/) {
        %(<ruby code="#{$2.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_else_statements!
      @erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
    end

    def prepare_elsif_statements!
      @erb.gsub!(/<%-?\s*(elsif .+?)\s*-?%>/) {
        %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_when_statements!
      @erb.gsub!(/<%-?\s*(when .+?)\s*-?%>/) {
        %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">)
      }
    end

    def prepare_end_statements!
      @erb.gsub!(/<%\s*(end|}|end\s+-)\s*%>/, %(</ruby>))
    end

    def prepare_regular_ruby_code!
      @erb.gsub!(/<%-?(.+?)\s*-?%>/m) {
        %(<ruby code="#{$1.gsub(/"/, '&quot;')}"></ruby>)
      }
    end
  end
end
{'fraction_non_alphanumeric': 0.21569156106767323, 'fraction_numerical': 0.0037746023186842814, 'mean_word_length': 2.366606170598911, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 39, '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': '5494281', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1450, 'n_words': 225}