Class: Gollum::File

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-lib/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki) ⇒ File

Public: Initialize a file.

wiki - The Gollum::Wiki in question.

Returns a newly initialized Gollum::File.



11
12
13
14
15
# File 'lib/gollum-lib/file.rb', line 11

def initialize(wiki)
  @wiki = wiki
  @blob = nil
  @path = nil
end

Instance Attribute Details

#pathObject (readonly)

Public: The String path of the file.



59
60
61
# File 'lib/gollum-lib/file.rb', line 59

def path
  @path
end

#versionObject

Public: The Grit::Commit version of the file.



56
57
58
# File 'lib/gollum-lib/file.rb', line 56

def version
  @version
end

Instance Method Details

#escaped_url_pathObject

Public: The url_path, but CGI escaped.

Returns the String url_path



29
30
31
# File 'lib/gollum-lib/file.rb', line 29

def escaped_url_path
  CGI.escape(self.url_path).gsub('%2F','/')
end

#find(name, version) ⇒ Object

Find a file in the given Gollum repo.

name - The full String path. version - The String version ID to find.

Returns a Gollum::File or nil if the file could not be found.



90
91
92
93
94
95
96
97
98
99
# File 'lib/gollum-lib/file.rb', line 90

def find(name, version)
  checked = name.downcase
  map     = @wiki.tree_map_for(version)
  if entry = map.detect { |entry| entry.path.downcase == checked }
    @path    = name
    @blob    = entry.blob(@wiki.repo)
    @version = version.is_a?(Grit::Commit) ? version : @wiki.commit_for(version)
    self
  end
end

#mime_typeObject

Public: The String mime type of the file.



62
63
64
# File 'lib/gollum-lib/file.rb', line 62

def mime_type
  @blob.mime_type
end

#nameObject Also known as: filename

Public: The on-disk filename of the file.

Returns the String name.



36
37
38
# File 'lib/gollum-lib/file.rb', line 36

def name
  @blob && @blob.name
end

#populate(blob, path = nil) ⇒ Object

Populate the File with information from the Blob.

blob - The Grit::Blob that contains the info. path - The String directory path of the file.

Returns the populated Gollum::File.



72
73
74
75
76
# File 'lib/gollum-lib/file.rb', line 72

def populate(blob, path=nil)
  @blob = blob
  @path = "#{path}/#{blob.name}"[1..-1]
  self
end

#raw_dataObject

Public: The raw contents of the page.

Returns the String data.



44
45
46
47
48
49
50
51
52
53
# File 'lib/gollum-lib/file.rb', line 44

def raw_data
  return nil unless @blob

  if !@wiki.repo.bare && @blob.is_symlink
    new_path = @blob.symlink_target(::File.join(@wiki.repo.path, '..', self.path))
    return IO.read(new_path) if new_path
  end

  @blob.data
end

#url_pathObject

Public: The url path required to reach this page within the repo.

Returns the String url_path



20
21
22
23
24
# File 'lib/gollum-lib/file.rb', line 20

def url_path
  path = self.path
  path = path.sub(/\/[^\/]+$/, '/') if path.include?('/')
  path
end