<commit_before>module Rack
  module Blogengine
    class DocParser
      # Parse in .content Documents.
      # @param target.
      # @return [Hash] Documents 
      def self.parseInDocuments(target)
        @target = target
        documents = []

        layout_file = ::File.open("#{@target}/assets/layout/layout.html", "r")
        @layout = layout_file.read

        Dir.foreach("#{target}/") do |item|
          extension = item.split(".")[1]
          next if item == '.' or item == '..' or extension != "content"
  					
          getFileContents(item)
          @html = fillFileContents(@layout)
  					
          @document = Document.new
          @document.path = @path
          @document.html = @html
          @document.title = @title
          @document.date = @date

          documents << @document
        end

        sort documents

        # Has to exec operator after all docs were read, so documents are available for operators (list all docs, etc...)
        documents.each do |document|
          document.exec_content_operator(documents, @target)
        end

        documentshashed = documents.map do |document|
          document.to_hash
        end

        return documentshashed
      end

      # Get File Contents (path, title, content)
      # @param [String] file
      def self.getFileContents(file)
        content_file = ::File.open("#{@target}/#{file}");
        content = content_file.read

        # Replace Closing tags
        content["/path"] = "/close"
        content["/title"] = "/close"
        content["/content"] = "/close"
        content["/date"] = "/close"
  
        contentarray = content.split("[/close]")

        contentarray.each do |contentblock|
          if contentblock.include? "[path]:"
            contentblock["[path]:"] = ""
            @path = "/#{contentblock}"
          end

          if contentblock.include? "[title]:"
            contentblock["[title]:"] = ""
            @title = contentblock.strip
          end

          if contentblock.include? "[content]:"
            contentblock["[content]:"] = ""
            @content = contentblock.strip
          end

          if contentblock.include? "[date]:"
            contentblock["[date]:"] = ""
            datearray = contentblock.split(",")
            datearray = datearray.map do |date|
              date = date.to_f
            end

            @date = Date.new(datearray[0], datearray[1], datearray[2])
          end
        end
      end

	    # Replace layout placeholder with content from .content file
	    # @param [String] layout
	    # return [String] html placeholder replaced with content
      def self.fillFileContents(layout)
        html = layout.dup

        html.gsub! "{title}", @title
        html["{content}"] = @content
        html.gsub! "{date}", @date.strftime('%a %d %b %Y')

        return html
      end

      # Sort documents array by date of each documenthash
      # @param [Array] documents
      # return [Array] documents (sorted)
      # Should it be sorted in Core or in the operator??
      def self.sort(documents)
        documents.sort! do | a, b |
          a.date.to_time.to_i <=> b.date.to_time.to_i<commit_msg>source styling<commit_after>module Rack
  module Blogengine
    class DocParser
      # Parse in .content Documents.
      # @param target.
      # @return [Hash] Documents 
      def self.parseInDocuments(target)
        @target = target
        documents = []

        layout_file = ::File.open("#{@target}/assets/layout/layout.html", "r")
        @layout = layout_file.read

        Dir.foreach("#{target}/") do |item|
          extension = item.split(".")[1]
          next if item == '.' or item == '..' or extension != "content"
  					
          getFileContents(item)
          @html = fillFileContents(@layout)
  					
          @document = Document.new
          @document.path = @path
          @document.html = @html
          @document.title = @title
          @document.date = @date

          documents << @document
        end

        sort documents

        # Has to exec operator after all docs were read, so documents are available for operators (list all docs, etc...)
        documents.each do |document|
          document.exec_content_operator(documents, @target)
        end

        documentshashed = documents.map do |document|
          document.to_hash
        end

        return documentshashed
      end

      # Get File Contents (path, title, content)
      # @param [String] file
      def self.getFileContents(file)
        content_file = ::File.open("#{@target}/#{file}");
        content = content_file.read

        # Replace Closing tags
        content["/path"] = "/close"
        content["/title"] = "/close"
        content["/content"] = "/close"
        content["/date"] = "/close"
  
        contentarray = content.split("[/close]")

        contentarray.each do |contentblock|
          if contentblock.include? "[path]:"
            contentblock["[path]:"] = ""
            @path = "/#{contentblock}"
          end

          if contentblock.include? "[title]:"
            contentblock["[title]:"] = ""
            @title = contentblock.strip
          end

          if contentblock.include? "[content]:"
            contentblock["[content]:"] = ""
            @content = contentblock.strip
          end

          if contentblock.include? "[date]:"
            contentblock["[date]:"] = ""
            datearray = contentblock.split(",")
            datearray = datearray.map do |date|
              date = date.to_f
            end

            @date = Date.new(datearray[0], datearray[1], datearray[2])
          end
        end
      end

      # Replace layout placeholder with content from .content file
      # @param [String] layout
      # return [String] html placeholder replaced with content
      def self.fillFileContents(layout)
        html = layout.dup

        html.gsub! "{title}", @title
        html["{content}"] = @content
        html.gsub! "{date}", @date.strftime('%a %d %b %Y')

        return html
      end

      # Sort documents array by date of each documenthash
      # @param [Array] documents
      # return [Array] documents (sorted)
      # Should it be sorted in Core or in the operator??
      def self.sort(documents)
        documents.sort! do | a, b |
          a.date.to_time.to_i <=> b.date.to_time.to_i
{'fraction_non_alphanumeric': 0.12308174130911369, 'fraction_numerical': 0.001252740369558409, 'mean_word_length': 1.8913535536441828, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '1344249', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1721, 'n_words': 462}