<commit_before>module GollumRails
  module Adapters
    module Gollum

      # Main page class for the Gollum connector.
      #
      # It provides some awesome features for connecting gollum to gollum_rails such as:
      #   * new_page
      #   * find_page
      #   * delete_page
      #   * rename_page
      #   * move_page
      #   * first_page_commit
      #   * page_creation time
      #   * ...
      #
      class Page

        Connector.page_class = self

        class << self

          # == Parses a given filepath e.g. '/test/page'
          # Name is page
          # path is /test
          #
          # Returns a Hash
          def parse_path(name)
            path = '/'
            name = name.to_s
            if name.include?('/')
              name = name[1..-1] if name[0] == "/"
              content = name.split('/')
              name = content.pop
              path = '/'+content.join('/')
            end
            { path: path, name: name }
          end
          
          # finds all versions of a page
          #
          # name - the pagename to search
          # wiki - instance of Gollum::Wiki
          #
          # Returns the Gollum::Page class
          def find_page(name, wiki)
            path_data = parse_path(name)
            wiki.paged(path_data[:name], path_data[:path], exact = true)
          end
        end

        # == Creates a new page
        #
        # name - The name of the page
        # content - The content of the page
        # wiki - An instance of Gollum::Wiki
        # type - A filetype as symbol (optional)
        # commit - Commit Hash
        #
        # Returns the page
        def new_page( name, content, wiki, type=:markdown, commit={} )
          path_data = self.class.parse_path(name)
          wiki.write_page( path_data[:name], type, content, commit, path_data[:path].gsub!(/^\//, "").gsub!(/(\/)+$/,'') || "" )
          self.class.find_page( name, wiki )
        end

        # == Updates an existing page
        #
        # page - An instance of Gollum::Page
        # wiki - An instance of Gollum::Wiki
        # content - New content
        # commit - Commit Hash
        # name - A new String (optional)
        # format - A filetype as symbol (optional)
        #
        # Returns the page
        def update_page( page, wiki, content=nil, commit={}, name=nil, format=nil)
          return if !page || ((!content||page.raw_data == content) && page.format == format)
          name ||= page.name
          format = (format || page.format).to_sym
          content ||= page.raw_data
          wiki.update_page(page,name,format,content.to_s,commit)
          self.class.find_page( mixin(page.url_path, name), wiki )
        end
        
        # == Preview page
        #
        # wiki - An instance of Gollum::Wiki
        # content - New content
        # name - A String
        # format - A filetype as symbol (optional)
        #
        def preview_page(wiki, name, content, format=:markdown)
          page = wiki.preview_page(name,content,format)
          page.formatted_data
        end

        # == Deletes an existing page
        #<commit_msg>fixed page return on wrong update parameters<commit_after>module GollumRails
  module Adapters
    module Gollum

      # Main page class for the Gollum connector.
      #
      # It provides some awesome features for connecting gollum to gollum_rails such as:
      #   * new_page
      #   * find_page
      #   * delete_page
      #   * rename_page
      #   * move_page
      #   * first_page_commit
      #   * page_creation time
      #   * ...
      #
      class Page

        Connector.page_class = self

        class << self

          # == Parses a given filepath e.g. '/test/page'
          # Name is page
          # path is /test
          #
          # Returns a Hash
          def parse_path(name)
            path = '/'
            name = name.to_s
            if name.include?('/')
              name = name[1..-1] if name[0] == "/"
              content = name.split('/')
              name = content.pop
              path = '/'+content.join('/')
            end
            { path: path, name: name }
          end
          
          # finds all versions of a page
          #
          # name - the pagename to search
          # wiki - instance of Gollum::Wiki
          #
          # Returns the Gollum::Page class
          def find_page(name, wiki)
            path_data = parse_path(name)
            wiki.paged(path_data[:name], path_data[:path], exact = true)
          end
        end

        # == Creates a new page
        #
        # name - The name of the page
        # content - The content of the page
        # wiki - An instance of Gollum::Wiki
        # type - A filetype as symbol (optional)
        # commit - Commit Hash
        #
        # Returns the page
        def new_page( name, content, wiki, type=:markdown, commit={} )
          path_data = self.class.parse_path(name)
          wiki.write_page( path_data[:name], type, content, commit, path_data[:path].gsub!(/^\//, "").gsub!(/(\/)+$/,'') || "" )
          self.class.find_page( name, wiki )
        end

        # == Updates an existing page
        #
        # page - An instance of Gollum::Page
        # wiki - An instance of Gollum::Wiki
        # content - New content
        # commit - Commit Hash
        # name - A new String (optional)
        # format - A filetype as symbol (optional)
        #
        # Returns the page
        def update_page( page, wiki, content=nil, commit={}, name=nil, format=nil)
          return page if page.nil?
          name ||= page.name
          format = (format || page.format).to_sym
          content ||= page.raw_data
          
          wiki.update_page(page,name,format,content.to_s,commit) unless ((!content||page.raw_data == content) && page.format == format)
          
          self.class.find_page( mixin(page.url_path, name), wiki )
        end
        
        # == Preview page
        #
        # wiki - An instance of Gollum::Wiki
        # content - New content
        # name - A String
        # format - A filetype as symbol (optional)
        #
        def preview_page(wiki, name, content, format=:markdown)
          page = wiki.preview_page(name,content,format)
          page.formatted_data
        end

        # == Deletes an existing page
        #
{'fraction_non_alphanumeric': 0.10550171928727728, 'fraction_numerical': 0.0009377930603313535, 'mean_word_length': 1.5103962338171832, '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': '238105', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1809, 'n_words': 516}