<commit_before>class MenuHandler
  def self.handle options
    source_file = options[:ex]['menu']
    return if ! source_file || options[:output] || options[:halt]
    file = "#{options[:last_source_dir]}#{source_file}"

    txt = File.read file

    path = Path.join(options[:args]||[])

    txt = Tree.children txt, path, options
    self.eval_when_exclamations txt, options

    options[:output] = txt
  end

  # If started with !, eval code...
  def self.eval_when_exclamations txt, options={}

    return if txt !~ /^! / || Keys.prefix == "source"

    source_file = options[:sources][-1][options[:source_index]]
    source_file = "#{options[:last_source_dir]}#{source_file}"

    line_number = options[:children_line]
    line_number += 4 if source_file =~ /\.rb$/

    # TODO: to tighten up, only do this if all lines start with "!"

    code = txt.gsub /^! /, ''

    exclamations_args = options[:exclamations_args] || []
    code = "args = #{exclamations_args.inspect}\n#{code}"

    returned, out, exception = Code.eval code, source_file, line_number

    returned ||= out   # Use output if nothing returned
    returned = returned.to_s if returned

    txt.replace(
      if exception
        CodeTree.draw_exception exception, code
      else
        returned   # Otherwise, just return return value or stdout!"
      end
      )
  end
end<commit_msg>Exclamations: fixed bug where they were left on blank lines when evaluating.<commit_after>class MenuHandler
  def self.handle options
    source_file = options[:ex]['menu']
    return if ! source_file || options[:output] || options[:halt]
    file = "#{options[:last_source_dir]}#{source_file}"

    txt = File.read file

    path = Path.join(options[:args]||[])

    txt = Tree.children txt, path, options
    self.eval_when_exclamations txt, options

    options[:output] = txt
  end

  # If started with !, eval code...
  def self.eval_when_exclamations txt, options={}

    return if txt !~ /^! / || Keys.prefix == "source"

    source_file = options[:sources][-1][options[:source_index]]
    source_file = "#{options[:last_source_dir]}#{source_file}"

    line_number = options[:children_line]
    line_number += 4 if source_file =~ /\.rb$/

    # TODO: to tighten up, only do this if all lines start with "!"

    code = txt.gsub /^! ?/, ''

    exclamations_args = options[:exclamations_args] || []
    code = "args = #{exclamations_args.inspect}\n#{code}"

    returned, out, exception = Code.eval code, source_file, line_number

    returned ||= out   # Use output if nothing returned
    returned = returned.to_s if returned

    txt.replace(
      if exception
        CodeTree.draw_exception exception, code
      else
        returned   # Otherwise, just return return value or stdout!"
      end
      )
  end
end
{'fraction_non_alphanumeric': 0.12652329749103944, 'fraction_numerical': 0.0014336917562724014, 'mean_word_length': 3.2096530920060333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4117050', 'n_tokens_mistral': 870, 'n_tokens_neox': 855, 'n_words': 230}