<commit_before># frozen_string_literal: true

module Jekyll
  class Regenerator
    attr_reader :site, :metadata, :cache
    attr_accessor :disabled
    private :disabled, :disabled=

    def initialize(site)
      @site = site

      # Read metadata from file
      read_metadata

      # Initialize cache to an empty hash
      clear_cache
    end

    # Checks if a renderable object needs to be regenerated
    #
    # Returns a boolean.
    def regenerate?(document)
      case document
      when Page
        regenerate_page?(document)
      when Document
        regenerate_document?(document)
      else
        source_path = document.respond_to?(:path) ? document.path : nil
        dest_path = if document.respond_to?(:destination)
                      document.destination(@site.dest)
                    end
        source_modified_or_dest_missing?(source_path, dest_path)
      end
    end

    # Add a path to the metadata
    #
    # Returns true, also on failure.
    def add(path)
      return true unless File.exist?(path)

      metadata[path] = {
        "mtime" => File.mtime(path),
        "deps"  => [],
      }
      cache[path] = true
    end

    # Force a path to regenerate
    #
    # Returns true.
    def force(path)
      cache[path] = true
    end

    # Clear the metadata and cache
    #
    # Returns nothing
    def clear
      @metadata = {}
      clear_cache
    end

    # Clear just the cache
    #
    # Returns nothing
    def clear_cache
      @cache = {}
    end

    # Checks if the source has been modified or the
    # destination is missing
    #
    # returns a boolean
    def source_modified_or_dest_missing?(source_path, dest_path)
      modified?(source_path) || (dest_path && !File.exist?(dest_path))
    end

    # Checks if a path's (or one of its dependencies)
    # mtime has changed
    #
    # Returns a boolean.
    def modified?(path)
      return true if disabled?

      # objects that don't have a path are always regenerated
      return true if path.nil?

      # Check for path in cache
      if cache.key? path
        return cache[path]
      end

      if metadata[path]
        # If we have seen this file before,
        # check if it or one of its dependencies has been modified
        existing_file_modified?(path)
      else
        # If we have not seen this file before, add it to the metadata and regenerate it
        add(path)
      end
    end

    # Add a dependency of a path
    #
    # Returns nothing.
    def add_dependency(path, dependency)
      return if metadata[path].nil? || disabled

      unless metadata[path]["deps"].include? dependency
        metadata[path]["deps"] << dependency
        add(dependency) unless metadata.include?(dependency)
      end
      regenerate? dependency
    end

    # Write the metadata to disk
    #
    # Returns nothing.
    def write_metadata
      unless disabled?
        File.binwrite(metadata_file, Marshal.dump(metadata))
      end
    end

    # Produce the absolute path of the metadata file
    #
    # Returns the String path of the file.
    def metadata_file
      site.in_source_dir(".jekyll-metadata")<commit_msg>memoize path to metadata file (#6602)<commit_after># frozen_string_literal: true

module Jekyll
  class Regenerator
    attr_reader :site, :metadata, :cache
    attr_accessor :disabled
    private :disabled, :disabled=

    def initialize(site)
      @site = site

      # Read metadata from file
      read_metadata

      # Initialize cache to an empty hash
      clear_cache
    end

    # Checks if a renderable object needs to be regenerated
    #
    # Returns a boolean.
    def regenerate?(document)
      case document
      when Page
        regenerate_page?(document)
      when Document
        regenerate_document?(document)
      else
        source_path = document.respond_to?(:path) ? document.path : nil
        dest_path = if document.respond_to?(:destination)
                      document.destination(@site.dest)
                    end
        source_modified_or_dest_missing?(source_path, dest_path)
      end
    end

    # Add a path to the metadata
    #
    # Returns true, also on failure.
    def add(path)
      return true unless File.exist?(path)

      metadata[path] = {
        "mtime" => File.mtime(path),
        "deps"  => [],
      }
      cache[path] = true
    end

    # Force a path to regenerate
    #
    # Returns true.
    def force(path)
      cache[path] = true
    end

    # Clear the metadata and cache
    #
    # Returns nothing
    def clear
      @metadata = {}
      clear_cache
    end

    # Clear just the cache
    #
    # Returns nothing
    def clear_cache
      @cache = {}
    end

    # Checks if the source has been modified or the
    # destination is missing
    #
    # returns a boolean
    def source_modified_or_dest_missing?(source_path, dest_path)
      modified?(source_path) || (dest_path && !File.exist?(dest_path))
    end

    # Checks if a path's (or one of its dependencies)
    # mtime has changed
    #
    # Returns a boolean.
    def modified?(path)
      return true if disabled?

      # objects that don't have a path are always regenerated
      return true if path.nil?

      # Check for path in cache
      if cache.key? path
        return cache[path]
      end

      if metadata[path]
        # If we have seen this file before,
        # check if it or one of its dependencies has been modified
        existing_file_modified?(path)
      else
        # If we have not seen this file before, add it to the metadata and regenerate it
        add(path)
      end
    end

    # Add a dependency of a path
    #
    # Returns nothing.
    def add_dependency(path, dependency)
      return if metadata[path].nil? || disabled

      unless metadata[path]["deps"].include? dependency
        metadata[path]["deps"] << dependency
        add(dependency) unless metadata.include?(dependency)
      end
      regenerate? dependency
    end

    # Write the metadata to disk
    #
    # Returns nothing.
    def write_metadata
      unless disabled?
        File.binwrite(metadata_file, Marshal.dump(metadata))
      end
    end

    # Produce the absolute path of the metadata file
    #
    # Returns the String path of the file.
    def metadata_file
      @metadata_file ||= site.in_source_dir(".jekyll-metadata")
{'fraction_non_alphanumeric': 0.07305577376276512, 'fraction_numerical': 0.0006284367635506677, 'mean_word_length': 2.1546085232903867, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1174026', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1769, 'n_words': 595}