Class: Gollum::Wiki

Inherits:
Object
  • Object
show all
Includes:
Pagination
Defined in:
lib/gollum/wiki.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pagination

included, #log_pagination_options, #page_to_skip

Constructor Details

#initialize(path, options = {}) ⇒ Wiki

Public: Initialize a new Gollum Repo.

path - The String path to the Git repository that holds the Gollum

site.

options - Optional Hash:

:base_path     - String base path for all Wiki links.
                 Default: "/"
:page_class    - The page Class. Default: Gollum::Page
:file_class    - The file Class. Default: Gollum::File
:markup_classes - A hash containing the markup Classes for each
                  document type. Default: { Gollum::Markup }
:sanitization  - An instance of Sanitization.
:page_file_dir - String the directory in which all page files reside
:ref - String the repository ref to retrieve pages from

Returns a fresh Gollum::Repo.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gollum/wiki.rb', line 139

def initialize(path, options = {})
  if path.is_a?(GitAccess)
    options[:access] = path
    path             = path.path
  end
  @path          = path
  @page_file_dir = options[:page_file_dir]
  @access        = options[:access]       || GitAccess.new(path, @page_file_dir)
  @base_path     = options[:base_path]    || "/"
  @page_class    = options[:page_class]   || self.class.page_class
  @file_class    = options[:file_class]   || self.class.file_class
  @markup_classes = options[:markup_classes] || self.class.markup_classes
  @repo          = @access.repo
  @ref           = options[:ref] || self.class.default_ref
  @sanitization  = options[:sanitization] || self.class.sanitization
  @history_sanitization = options[:history_sanitization] ||
    self.class.history_sanitization
end

Class Attribute Details

.default_committer_emailObject

Sets the default email for commits.



22
23
24
# File 'lib/gollum/wiki.rb', line 22

def default_committer_email
  @default_committer_email
end

.default_committer_nameObject

Sets the default name for commits.



19
20
21
# File 'lib/gollum/wiki.rb', line 19

def default_committer_name
  @default_committer_name
end

.default_refObject

Sets the default ref for the wiki.



16
17
18
# File 'lib/gollum/wiki.rb', line 16

def default_ref
  @default_ref
end

.file_classObject

Gets the file class used by all instances of this Wiki. Default: Gollum::File.



45
46
47
48
49
50
51
52
# File 'lib/gollum/wiki.rb', line 45

def file_class
  @file_class ||
    if superclass.respond_to?(:file_class)
      superclass.file_class
    else
      ::Gollum::File
    end
end

.history_sanitizationObject

Gets the default sanitization options for older page revisions used by instances of this Wiki.



92
93
94
95
96
97
98
99
# File 'lib/gollum/wiki.rb', line 92

def history_sanitization
  if @history_sanitization.nil?
    @history_sanitization = sanitization ?
      sanitization.history_sanitization  :
      false
  end
  @history_sanitization
end

.markup_classesObject

Gets the markup class used by all instances of this Wiki. Default: Gollum::Markup



56
57
58
59
60
61
62
63
# File 'lib/gollum/wiki.rb', line 56

def markup_classes
  @markup_classes ||=
    if superclass.respond_to?(:markup_classes)
      superclass.markup_classes
    else
      Hash.new(::Gollum::Markup)
    end
end

.page_classObject

Gets the page class used by all instances of this Wiki. Default: Gollum::Page.



34
35
36
37
38
39
40
41
# File 'lib/gollum/wiki.rb', line 34

def page_class
  @page_class ||
    if superclass.respond_to?(:page_class)
      superclass.page_class
    else
      ::Gollum::Page
    end
end

.sanitizationObject

Gets the default sanitization options for current pages used by instances of this Wiki.



83
84
85
86
87
88
# File 'lib/gollum/wiki.rb', line 83

def sanitization
  if @sanitization.nil?
    @sanitization = Sanitization.new
  end
  @sanitization
end

Instance Attribute Details

#base_pathObject (readonly)

The String base path to prefix to internal links. For example, when set to “/wiki”, the page “Hobbit” will be linked as “/wiki/Hobbit”. Defaults to “/”.



109
110
111
# File 'lib/gollum/wiki.rb', line 109

def base_path
  @base_path
end

#file_classObject (readonly)

Gets the file class used by all instances of this Wiki.



266
267
268
# File 'lib/gollum/wiki.rb', line 266

def file_class
  @file_class
end

#history_sanitizationObject (readonly)

Gets the sanitization options for older page revisions used by this Wiki.



115
116
117
# File 'lib/gollum/wiki.rb', line 115

def history_sanitization
  @history_sanitization
end

#markup_classesObject (readonly)

Gets the markup class used by all instances of this Wiki.



269
270
271
# File 'lib/gollum/wiki.rb', line 269

def markup_classes
  @markup_classes
end

#page_classObject (readonly)

Gets the page class used by all instances of this Wiki.



263
264
265
# File 'lib/gollum/wiki.rb', line 263

def page_class
  @page_class
end

#page_file_dirObject (readonly)

Gets the String directory in which all page files reside.



121
122
123
# File 'lib/gollum/wiki.rb', line 121

def page_file_dir
  @page_file_dir
end

#pathObject (readonly)

The String path to the Git repository that holds the Gollum site.

Returns the String path.



260
261
262
# File 'lib/gollum/wiki.rb', line 260

def path
  @path
end

#refObject (readonly)

Gets the String ref in which all page files reside.



118
119
120
# File 'lib/gollum/wiki.rb', line 118

def ref
  @ref
end

#repoObject (readonly)

The Grit::Repo associated with the wiki.

Returns the Grit::Repo.



255
256
257
# File 'lib/gollum/wiki.rb', line 255

def repo
  @repo
end

#sanitizationObject (readonly)

Gets the sanitization options for current pages used by this Wiki.



112
113
114
# File 'lib/gollum/wiki.rb', line 112

def sanitization
  @sanitization
end

Class Method Details

.markup_class(language = :default) ⇒ Object Also known as: default_markup_class

Gets the default markup class used by all instances of this Wiki. Kept for backwards compatibility until Gollum v2.x



67
68
69
# File 'lib/gollum/wiki.rb', line 67

def markup_class(language=:default)
  markup_classes[language]
end

.markup_class=(default) ⇒ Object Also known as: default_markup_class=

Sets the default markup class used by all instances of this Wiki. Kept for backwards compatibility until Gollum v2.x



73
74
75
76
# File 'lib/gollum/wiki.rb', line 73

def markup_class=(default)
  @markup_classes = Hash.new(default).update(markup_classes)
  default
end

Instance Method Details

#clear_cacheObject

Public: Refreshes just the cached Git reference data. This should be called after every Gollum update.

Returns nothing.



222
223
224
# File 'lib/gollum/wiki.rb', line 222

def clear_cache
  @access.refresh
end

#commit_for(ref) ⇒ Object

Gets the commit object for the given ref or sha.

ref - A string ref or SHA pointing to a valid commit.

Returns a Grit::Commit instance.



357
358
359
360
# File 'lib/gollum/wiki.rb', line 357

def commit_for(ref)
  @access.commit(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
end

#default_committer_emailObject

Gets the default email for commits.

Returns the String email address.



347
348
349
350
# File 'lib/gollum/wiki.rb', line 347

def default_committer_email
  @default_committer_email ||= \
    @repo.config['user.email'] || self.class.default_committer_email
end

#default_committer_nameObject

Gets the default name for commits.

Returns the String name.



339
340
341
342
# File 'lib/gollum/wiki.rb', line 339

def default_committer_name
  @default_committer_name ||= \
    @repo.config['user.name'] || self.class.default_committer_name
end

#exist?Boolean

Public: check whether the wiki’s git repo exists on the filesystem.

Returns true if the repo exists, and false if it does not.

Returns:

  • (Boolean)


161
162
163
# File 'lib/gollum/wiki.rb', line 161

def exist?
  @access.exist?
end

#file(name, version = @ref) ⇒ Object

Public: Get the static file for a given name.

name - The full String pathname to the file. version - The String version ID to find (default: @ref).

Returns a Gollum::File or nil if no matching file was found.



181
182
183
# File 'lib/gollum/wiki.rb', line 181

def file(name, version = @ref)
  @file_class.new(self).find(name, version)
end

#full_reverse_diff(sha1, sha2 = nil) ⇒ Object

Creates a reverse diff for the given SHAs.

sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child.

Returns a String of the reverse Diff to apply.



332
333
334
# File 'lib/gollum/wiki.rb', line 332

def full_reverse_diff(sha1, sha2 = nil)
  full_reverse_diff_for(nil, sha1, sha2)
end

#full_reverse_diff_for(page, sha1, sha2 = nil) ⇒ Object

Creates a reverse diff for the given SHAs on the given Gollum::Page.

page - The Gollum::Page to scope the patch to, or a String Path. sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child.

Returns a String of the reverse Diff to apply.



316
317
318
319
320
321
322
323
# File 'lib/gollum/wiki.rb', line 316

def full_reverse_diff_for(page, sha1, sha2 = nil)
  sha1, sha2 = "#{sha1}^", sha1 if sha2.nil?
  args = [{:R => true}, sha1, sha2]
  if page
    args << '--' << (page.respond_to?(:path) ? page.path : page.to_s)
  end
  repo.git.native(:diff, *args)
end

#history_sanitizerObject

Public: Creates a Sanitize instance using the Wiki’s history sanitization options.

Returns a Sanitize instance.



240
241
242
243
244
# File 'lib/gollum/wiki.rb', line 240

def history_sanitizer
  if options = history_sanitization
    @history_sanitizer ||= options.to_sanitize
  end
end

#inspectObject



374
375
376
# File 'lib/gollum/wiki.rb', line 374

def inspect
  %(#<#{self.class.name}:#{object_id} #{@repo.path}>)
end

#log(options = {}) ⇒ Object

Public: All of the versions that have touched the Page.

options - The options Hash:

:page     - The Integer page number (default: 1).
:per_page - The Integer max count of items to return.

Returns an Array of Grit::Commit.



214
215
216
# File 'lib/gollum/wiki.rb', line 214

def log(options = {})
  @repo.log(@ref, nil, log_pagination_options(options))
end

#normalize(data) ⇒ Object

Normalize the data.

data - The String data to be normalized.

Returns the normalized data String.



276
277
278
# File 'lib/gollum/wiki.rb', line 276

def normalize(data)
  data.gsub(/\r/, '')
end

#page(name, version = @ref) ⇒ Object

Public: Get the formatted page for a given page name.

name - The human or canonical String page name of the wiki page. version - The String version ID to find (default: @ref).

Returns a Gollum::Page or nil if no matching page was found.



171
172
173
# File 'lib/gollum/wiki.rb', line 171

def page(name, version = @ref)
  @page_class.new(self).find(name, version)
end

#page_file_name(name, format) ⇒ Object

Assemble a Page’s filename from its name and format.

name - The String name of the page (may be in human format). format - The Symbol format of the page.

Returns the String filename.



286
287
288
289
# File 'lib/gollum/wiki.rb', line 286

def page_file_name(name, format)
  ext = @page_class.format_to_ext(format)
  @page_class.cname(name) + '.' + ext
end

#pages(treeish = nil) ⇒ Object

Public: Lists all pages for this wiki.

treeish - The String commit ID or ref to find (default: @ref)

Returns an Array of Gollum::Page instances.



190
191
192
# File 'lib/gollum/wiki.rb', line 190

def pages(treeish = nil)
  tree_list(treeish || @ref)
end

#sanitizerObject

Public: Creates a Sanitize instance using the Wiki’s sanitization options.

Returns a Sanitize instance.



230
231
232
233
234
# File 'lib/gollum/wiki.rb', line 230

def sanitizer
  if options = sanitization
    @sanitizer ||= options.to_sanitize
  end
end

#size(ref = nil) ⇒ Object

Public: Returns the number of pages accessible from a commit

ref - A String ref that is either a commit SHA or references one.

Returns a Fixnum



199
200
201
202
203
204
205
# File 'lib/gollum/wiki.rb', line 199

def size(ref = nil)
  tree_map_for(ref || @ref).inject(0) do |num, entry|
    num + (@page_class.valid_page_name?(entry.name) ? 1 : 0)
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
  0
end

#tree_list(ref) ⇒ Object

Fill an array with a list of pages.

ref - A String ref that is either a commit SHA or references one.

Returns a flat Array of Gollum::Page instances.



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/gollum/wiki.rb', line 296

def tree_list(ref)
  if sha = @access.ref_to_sha(ref)
    commit = @access.commit(sha)
    tree_map_for(sha).inject([]) do |list, entry|
      next list unless @page_class.valid_page_name?(entry.name)
      list << entry.page(self, commit)
    end
  else
    []
  end
end

#tree_map_for(ref) ⇒ Object

Finds a full listing of files and their blob SHA for a given ref. Each listing is cached based on its actual commit SHA.

ref - A String ref that is either a commit SHA or references one.

Returns an Array of BlobEntry instances.



368
369
370
371
372
# File 'lib/gollum/wiki.rb', line 368

def tree_map_for(ref)
  @access.tree(ref)
rescue Grit::GitRuby::Repository::NoSuchShaFound
  []
end