<commit_before>module Brief
  class Document
    include Brief::Document::Rendering
    include Brief::Document::FrontMatter
    include Brief::Document::Templating
    include Brief::Document::Attachments
    include Brief::Document::SourceMap

    def self.from_contents(content, frontmatter, &block)
    end

    attr_accessor :path, :content, :frontmatter, :raw_content, :options

    def initialize(path, options = {})
      if path.respond_to?(:key?) && options.empty?
        @frontmatter = path.to_mash
      else
        @path = Pathname(path)
      end

      @options = options.to_mash

      if @path && self.path.exist?
        @raw_content = self.path.read
        load_frontmatter
      elsif options[:contents]
        @raw_content = options[:contents]
      end
    end

    def document
      self
    end

    def clone
      new(path, options)
    end

    def title
      (data && data.title) || css('h1:first-of-type').text || path.to_s.split("/").last.gsub(/\..*/,'')
    end

    def to_s
      "#{ model_class }.at_path(#{relative_path})"
    end

    def inspect
      "#{ model_class }.at_path(#{relative_path})"
    end

    def relative_path
      briefcase.present? ? path.relative_path_from(briefcase.docs_path) : path
    end

    def content_hash
      Digest::MD5.hexdigest(@content.to_s)
    end

    def file_hash
      Digest::MD5.hexdigest(path.read.to_s)
    end

    def content_stale?
      content_hash != file_hash
    end

    def raw= val
      @raw_set = true
      @raw_content = val
      #document.load_frontmatter
      @raw_content
    end

    def set_raw?
      !!@raw_set
    end

    def save
      if set_raw?
        file_contents = raw_content
      else
        file_contents = combined_data_and_content
      end

      path.open('w') {|fh| fh.write(file_contents) }
      refresh!
    end

    def refresh!
      @content = nil
      @raw_content = path.read
      @frontmatter = nil
      @raw_frontmatter = nil
      @refreshing = true
      @content_hash = nil
      load_frontmatter
      true
    end

    def save!
      if set_raw?
        file_contents = raw_content<commit_msg>adding a convenience method for getting a refreshed document<commit_after>module Brief
  class Document
    include Brief::Document::Rendering
    include Brief::Document::FrontMatter
    include Brief::Document::Templating
    include Brief::Document::Attachments
    include Brief::Document::SourceMap

    def self.from_contents(content, frontmatter, &block)
    end

    attr_accessor :path, :content, :frontmatter, :raw_content, :options

    def initialize(path, options = {})
      if path.respond_to?(:key?) && options.empty?
        @frontmatter = path.to_mash
      else
        @path = Pathname(path)
      end

      @options = options.to_mash

      if @path && self.path.exist?
        @raw_content = self.path.read
        load_frontmatter
      elsif options[:contents]
        @raw_content = options[:contents]
      end
    end

    def document
      self
    end

    def clone
      new(path, options)
    end

    def title
      (data && data.title) || css('h1:first-of-type').text || path.to_s.split("/").last.gsub(/\..*/,'')
    end

    def to_s
      "#{ model_class }.at_path(#{relative_path})"
    end

    def inspect
      "#{ model_class }.at_path(#{relative_path})"
    end

    def relative_path
      briefcase.present? ? path.relative_path_from(briefcase.docs_path) : path
    end

    def content_hash
      Digest::MD5.hexdigest(@content.to_s)
    end

    def file_hash
      Digest::MD5.hexdigest(path.read.to_s)
    end

    def content_stale?
      content_hash != file_hash
    end

    def raw= val
      @raw_set = true
      @raw_content = val
      #document.load_frontmatter
      @raw_content
    end

    def set_raw?
      !!@raw_set
    end

    def save
      if set_raw?
        file_contents = raw_content
      else
        file_contents = combined_data_and_content
      end

      path.open('w') {|fh| fh.write(file_contents) }
      refresh!
    end

    def refresh!
      @content = nil
      @raw_content = path.read
      @frontmatter = nil
      @raw_frontmatter = nil
      @refreshing = true
      @content_hash = nil
      load_frontmatter
      true
    end

    def refreshed
      refresh!
      self
    end

    def save!
      if set_raw?
        file_contents = raw_content
{'fraction_non_alphanumeric': 0.09763313609467456, 'fraction_numerical': 0.0013654984069185253, 'mean_word_length': 2.257968865826538, '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': '3060415', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1376, 'n_words': 308}