<commit_before>module Ginsu
  class Knife

    def self.slice
      self.partials
      self.pages
      self.templates
      self.links
      self.folders
    end

    def self.pages parameters=nil
      files = (parameters and parameters[:partials]) ? @@config.partials : @@config.pages
      files.each do |file|

        # These should probably raise errors.
        puts "Error: Each file requires a :static parameter to specify the source file." if
          file[:static].blank?
        puts "Error: Each file requires a :search parameter with a search string." if
          file[:search].blank?
        puts "Error: Each file requires a :partial parameter with the name of the destination template." if
          file[:partial].blank?

        # Open the static source HTML file.
        static_source_string = ''
        static_source_path = File.join(@@config.source, file[:static])
        File.open(static_source_path, "r") { |f|
          static_source_string = ''
          # Run a proc on each line?
          if file[:do]
            f.each {|line| static_source_string << file[:do].call(line)}
          else
            static_source_string = f.read
          end
        }
        static_source = Hpricot(static_source_string)

        # Use Hpricot to slice out the desired element.
        found = static_source.search(file[:search]).first

        # Make sure that the target folder exists.
        unless File.exists?(target_folder = File.join('app/views/', File.dirname(file[:partial])))
          Dir.mkdir(target_folder)
        end

        # Drop that found string into the appropriate partial.
        target_filename = file[:partial]
        puts "Slicing '#{target_filename}' from '#{static_source_path}'..."
        target_filename.gsub!(/\/([^\_][^\/]+$)/) {|string| '/_' + $1 } if<commit_msg>Added support for optional pages.<commit_after>module Ginsu
  class Knife

    def self.slice
      self.partials
      self.pages
      self.templates
      self.links
      self.folders
    end

    def self.pages parameters=nil
      files = (parameters and parameters[:partials]) ? @@config.partials : @@config.pages
      files.each do |file|

        # These should probably raise errors.
        puts "Error: Each file requires a :static parameter to specify the source file." if
          file[:static].blank?
        puts "Error: Each file requires a :search parameter with a search string." if
          file[:search].blank?
        puts "Error: Each file requires a :partial parameter with the name of the destination template." if
          file[:partial].blank?

        # Open the static source HTML file.
        static_source_string = ''
        static_source_path = File.join(@@config.source, file[:static])

        begin
          File.open(static_source_path, "r") { |f|
            static_source_string = ''
            # Run a proc on each line?
            if file[:do]
              f.each {|line| static_source_string << file[:do].call(line)}
            else
              static_source_string = f.read
            end
          }
          static_source = Hpricot(static_source_string)
        rescue SystemCallError
          $stderr.print "IO failed: " + $!
          if file[:optional]
            next
          else
            raise
          end
        end

        # Use Hpricot to slice out the desired element.
        found = static_source.search(file[:search]).first

        # Make sure that the target folder exists.
        unless File.exists?(target_folder = File.join('app/views/', File.dirname(file[:partial])))
          Dir.mkdir(target_folder)
        end

        # Drop that found string into the appropriate partial.
        target_filename = file[:partial]
        puts "Slicing '#{target_filename}' from '#{static_source_path}'..."
        target_filename.gsub!(/\/([^\_][^\/]+$)/) {|string| '/_' + $1 } if
{'fraction_non_alphanumeric': 0.1044776119402985, 'fraction_numerical': 0.000514668039114771, 'mean_word_length': 2.1601626016260163, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4467865', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1042, 'n_words': 314}