Class: Gollum::File

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

Direct Known Subclasses

Page

Constant Summary collapse

!Gem.win_platform?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki, blob, path, version, try_on_disk = false) ⇒ File

Public: Initialize a file.

wiki - The Gollum::Wiki blob - The Gollum::Git::Blob path - The String path version - The String SHA or Gollum::Git::Commit version try_on_disk - If true, try to get an on disk reference for this file.

Returns a newly initialized Gollum::File.



74
75
76
77
78
79
80
# File 'lib/gollum-lib/file.rb', line 74

def initialize(wiki, blob, path, version, try_on_disk = false)
  @wiki         = wiki
  @blob         = blob
  @path         = "#{path}/#{blob.name}"[1..-1]
  @version      = version.is_a?(Gollum::Git::Commit) ? version : @wiki.commit_for(version)
  get_disk_reference if try_on_disk
end

Instance Attribute Details

#on_diskObject

Public: Whether the file can be read from disk.



91
92
93
# File 'lib/gollum-lib/file.rb', line 91

def on_disk
  @on_disk
end

#pathObject (readonly)

Public: The path of the page within the repo.

Returns the String path.



85
86
87
# File 'lib/gollum-lib/file.rb', line 85

def path
  @path
end

#versionObject

Public: The Gollum::Git::Commit version of the file.



88
89
90
# File 'lib/gollum-lib/file.rb', line 88

def version
  @version
end

Class Method Details

.find(wiki, path, version, try_on_disk = false, global_match = false) ⇒ Object

Find a file in the given Gollum wiki.

wiki - The wiki. path - The full String path. version - The String version ID to find. try_on_disk - If true, try to return just a reference to a file

that exists on the disk.

global_match - If true, find a File matching path’s filename, but not it’s directory (so anywhere in the repo)

Returns a Gollum::File or nil if the file could not be found. Note that if you specify try_on_disk=true, you may or may not get a file for which on_disk? is actually true.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gollum-lib/file.rb', line 49

def self.find(wiki, path, version, try_on_disk = false, global_match = false)
  map = wiki.tree_map_for(version.to_s)

  query_path = Pathname.new(::File.join(['/', wiki.page_file_dir, path].compact)).cleanpath.to_s
  query_path.sub!(/^\/\//, '/') if Gem.win_platform? # On Windows, Pathname#cleanpath will leave double slashes at the start of a path intact, so sub them out.

  begin
    entry = map.detect do |entry|
      path_match(query_path, entry, global_match, wiki.hyphened_tag_lookup, wiki.case_insensitive_tag_lookup)
    end
    entry ? self.new(wiki, entry.blob(wiki.repo), entry.dir, version, try_on_disk) : nil
  rescue Gollum::Git::NoSuchShaFound
    nil
  end
end

.path_compare(query, match_path, hyphened_tags, case_insensitive) ⇒ Object

For use with self.path_match: returns true if ‘query’ and ‘match_path’ match, strictly or taking account of the following parameters: hyphened_tags - If true, replace spaces in match_path with hyphens. case_insensitive - If true, compare query and match_path case-insensitively



25
26
27
28
29
30
31
32
33
34
# File 'lib/gollum-lib/file.rb', line 25

def path_compare(query, match_path, hyphened_tags, case_insensitive)
  if hyphened_tags
    final_query = query.gsub(' ', '-')
    final_match = match_path.gsub(' ', '-')
  else
    final_query = query
    final_match = match_path
  end
  final_match.send(case_insensitive ? :casecmp? : :==, final_query)
end

.path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false) ⇒ Object

For use with self.find: returns true if the given query corresponds to the in-repo path of the BlobEntry.

query - The String path to match. entry - The BlobEntry to check against. global_match - (Not implemented for File, see Page.path_match) hyphened_tags - If true, replace spaces in match_path with hyphens. case_insensitive - If true, compare query and match_path case-insensitively



18
19
20
# File 'lib/gollum-lib/file.rb', line 18

def path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false)
  path_compare(query, ::File.join('/', entry.path), hyphened_tags, case_insensitive)
end

.protected_filesObject



157
158
159
# File 'lib/gollum-lib/file.rb', line 157

def self.protected_files
  ['custom.css', 'custom.js', '.redirects.gollum']
end

Instance Method Details

#escaped_url_pathObject

Public: The url_path, but URL encoded.

Returns the String url_path



119
120
121
# File 'lib/gollum-lib/file.rb', line 119

def escaped_url_path
  ERB::Util.url_encode(self.url_path).gsub('%2F', '/').force_encoding('utf-8')
end

#filenameObject Also known as: name

Public: The on-disk filename of the page including extension.

Returns the String name.



103
104
105
# File 'lib/gollum-lib/file.rb', line 103

def filename
  @blob && @blob.name
end

#mime_typeObject

Public: The String mime type of the file.



153
154
155
# File 'lib/gollum-lib/file.rb', line 153

def mime_type
  @blob && @blob.mime_type
end

#on_disk?Boolean

Public: Is this an on-disk file reference?

Returns true if this is a pointer to an on-disk file

Returns:

  • (Boolean)


141
142
143
# File 'lib/gollum-lib/file.rb', line 141

def on_disk?
  !!@on_disk
end

#on_disk_pathObject

Public: The path to this file on disk

Returns nil if on_disk? is false.



148
149
150
# File 'lib/gollum-lib/file.rb', line 148

def on_disk_path
  @on_disk_path
end

#raw_dataObject

Public: The raw contents of the file.

Returns the String data.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gollum-lib/file.rb', line 126

def raw_data
  return IO.read(@on_disk_path) if on_disk?
  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

#shaObject

Public: The SHA hash identifying this page

Returns the String SHA.



96
97
98
# File 'lib/gollum-lib/file.rb', line 96

def sha
  @blob && @blob.id
end

#url_pathObject

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

Returns the String url_path



111
112
113
114
# File 'lib/gollum-lib/file.rb', line 111

def url_path
  # Chop off the page_file_dir and first slash if necessary
  @wiki.page_file_dir ? self.path[@wiki.page_file_dir.length+1..-1] : self.path
end