<commit_before>require_relative 'helpers/jekyll_post_helper'

module Jepeto

  # Don't edit this constant hash!
  # This can be overridden by the DEFAULT_OPTIONS hash
  # or by options passed in by the user.
  HARDCODED_DEFAULT_OPTIONS = {
    date:      Date.today.to_s,
    extension: 'markdown',
    published: false,
    layout:    'default',
    location:  '.',
    debug:     false
  }

  # This array should contain all the file extensions supported by jekyll
  VALID_FILE_EXTENSIONS = [
    'markdown', 'mdown', 'md',
    'textile'
  ]

  POST_DIRECTORY = "_posts"

  class JekyllPost
    include Jepeto::JekyllPostHelper
    
    attr_reader :options

    def initialize(options)
      define_instance_variables!
      @options = check_options(options)
    end

    # Automagically create custom accessor methods for each option
    %w[title date extension published layout].each do |option|
      define_method(option) do
        instance_variable_get("@options").fetch(option.to_sym)
      end
    end

    # Automagically create custom accessor methods for the other attributes
    %w[slug filename yaml_front_matter].each do |attribute|
      define_method(attribute) do
        send "generate_#{attribute}" if instance_variable_get("@#{attribute}").nil?
      end
    end

    def save!

      Dir.chdir('..') if Dir.getwd.include?(Jepeto::POST_DIRECTORY)
      post_file = File.join(Jepeto::POST_DIRECTORY, self.filename)

      unless File.writable?(Jepeto::POST_DIRECTORY)
        raise "The post directory is not wriatble"
        exit
      end

      if File.exists?(post_file)
        raise "A post file with the same name already exists"
        exit
      end

      File.open(post_file, 'w') do |file|
        file.puts yaml_front_matter
      end

      File.expand_path(post_file)
    end

    private

    def define_instance_variables!
      %w[slug filename yaml_front_matter location].each do |attribute|
        instance_variable_set("@#{attribute}", nil)
      end
    end

    def check_options(options)
      # If the user defined default values via the DEFAULT_OPTIONS constant
      # replace all the nil values with default values.

      jprc_options = ""
      config_file = File.expand_path("~/.jprc")
      if File.exists?(config_file)
        File.open(config_file, 'r') do |file|
          while line = file.gets
            jprc_options << line
          end
        end
         jprc_options = YAML.load(jprc_options)
         jprc_options = jprc_options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

         options = merge_options(options, jprc_options)
      end

      # If there are still some nil values, replace them with default values from
      # the HARDCODED_DEFAULT_OPTIONS constant.
      options = merge_options(options, Jepeto::HARDCODED_DEFAULT_OPTIONS)

      options[:extension] = check_extension(options[:extension])

      # At this point, the only value that could potentially
      # be nil or empty is the title.
      # That is unacceptable!!
      if options[:title].nil? || options[:title].empty?
        raise ArgumentError, "The post file can't be created without a fucking title!!!"
      end

      set_location!(options[:location], options[:debug])

      options
    end

    def debug?
      @options[:debug]
    end

    def merge_options(options, default_options)
      default_options.each do |option, value|
        if options[option].nil?
          options[option] = value
        end
      end

      options
    end

    def set_location!(location, debug = false)
      # location of the _posts folder not locaation of the post file!!
      location ||= get_default_location

      # Remove the trailling slash if they're there
      location.chomp!('/')

      if (location[-5] == Jepeto::POST_DIRECTORY && File.directory?(location))
        # if the script was called from within the posts directory
        location.chomp!(Jepeto::POST_DIRECTORY)
      else
        raise "Unable to find the posts directory" unless (debug || File.directory?(location.chomp('/') + '/' + Jepeto::POST_DIRECTORY))
      end

      # Make sure there isn't a trailling slash
      # and return the full path
      @location ||= File.expand_path(location.chomp('/'))
    end

    def get_default_location
      if Jepeto.const_defined?(:DEFAULT_OPTIONS) && !Jepeto::DEFAULT_OPTIONS[:location].nil?
        location = Jepeto::DEFAULT_OPTIONS[:location]
      else
        location = Jepeto::HARDCODED_DEFAULT_OPTIONS[:location]
      end

      File.join(location, Jepeto::POST_DIRECTORY)
    end

    def check_extension(extension)
      extension.slice!(0) if extension[0] == '.'
      raise "#{extension} is not a valid extension." unless VALID_FILE_EXTENSIONS.include?(extension)

      @extension = extension
    end
<commit_msg>Removed debug mode.<commit_after>require_relative 'helpers/jekyll_post_helper'

module Jepeto

  # Don't edit this constant hash!
  # This can be overridden by the DEFAULT_OPTIONS hash
  # or by options passed in by the user.
  HARDCODED_DEFAULT_OPTIONS = {
    date:      Date.today.to_s,
    extension: 'markdown',
    published: false,
    layout:    'default',
    location:  '.'
  }

  # This array should contain all the file extensions supported by jekyll
  VALID_FILE_EXTENSIONS = [
    'markdown', 'mdown', 'md',
    'textile'
  ]

  POST_DIRECTORY = "_posts"

  class JekyllPost
    include Jepeto::JekyllPostHelper
    
    attr_reader :options

    def initialize(options)
      define_instance_variables!
      @options = check_options(options)
    end

    # Automagically create custom accessor methods for each option
    %w[title date extension published layout].each do |option|
      define_method(option) do
        instance_variable_get("@options").fetch(option.to_sym)
      end
    end

    # Automagically create custom accessor methods for the other attributes
    %w[slug filename yaml_front_matter].each do |attribute|
      define_method(attribute) do
        send "generate_#{attribute}" if instance_variable_get("@#{attribute}").nil?
      end
    end

    def save!

      Dir.chdir('..') if Dir.getwd.include?(Jepeto::POST_DIRECTORY)
      post_file = File.join(Jepeto::POST_DIRECTORY, self.filename)

      unless File.writable?(Jepeto::POST_DIRECTORY)
        raise "The post directory is not wriatble"
        exit
      end

      if File.exists?(post_file)
        raise "A post file with the same name already exists"
        exit
      end

      File.open(post_file, 'w') do |file|
        file.puts yaml_front_matter
      end

      File.expand_path(post_file)
    end

    private

    def define_instance_variables!
      %w[slug filename yaml_front_matter location].each do |attribute|
        instance_variable_set("@#{attribute}", nil)
      end
    end

    def check_options(options)
      # If the user defined default values via the DEFAULT_OPTIONS constant
      # replace all the nil values with default values.

      jprc_options = ""
      config_file = File.expand_path("~/.jprc")
      if File.exists?(config_file)
        File.open(config_file, 'r') do |file|
          while line = file.gets
            jprc_options << line
          end
        end
         jprc_options = YAML.load(jprc_options)
         jprc_options = jprc_options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

         options = merge_options(options, jprc_options)
      end

      # If there are still some nil values, replace them with default values from
      # the HARDCODED_DEFAULT_OPTIONS constant.
      options = merge_options(options, Jepeto::HARDCODED_DEFAULT_OPTIONS)

      options[:extension] = check_extension(options[:extension])

      # At this point, the only value that could potentially
      # be nil or empty is the title.
      # That is unacceptable!!
      if options[:title].nil? || options[:title].empty?
        raise ArgumentError, "The post file can't be created without a fucking title!!!"
      end

      set_location!(options[:location])

      options
    end

    def merge_options(options, default_options)
      default_options.each do |option, value|
        if options[option].nil?
          options[option] = value
        end
      end

      options
    end

    def set_location!(location)
      # location of the _posts folder not locaation of the post file!!
      location ||= get_default_location

      # Remove the trailling slash if they're there
      location.chomp!('/')

      if (location[-5] == Jepeto::POST_DIRECTORY && File.directory?(location))
        # if the script was called from within the posts directory
        location.chomp!(Jepeto::POST_DIRECTORY)
      else
        raise "Unable to find the posts directory" unless ( File.directory?(location.chomp('/') + '/' + Jepeto::POST_DIRECTORY))
      end

      # Make sure there isn't a trailling slash
      # and return the full path
      @location ||= File.expand_path(location.chomp('/'))
    end

    def get_default_location
      if Jepeto.const_defined?(:DEFAULT_OPTIONS) && !Jepeto::DEFAULT_OPTIONS[:location].nil?
        location = Jepeto::DEFAULT_OPTIONS[:location]
      else
        location = Jepeto::HARDCODED_DEFAULT_OPTIONS[:location]
      end

      File.join(location, Jepeto::POST_DIRECTORY)
    end

    def check_extension(extension)
      extension.slice!(0) if extension[0] == '.'
      raise "#{extension} is not a valid extension." unless VALID_FILE_EXTENSIONS.include?(extension)

      @extension = extension
    end

{'fraction_non_alphanumeric': 0.09170259753917341, 'fraction_numerical': 0.0006309811757282574, 'mean_word_length': 2.822347266881029, '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': '5799225', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2772, 'n_words': 770}