Class: Gollum::Wiki

Inherits:
Object
  • Object
show all
Includes:
Pagination
Defined in:
lib/gollum-lib/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:

:universal_toc - Table of contents on all pages.  Default: false
:base_path     - String base path for all Wiki links.
                 Default: "/"
:page_file_dir - String the directory in which all page files reside
:ref - String the repository ref to retrieve pages from
:mathjax       - Set to false to disable mathjax.
:user_icons    - Enable user icons on the history page. [gravatar, identicon, none].
                 Default: none
:global_tag_lookup        - Enable 4.x compatibility behavior for links
:hyphened_tag_lookup - Spaces in tag paths are treated as dashes (-)
:case_insensitive_tag_lookup - Paths in tags are compared case_insensitively
:css           - Include the custom.css file from the repo.
:emoji         - Parse and interpret emoji tags (e.g. :heart:).
:h1_title      - Concatenate all h1's on a page to form the
                 page title.
:display_metadata - Whether or not to render a page's metadata on the page. Default: true
:index_page    - The default page to retrieve or create if the
                 a directory is accessed.
:bar_side      - Where the sidebar should be displayed, may be:
                  - :left
                  - :right
:allow_editing - Set whether wiki content can be edited. Default: true
:allow_uploads - Set to true to allow file uploads.
:per_page_uploads - Whether uploads should be stored in a central
                 'uploads' directory, or in a directory named for
                 the page they were uploaded to.
:filter_chain  - Override the default filter chain with your own.

Returns a fresh Gollum::Repo.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gollum-lib/wiki.rb', line 125

def initialize(path, options = {})
  options = self.class.default_options.merge(options)
  if path.is_a?(GitAccess)
    options[:access] = path
    path             = path.path
  end

  @path                 = path
  @repo_is_bare         = options.fetch :repo_is_bare, nil
  @page_file_dir        = options.fetch :page_file_dir, nil
  @page_file_dir        = Pathname.new("/#{@page_file_dir}").cleanpath.to_s[1..-1] if @page_file_dir
  @access               = options.fetch :access, GitAccess.new(path, @page_file_dir, @repo_is_bare)
  @base_path            = options.fetch :base_path, "/"
  @repo                 = @access.repo
  @ref                  = options.fetch :ref, self.class.find_default_ref(@repo)
  @universal_toc        = options.fetch :universal_toc, false
  @mathjax              = options.fetch :mathjax, false
  @global_tag_lookup    = options.fetch :global_tag_lookup, false
  @hyphened_tag_lookup  = options.fetch :hyphened_tag_lookup, false
  @case_insensitive_tag_lookup = options.fetch :case_insensitive_tag_lookup, false
  @css                  = options.fetch :css, false
  @emoji                = options.fetch :emoji, false
  @critic_markup        = options.fetch :critic_markup, false
  @h1_title             = options.fetch :h1_title, false
  @display_metadata     = options.fetch :display_metadata, true
  @index_page           = options.fetch :index_page, 'Home'
  @bar_side             = options.fetch :sidebar, :right
  @user_icons           = ['gravatar', 'identicon'].include?(options[:user_icons]) ?
      options[:user_icons] : 'none'
  @allow_editing        = options.fetch :allow_editing, true
  @allow_uploads        = options.fetch :allow_uploads, false
  @per_page_uploads     = options.fetch :per_page_uploads, false
  @metadata             = options.fetch :metadata, {}
  @filter_chain         = options.fetch :filter_chain,
                                        [:YAML, :BibTeX, :PlainText, :CriticMarkup, :TOC, :Sanitize, :RemoteCode, :Code, :Macro, :Emoji, :PlantUML, :Tags, :PandocBib, :Render]
  @filter_chain.delete(:Emoji) unless options.fetch :emoji, false
  @filter_chain.delete(:PandocBib) unless ::Gollum::MarkupRegisterUtils.using_pandoc?
  @filter_chain.delete(:CriticMarkup) unless options.fetch :critic_markup, false

  Hook.execute(:post_wiki_initialize, self)
end

Class Attribute Details

.default_committer_emailObject



34
35
36
# File 'lib/gollum-lib/wiki.rb', line 34

def default_committer_email
  @default_committer_email || '[email protected]'
end

.default_committer_nameObject



30
31
32
# File 'lib/gollum-lib/wiki.rb', line 30

def default_committer_name
  @default_committer_name || 'Anonymous'
end

.default_optionsObject



38
39
40
# File 'lib/gollum-lib/wiki.rb', line 38

def default_options
  @default_options || {}
end

.default_refsObject



26
27
28
# File 'lib/gollum-lib/wiki.rb', line 26

def default_refs
  @default_refs || ['master', 'main']
end

Instance Attribute Details

#allow_editingObject (readonly)

Whether or not content is editable. Defaults to true



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

def allow_editing
  @allow_editing
end

#allow_uploadsObject (readonly)

Toggles file upload functionality.



615
616
617
# File 'lib/gollum-lib/wiki.rb', line 615

def allow_uploads
  @allow_uploads
end

#bar_sideObject (readonly)

Gets side on which the sidebar should be shown



80
81
82
# File 'lib/gollum-lib/wiki.rb', line 80

def bar_side
  @bar_side
end

#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 “/”.



52
53
54
# File 'lib/gollum-lib/wiki.rb', line 52

def base_path
  @base_path
end

#case_insensitive_tag_lookupObject (readonly)

Enable 4.x compatibility for case-case_insensitive links



609
610
611
# File 'lib/gollum-lib/wiki.rb', line 609

def case_insensitive_tag_lookup
  @case_insensitive_tag_lookup
end

#cssObject (readonly)

Injects custom css from custom.css in root repo. Defaults to false



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

def css
  @css
end

#display_metadataObject (readonly)

Whether or not to render a page’s metadata on the page Defaults to true



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

def 
  @display_metadata
end

#filter_chainObject (readonly)

An array of symbols which refer to classes under Gollum::Filter, each of which is an element in the “filtering chain”. See the documentation for Gollum::Filter for more on how this chain works, and what filter classes need to implement.



86
87
88
# File 'lib/gollum-lib/wiki.rb', line 86

def filter_chain
  @filter_chain
end

#global_tag_lookupObject (readonly)

Enable 4.x compatibility behavior for links



606
607
608
# File 'lib/gollum-lib/wiki.rb', line 606

def global_tag_lookup
  @global_tag_lookup
end

#h1_titleObject (readonly)

Sets page title to value of first h1 Defaults to false



70
71
72
# File 'lib/gollum-lib/wiki.rb', line 70

def h1_title
  @h1_title
end

#hyphened_tag_lookupObject (readonly)

Spaces in tag paths are treated as dashes (-)



612
613
614
# File 'lib/gollum-lib/wiki.rb', line 612

def hyphened_tag_lookup
  @hyphened_tag_lookup
end

#index_pageObject (readonly)

Gets the custom index page for / and subdirs (e.g. foo/)



77
78
79
# File 'lib/gollum-lib/wiki.rb', line 77

def index_page
  @index_page
end

#mathjaxObject (readonly)

Toggles mathjax.



596
597
598
# File 'lib/gollum-lib/wiki.rb', line 596

def mathjax
  @mathjax
end

#metadataObject (readonly)

Global metadata to be merged into the metadata for each page



89
90
91
# File 'lib/gollum-lib/wiki.rb', line 89

def 
  @metadata
end

#page_file_dirObject (readonly)

Gets the String directory in which all page files reside.



58
59
60
# File 'lib/gollum-lib/wiki.rb', line 58

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.



47
48
49
# File 'lib/gollum-lib/wiki.rb', line 47

def path
  @path
end

#per_page_uploadsObject (readonly)

Toggles whether uploaded files go into ‘uploads’, or a directory named after the page they were uploaded to.



619
620
621
# File 'lib/gollum-lib/wiki.rb', line 619

def per_page_uploads
  @per_page_uploads
end

#refObject (readonly)

Gets the String ref in which all page files reside.



55
56
57
# File 'lib/gollum-lib/wiki.rb', line 55

def ref
  @ref
end

#repoObject (readonly)

The Gollum::Git::Repo associated with the wiki.

Returns the Gollum::Git::Repo.



585
586
587
# File 'lib/gollum-lib/wiki.rb', line 585

def repo
  @repo
end

#repo_is_bareObject (readonly)

Whether or not the wiki’s repository is bare (doesn’t have a working directory)



44
45
46
# File 'lib/gollum-lib/wiki.rb', line 44

def repo_is_bare
  @repo_is_bare
end

#show_allObject (readonly)

Toggles showing all files in files view. Default is false. When false, only valid pages in the git repo are displayed.



603
604
605
# File 'lib/gollum-lib/wiki.rb', line 603

def show_all
  @show_all
end

#universal_tocObject (readonly)

Toggles display of universal table of contents



593
594
595
# File 'lib/gollum-lib/wiki.rb', line 593

def universal_toc
  @universal_toc
end

#user_iconsObject (readonly)

Toggles user icons. Default: ‘none’



599
600
601
# File 'lib/gollum-lib/wiki.rb', line 599

def user_icons
  @user_icons
end

Class Method Details

.find_default_ref(repo) ⇒ Object



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

def find_default_ref(repo)
  repo.find_branch(self.default_refs) || Gollum::Git.global_default_branch || self.default_refs.first
end

Instance Method Details

#add_redirect(old_path, new_path, commit = nil) ⇒ Object



566
567
568
569
# File 'lib/gollum-lib/wiki.rb', line 566

def add_redirect(old_path, new_path, commit=nil)
  redirects[old_path] = new_path
  redirects.dump(commit)
end

#clear_cacheObject

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

Returns nothing.



553
554
555
# File 'lib/gollum-lib/wiki.rb', line 553

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 Gollum::Git::Commit instance.



683
684
685
686
# File 'lib/gollum-lib/wiki.rb', line 683

def commit_for(ref)
  @access.commit(ref)
rescue Gollum::Git::NoSuchShaFound
end

#default_committer_emailObject

Gets the default email for commits.

Returns the String email address.



672
673
674
675
676
# File 'lib/gollum-lib/wiki.rb', line 672

def default_committer_email
  email = @repo.config['user.email']
  email = email.delete('<>') if email
  @default_committer_email ||= email || self.class.default_committer_email
end

#default_committer_nameObject

Gets the default name for commits.

Returns the String name.



664
665
666
667
# File 'lib/gollum-lib/wiki.rb', line 664

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

#delete_file(path, commit) ⇒ Object

Public: Delete a file.

path - The path to the file to delete commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/gollum-lib/wiki.rb', line 409

def delete_file(path, commit)
  fullpath     = ::File.join([page_file_dir, path].compact)
  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  committer.delete(fullpath)

  committer.after_commit do |index, _sha|
    dir = '' if dir == '.'
    @access.refresh
    index.update_working_dir(fullpath)
  end

  multi_commit ? committer : committer.commit
end

#delete_page(page, commit) ⇒ Object

Public: Delete a page.

page - The Gollum::Page to delete. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



389
390
391
# File 'lib/gollum-lib/wiki.rb', line 389

def delete_page(page, commit)
  delete_file(page.url_path, commit)
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)


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

def exist?
  @access.exist?
end

#file(name, version = nil, try_on_disk = false) ⇒ 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). try_on_disk - If true, try to return just a reference to a file

that exists on the disk.

Returns a Gollum::File or nil if no matching file was 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.



195
196
197
# File 'lib/gollum-lib/wiki.rb', line 195

def file(name, version = nil, try_on_disk = false)
  ::Gollum::File.find(self, name, version.nil? ? @ref : version, try_on_disk)
end

#files(treeish = nil) ⇒ Object

Public: Lists all non-page files for this wiki.

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

Returns an Array of Gollum::File instances.



480
481
482
# File 'lib/gollum-lib/wiki.rb', line 480

def files(treeish = nil)
  tree_list(treeish || @ref, false, true)
end

#inspectObject



706
707
708
# File 'lib/gollum-lib/wiki.rb', line 706

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

#latest_changes(options = {}) ⇒ Object

Returns the latest changes in the wiki (globally)

options - The options Hash:

:max_count  - The Integer number of items to return.

Returns an Array of Gollum::Git::Commit.



544
545
546
547
# File 'lib/gollum-lib/wiki.rb', line 544

def latest_changes(options={})
  options[:max_count] = 10 unless options[:max_count]
  @repo.log(@ref, page_file_dir, options)
end

#log(options = {}) ⇒ Object

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

options - The options Hash:

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

Returns an Array of Gollum::Git::Commit.



534
535
536
# File 'lib/gollum-lib/wiki.rb', line 534

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.



626
627
628
# File 'lib/gollum-lib/wiki.rb', line 626

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

#overwrite_file(name, data, commit = {}) ⇒ Object

Public: Write a file to the Gollum repo regardless of existing versions.

path - The String path where the file will be written. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update



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

def overwrite_file(name, data, commit = {})
  write(merge_path_elements(nil, name, nil), data, commit, force_overwrite = true)
end

#page(path, version = nil, global_match = false) ⇒ Object

Public: Get the formatted page for a given page name, version, and dir.

path - The String path to the the wiki page (may or may not include file extension). version - The String version ID to find (default: @ref). global_match - If true, find a File matching path’s filename, but not it’s directory (so anywhere in the repo)

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



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

def page(path, version = nil, global_match = false)
  ::Gollum::Page.find(self, path, version.nil? ? @ref : version, false, global_match)
end

#page_file_name(name, format) ⇒ Object

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

name - The String name of the page (should be pre-canonicalized). format - The Symbol format of the page.

Returns the String filename.



636
637
638
# File 'lib/gollum-lib/wiki.rb', line 636

def page_file_name(name, format)
  format.nil? ? name : "#{name}.#{::Gollum::Page.format_to_ext(format)}"
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.



471
472
473
# File 'lib/gollum-lib/wiki.rb', line 471

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

#preview_page(name, data, format) ⇒ Object

Public: Create an in-memory Page with the given data and format. This is useful for previewing what content will look like before committing it to the repository.

name - The String name of the page. format - The Symbol format of the page. data - The new String contents of the page.

Returns the in-memory Gollum::Page.



208
209
210
# File 'lib/gollum-lib/wiki.rb', line 208

def preview_page(name, data, format)
  ::Gollum::PreviewPage.new(self, "#{name}.#{::Gollum::Page.format_to_ext(format.to_sym)}", data, @access.commit(@ref))
end

#redirectsObject



557
558
559
560
561
562
563
564
# File 'lib/gollum-lib/wiki.rb', line 557

def redirects
  if @redirects.nil? || @redirects.stale?
    @redirects = {}.extend(::Gollum::Redirects)
    @redirects.init(self)
    @redirects.load
  end
  @redirects
end

#remove_redirect(path, commit = nil) ⇒ Object



571
572
573
574
# File 'lib/gollum-lib/wiki.rb', line 571

def remove_redirect(path, commit=nil)
  redirects.tap{|k| k.delete(path)}
  redirects.dump(commit)
end

#rename_page(page, rename, commit = {}) ⇒ Object

Public: Rename an existing page without altering content.

page - The Gollum::Page to update. rename - The String extension-less full path of the page (leading ‘/’ is ignored). commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update. Returns false if the operation is a NOOP.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/gollum-lib/wiki.rb', line 291

def rename_page(page, rename, commit = {})
  return false if page.nil?
  return false if rename.nil? or rename.empty?

  (target_dir, target_name) = ::File.split(rename)
  (source_dir, source_name) = ::File.split(page.path)
  source_name               = page.filename_stripped

  # File.split gives us relative paths with ".", commiter.add_to_index doesn't like that.
  target_dir                = '' if target_dir == '.'
  source_dir                = '' if source_dir == '.'
  target_dir                = target_dir.gsub(/^\//, '')

  # if the rename is a NOOP, abort
  if source_dir == target_dir and source_name == target_name
    return false
  end

  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  committer.delete(page.path)
  committer.add_to_index(merge_path_elements(target_dir, target_name, page.format), page.raw_data)

  committer.after_commit do |index, _sha|
    @access.refresh
    index.update_working_dir(merge_path_elements(source_dir, source_name, page.format))
    index.update_working_dir(merge_path_elements(target_dir, target_name, page.format))
  end

  multi_commit ? committer : committer.commit
end

#revert_commit(sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff to the repo. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

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

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



460
461
462
463
464
# File 'lib/gollum-lib/wiki.rb', line 460

def revert_commit(sha1, sha2 = nil, commit = {})
  left, right, options = parse_revert_options(sha1, sha2, commit)
  tree, files = repo.git.revert_commit(left, right)
  commit_and_update_paths(tree, files, options)
end

#revert_page(page, sha1, sha2 = nil, commit = {}) ⇒ Object

Public: Applies a reverse diff for a given page. If only 1 SHA is given, the reverse diff will be taken from its parent (^SHA…SHA). If two SHAs are given, the reverse diff is taken from SHA1…SHA2.

page - The Gollum::Page to delete. sha1 - String SHA1 of the earlier parent if two SHAs are given,

or the child.

sha2 - Optional String SHA1 of the child. commit - The commit Hash details:

:message - The String commit message.
:name    - The String author full name.
:email   - The String email address.
:parent  - Optional Gollum::Git::Commit parent to this update.

Returns a String SHA1 of the new commit, or nil if the reverse diff does not apply.



440
441
442
443
444
# File 'lib/gollum-lib/wiki.rb', line 440

def revert_page(page, sha1, sha2 = nil, commit = {})
  return false unless page
  left, right, options = parse_revert_options(sha1, sha2, commit)
  commit_and_update_paths(@repo.git.revert_path(page.path, left, right), [page.path], options)
end

#sanitizerObject

Public: Creates a Sanitize instance

Returns a Sanitize instance.



713
714
715
# File 'lib/gollum-lib/wiki.rb', line 713

def sanitizer
  @sanitizer ||= Gollum::Sanitization.new(Gollum::Markup.to_xml_opts)
end

#search(query) ⇒ Object

Public: Search all pages for this wiki.

query - The string to search for

Returns an Array with Objects of page name and count of matches



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/gollum-lib/wiki.rb', line 502

def search(query)
  options = {:path => page_file_dir, :ref => ref}
  search_terms = query.scan(/"([^"]+)"|(\S+)/).flatten.compact.map {|term| Regexp.escape(term)}
  search_terms_regex = search_terms.join('|')
  query = /^(.*(?:#{search_terms_regex}).*)$/i
  results = @repo.git.grep(search_terms, options) do |name, data|
    result = {:count => 0}
    result[:name] = extract_page_file_dir(name)
    result[:filename_count] = result[:name].scan(/#{search_terms_regex}/i).size
    result[:context] = []
    if data
      begin
        data.scan(query) do |match|
          result[:context] << match.first
          result[:count] += match.first.scan(/#{search_terms_regex}/i).size
        end
      rescue ArgumentError # https://github.com/gollum/gollum/issues/1491
        next
      end
    end
    ((result[:count] + result[:filename_count]) == 0) ? nil : result
  end
  [results, search_terms]
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



489
490
491
492
493
494
495
# File 'lib/gollum-lib/wiki.rb', line 489

def size(ref = nil)
  tree_map_for(ref || @ref).inject(0) do |num, entry|
    num + (::Gollum::Page.valid_page_name?(entry.name) ? 1 : 0)
  end
rescue Gollum::Git::NoSuchShaFound
  0
end

#tree_list(ref = @ref, pages = true, files = true) ⇒ Object

Fill an array with a list of pages and files in the wiki.

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

Returns a flat Array of Gollum::Page and Gollum::File instances.



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/gollum-lib/wiki.rb', line 645

def tree_list(ref = @ref, pages=true, files=true)
  if (sha = @access.ref_to_sha(ref))
    commit = @access.commit(sha)
    tree_map_for(sha).inject([]) do |list, entry|
      if ::Gollum::Page.valid_page_name?(entry.name)
        list << entry.page(self, commit) if pages
      elsif files && !entry.name.start_with?('_') && !::Gollum::Page.protected_files.include?(entry.name)
        list << entry.file(self, commit)
      end
      list
    end
  else
    []
  end
end

#tree_map_for(ref, ignore_page_file_dir = false) ⇒ 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. ignore_page_file_dir - Boolean, if true, searches all files within the git repo, regardless of dir/subdir

Returns an Array of BlobEntry instances.



695
696
697
698
699
700
701
702
703
704
# File 'lib/gollum-lib/wiki.rb', line 695

def tree_map_for(ref, ignore_page_file_dir = false)
  if ignore_page_file_dir && !@page_file_dir.nil?
    @root_access ||= GitAccess.new(path, nil, @repo_is_bare)
    @root_access.tree(ref)
  else
    @access.tree(ref)
  end
rescue Gollum::Git::NoSuchShaFound
  []
end

#update_page(page, name, format, data, commit = {}) ⇒ Object

Public: Update an existing page with new content. The location of the page inside the repository will not change. If the given format is different than the current format of the page, the filename will be changed to reflect the new format.

page - The Gollum::Page to update. name - The String extension-less name of the page. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/gollum-lib/wiki.rb', line 346

def update_page(page, name, format, data, commit = {})
  name     ||= page.name
  format   ||= page.format
  dir      = ::File.dirname(page.path)
  dir      = nil if dir == '.'
  rename   = (page.name != name || page.format != format)
  new_path = ::File.join([dir, self.page_file_name(name, format)].compact) if rename

  multi_commit = !!commit[:committer]
  committer    = multi_commit ? commit[:committer] : Committer.new(self, commit)

  if !rename
    committer.add(page.path, normalize(data))
  else
    committer.delete(page.path)
    committer.add_to_index(new_path, data)
  end

  committer.after_commit do |index, _sha|
    @access.refresh
    index.update_working_dir(page.path)
    index.update_working_dir(new_path) if rename
  end

  multi_commit ? committer : committer.commit
end

#write_file(name, data, commit = {}) ⇒ Object

Public: Write a new version of a file to the Gollum repo.

path - The String path where the file will be written. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update



249
250
251
# File 'lib/gollum-lib/wiki.rb', line 249

def write_file(name, data, commit = {})
  write(merge_path_elements(nil, name, nil), data, commit)
end

#write_page(path, format, data, commit = {}) ⇒ Object

Public: Write a new version of a page to the Gollum repo root.

path - The String path where the page will be written. format - The Symbol format of the page. data - The new String contents of the page. commit - The commit Hash details:

:message   - The String commit message.
:name      - The String author full name.
:email     - The String email address.
:parent    - Optional Gollum::Git::Commit parent to this update.
:tree      - Optional String SHA of the tree to create the
             index from.
:committer - Optional Gollum::Committer instance.  If provided,
             assume that this operation is part of batch of
             updates and the commit happens later.

Returns the String SHA1 of the newly written version, or the Gollum::Committer instance if this is part of a batch update.



229
230
231
# File 'lib/gollum-lib/wiki.rb', line 229

def write_page(path, format, data, commit = {})
 write(merge_path_elements(nil, path, format), data, commit)
end