def add_to_index(dir, name, format, data, allow_same_ext = false)
      # spaces must be dashes
      dir.gsub!(' ', '-')
      name.gsub!(' ', '-')

      path = @wiki.page_file_name(name, format)

      dir  = '/' if dir.strip.empty?

      fullpath = ::File.join(*[dir, path])
      fullpath = fullpath[1..-1] if fullpath =~ /^\//

      if index.current_tree && (tree = index.current_tree / (@wiki.page_file_dir || '/'))
        tree = tree / dir unless tree.nil?
      end

      if tree
        downpath = path.downcase.sub(/\.\w+$/, '')

        tree.blobs.each do |blob|
          next if page_path_scheduled_for_deletion?(index.tree, fullpath)

          existing_file     = blob.name.downcase.sub(/\.\w+$/, '')
          existing_file_ext = ::File.extname(blob.name).sub(/^\./, '')

          new_file_ext = ::File.extname(path).sub(/^\./, '')

          if downpath == existing_file && !(allow_same_ext && new_file_ext == existing_file_ext)
            raise DuplicatePageError.new(dir, blob.name, path)
          end
        end
      end

      fullpath = fullpath.force_encoding('ascii-8bit') if fullpath.respond_to?(:force_encoding)

      begin
        data = @wiki.normalize(data)
      rescue ArgumentError => err
        # Swallow errors that arise from data being binary
        raise err unless err.message.include?('invalid byte sequence')
      end
      index.add(fullpath, data)
    end
def add_to_index(dir, name, format, data, allow_same_ext = false)
      # spaces must be dashes
      dir.gsub!(' ', '-')
      name.gsub!(' ', '-')

      path = @wiki.page_file_name(name, format)

      dir  = '/' if dir.strip.empty?

      fullpath = ::File.join(*[dir, path])
      fullpath = fullpath[1..-1] if fullpath =~ /^\//

      if index.current_tree && (tree = index.current_tree / (@wiki.page_file_dir || '/'))
        tree = tree / dir unless tree.nil?
      end

      if tree
        downpath = path.downcase.sub(/\.\w+$/, '')

        tree.blobs.each do |blob|
          next if page_path_scheduled_for_deletion?(index.tree, fullpath)

          existing_file     = blob.name.downcase.sub(/\.\w+$/, '')
          existing_file_ext = ::File.extname(blob.name).sub(/^\./, '')

          new_file_ext = ::File.extname(path).sub(/^\./, '')

          if downpath == existing_file && !(allow_same_ext && new_file_ext == existing_file_ext)
            raise DuplicatePageError.new(dir, blob.name, path)
          end
        end
      end

      fullpath = fullpath.force_encoding('ascii-8bit') if fullpath.respond_to?(:force_encoding)

      begin
        data = @wiki.normalize(data)
      rescue ArgumentError => err
        # Swallow errors that arise from data being binary
        raise err unless err.message.include?('invalid byte sequence')
      end
      index.add(fullpath, data)
    end
Adds a page to the given Index.

 dir    - The String subdirectory of the Gollum::Page without any
          prefix or suffix slashes (e.g. "foo/bar").
 name   - The String Gollum::Page filename_stripped.
 format - The Symbol Gollum::Page format.
 data   - The String wiki data to store in the tree map.
 allow_same_ext - A Boolean determining if the tree map allows the same
                  filename with the same extension.

 Raises Gollum::DuplicatePageError if a matching filename already exists.
 This way, pages are not inadvertently overwritten.

 Returns nothing (modifies the Index in place).