Class: Gollum::Wiki
- Inherits:
-
Object
- Object
- Gollum::Wiki
- Includes:
- Pagination
- Defined in:
- lib/gollum-lib/wiki.rb
Class Attribute Summary collapse
- .default_committer_email ⇒ Object
- .default_committer_name ⇒ Object
- .default_options ⇒ Object
- .default_ref ⇒ Object
Instance Attribute Summary collapse
-
#allow_editing ⇒ Object
readonly
Whether or not content is editable.
-
#allow_uploads ⇒ Object
readonly
Toggles file upload functionality.
-
#bar_side ⇒ Object
readonly
Gets side on which the sidebar should be shown.
-
#base_path ⇒ Object
readonly
The String base path to prefix to internal links.
-
#case_insensitive_tag_lookup ⇒ Object
readonly
Enable 4.x compatibility for case-case_insensitive links.
-
#css ⇒ Object
readonly
Injects custom css from custom.css in root repo.
-
#display_metadata ⇒ Object
readonly
Whether or not to render a page’s metadata on the page Defaults to true.
-
#filter_chain ⇒ Object
readonly
An array of symbols which refer to classes under Gollum::Filter, each of which is an element in the “filtering chain”.
-
#global_tag_lookup ⇒ Object
readonly
Enable 4.x compatibility behavior for links.
-
#h1_title ⇒ Object
readonly
Sets page title to value of first h1 Defaults to false.
-
#hyphened_tag_lookup ⇒ Object
readonly
Spaces in tag paths are treated as dashes (-).
-
#index_page ⇒ Object
readonly
Gets the custom index page for / and subdirs (e.g. foo/).
-
#mathjax ⇒ Object
readonly
Toggles mathjax.
-
#metadata ⇒ Object
readonly
Global metadata to be merged into the metadata for each page.
-
#page_file_dir ⇒ Object
readonly
Gets the String directory in which all page files reside.
-
#path ⇒ Object
readonly
The String path to the Git repository that holds the Gollum site.
-
#per_page_uploads ⇒ Object
readonly
Toggles whether uploaded files go into ‘uploads’, or a directory named after the page they were uploaded to.
-
#ref ⇒ Object
readonly
Gets the String ref in which all page files reside.
-
#repo ⇒ Object
readonly
The Gollum::Git::Repo associated with the wiki.
-
#repo_is_bare ⇒ Object
readonly
Whether or not the wiki’s repository is bare (doesn’t have a working directory).
-
#show_all ⇒ Object
readonly
Toggles showing all files in files view.
-
#universal_toc ⇒ Object
readonly
Toggles display of universal table of contents.
-
#user_icons ⇒ Object
readonly
Toggles user icons.
Instance Method Summary collapse
- #add_redirect(old_path, new_path, commit = nil) ⇒ Object
-
#clear_cache ⇒ Object
Public: Refreshes just the cached Git reference data.
-
#commit_for(ref) ⇒ Object
Gets the commit object for the given ref or sha.
-
#default_committer_email ⇒ Object
Gets the default email for commits.
-
#default_committer_name ⇒ Object
Gets the default name for commits.
-
#delete_file(path, commit) ⇒ Object
Public: Delete a file.
-
#delete_page(page, commit) ⇒ Object
Public: Delete a page.
-
#exist? ⇒ Boolean
Public: check whether the wiki’s git repo exists on the filesystem.
-
#file(name, version = nil, try_on_disk = false) ⇒ Object
Public: Get the static file for a given name.
-
#files(treeish = nil) ⇒ Object
Public: Lists all non-page files for this wiki.
-
#initialize(path, options = {}) ⇒ Wiki
constructor
Public: Initialize a new Gollum Repo.
- #inspect ⇒ Object
-
#latest_changes(options = {}) ⇒ Object
Returns the latest changes in the wiki (globally).
-
#log(options = {}) ⇒ Object
Public: All of the versions that have touched the Page.
-
#normalize(data) ⇒ Object
Normalize the data.
-
#overwrite_file(name, data, commit = {}) ⇒ Object
Public: Write a file to the Gollum repo regardless of existing versions.
-
#page(path, version = nil, global_match = false) ⇒ Object
Public: Get the formatted page for a given page name, version, and dir.
-
#page_file_name(name, format) ⇒ Object
Assemble a Page’s filename from its name and format.
-
#pages(treeish = nil) ⇒ Object
Public: Lists all pages for this wiki.
-
#preview_page(name, data, format) ⇒ Object
Public: Create an in-memory Page with the given data and format.
- #redirects ⇒ Object
- #remove_redirect(path, commit = nil) ⇒ Object
-
#rename_page(page, rename, commit = {}) ⇒ Object
Public: Rename an existing page without altering content.
-
#revert_commit(sha1, sha2 = nil, commit = {}) ⇒ Object
Public: Applies a reverse diff to the repo.
-
#revert_page(page, sha1, sha2 = nil, commit = {}) ⇒ Object
Public: Applies a reverse diff for a given page.
-
#sanitizer ⇒ Object
Public: Creates a Sanitize instance.
-
#search(query) ⇒ Object
Public: Search all pages for this wiki.
-
#size(ref = nil) ⇒ Object
Public: Returns the number of pages accessible from a commit.
-
#tree_list(ref = @ref, pages = true, files = true) ⇒ Object
Fill an array with a list of pages and files in the wiki.
-
#tree_map_for(ref, ignore_page_file_dir = false) ⇒ Object
Finds a full listing of files and their blob SHA for a given ref.
-
#update_page(page, name, format, data, commit = {}) ⇒ Object
Public: Update an existing page with new content.
-
#write_file(name, data, commit = {}) ⇒ Object
Public: Write a new version of a file to the Gollum repo.
-
#write_page(path, format, data, commit = {}) ⇒ Object
Public: Write a new version of a page to the Gollum repo root.
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.
121 122 123 124 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 |
# File 'lib/gollum-lib/wiki.rb', line 121 def initialize(path, = {}) = self.class..merge() if path.is_a?(GitAccess) [:access] = path path = path.path end @path = path @repo_is_bare = .fetch :repo_is_bare, nil @page_file_dir = .fetch :page_file_dir, nil @page_file_dir = Pathname.new("/#{@page_file_dir}").cleanpath.to_s[1..-1] if @page_file_dir @access = .fetch :access, GitAccess.new(path, @page_file_dir, @repo_is_bare) @base_path = .fetch :base_path, "/" @repo = @access.repo @ref = .fetch :ref, self.class.default_ref @universal_toc = .fetch :universal_toc, false @mathjax = .fetch :mathjax, false @global_tag_lookup = .fetch :global_tag_lookup, false @hyphened_tag_lookup = .fetch :hyphened_tag_lookup, false @case_insensitive_tag_lookup = .fetch :case_insensitive_tag_lookup, false @css = .fetch :css, false @emoji = .fetch :emoji, false @critic_markup = .fetch :critic_markup, false @h1_title = .fetch :h1_title, false @display_metadata = .fetch :display_metadata, true @index_page = .fetch :index_page, 'Home' @bar_side = .fetch :sidebar, :right @user_icons = ['gravatar', 'identicon'].include?([:user_icons]) ? [:user_icons] : 'none' @allow_editing = .fetch :allow_editing, true @allow_uploads = .fetch :allow_uploads, false @per_page_uploads = .fetch :per_page_uploads, false @metadata = .fetch :metadata, {} @filter_chain = .fetch :filter_chain, [:YAML, :BibTeX, :PlainText, :CriticMarkup, :TOC, :RemoteCode, :Code, :Macro, :Emoji, :Sanitize, :PlantUML, :Tags, :PandocBib, :Render] @filter_chain.delete(:Emoji) unless .fetch :emoji, false @filter_chain.delete(:PandocBib) unless ::Gollum::MarkupRegisterUtils.using_pandoc? @filter_chain.delete(:CriticMarkup) unless .fetch :critic_markup, false Hook.execute(:post_wiki_initialize, self) end |
Class Attribute Details
.default_committer_email ⇒ Object
30 31 32 |
# File 'lib/gollum-lib/wiki.rb', line 30 def default_committer_email @default_committer_email || '[email protected]' end |
.default_committer_name ⇒ Object
26 27 28 |
# File 'lib/gollum-lib/wiki.rb', line 26 def default_committer_name @default_committer_name || 'Anonymous' end |
.default_options ⇒ Object
34 35 36 |
# File 'lib/gollum-lib/wiki.rb', line 34 def @default_options || {} end |
.default_ref ⇒ Object
22 23 24 |
# File 'lib/gollum-lib/wiki.rb', line 22 def default_ref @default_ref || 'master' end |
Instance Attribute Details
#allow_editing ⇒ Object (readonly)
Whether or not content is editable. Defaults to true
62 63 64 |
# File 'lib/gollum-lib/wiki.rb', line 62 def allow_editing @allow_editing end |
#allow_uploads ⇒ Object (readonly)
Toggles file upload functionality.
611 612 613 |
# File 'lib/gollum-lib/wiki.rb', line 611 def allow_uploads @allow_uploads end |
#bar_side ⇒ Object (readonly)
Gets side on which the sidebar should be shown
76 77 78 |
# File 'lib/gollum-lib/wiki.rb', line 76 def @bar_side end |
#base_path ⇒ Object (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 “/”.
48 49 50 |
# File 'lib/gollum-lib/wiki.rb', line 48 def base_path @base_path end |
#case_insensitive_tag_lookup ⇒ Object (readonly)
Enable 4.x compatibility for case-case_insensitive links
605 606 607 |
# File 'lib/gollum-lib/wiki.rb', line 605 def case_insensitive_tag_lookup @case_insensitive_tag_lookup end |
#css ⇒ Object (readonly)
Injects custom css from custom.css in root repo. Defaults to false
58 59 60 |
# File 'lib/gollum-lib/wiki.rb', line 58 def css @css end |
#display_metadata ⇒ Object (readonly)
Whether or not to render a page’s metadata on the page Defaults to true
70 71 72 |
# File 'lib/gollum-lib/wiki.rb', line 70 def @display_metadata end |
#filter_chain ⇒ Object (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.
82 83 84 |
# File 'lib/gollum-lib/wiki.rb', line 82 def filter_chain @filter_chain end |
#global_tag_lookup ⇒ Object (readonly)
Enable 4.x compatibility behavior for links
602 603 604 |
# File 'lib/gollum-lib/wiki.rb', line 602 def global_tag_lookup @global_tag_lookup end |
#h1_title ⇒ Object (readonly)
Sets page title to value of first h1 Defaults to false
66 67 68 |
# File 'lib/gollum-lib/wiki.rb', line 66 def h1_title @h1_title end |
#hyphened_tag_lookup ⇒ Object (readonly)
Spaces in tag paths are treated as dashes (-)
608 609 610 |
# File 'lib/gollum-lib/wiki.rb', line 608 def hyphened_tag_lookup @hyphened_tag_lookup end |
#index_page ⇒ Object (readonly)
Gets the custom index page for / and subdirs (e.g. foo/)
73 74 75 |
# File 'lib/gollum-lib/wiki.rb', line 73 def index_page @index_page end |
#mathjax ⇒ Object (readonly)
Toggles mathjax.
592 593 594 |
# File 'lib/gollum-lib/wiki.rb', line 592 def mathjax @mathjax end |
#metadata ⇒ Object (readonly)
Global metadata to be merged into the metadata for each page
85 86 87 |
# File 'lib/gollum-lib/wiki.rb', line 85 def @metadata end |
#page_file_dir ⇒ Object (readonly)
Gets the String directory in which all page files reside.
54 55 56 |
# File 'lib/gollum-lib/wiki.rb', line 54 def page_file_dir @page_file_dir end |
#path ⇒ Object (readonly)
The String path to the Git repository that holds the Gollum site.
Returns the String path.
43 44 45 |
# File 'lib/gollum-lib/wiki.rb', line 43 def path @path end |
#per_page_uploads ⇒ Object (readonly)
Toggles whether uploaded files go into ‘uploads’, or a directory named after the page they were uploaded to.
615 616 617 |
# File 'lib/gollum-lib/wiki.rb', line 615 def per_page_uploads @per_page_uploads end |
#ref ⇒ Object (readonly)
Gets the String ref in which all page files reside.
51 52 53 |
# File 'lib/gollum-lib/wiki.rb', line 51 def ref @ref end |
#repo ⇒ Object (readonly)
The Gollum::Git::Repo associated with the wiki.
Returns the Gollum::Git::Repo.
581 582 583 |
# File 'lib/gollum-lib/wiki.rb', line 581 def repo @repo end |
#repo_is_bare ⇒ Object (readonly)
Whether or not the wiki’s repository is bare (doesn’t have a working directory)
40 41 42 |
# File 'lib/gollum-lib/wiki.rb', line 40 def @repo_is_bare end |
#show_all ⇒ Object (readonly)
Toggles showing all files in files view. Default is false. When false, only valid pages in the git repo are displayed.
599 600 601 |
# File 'lib/gollum-lib/wiki.rb', line 599 def show_all @show_all end |
#universal_toc ⇒ Object (readonly)
Toggles display of universal table of contents
589 590 591 |
# File 'lib/gollum-lib/wiki.rb', line 589 def universal_toc @universal_toc end |
#user_icons ⇒ Object (readonly)
Toggles user icons. Default: ‘none’
595 596 597 |
# File 'lib/gollum-lib/wiki.rb', line 595 def user_icons @user_icons end |
Instance Method Details
#add_redirect(old_path, new_path, commit = nil) ⇒ Object
562 563 564 565 |
# File 'lib/gollum-lib/wiki.rb', line 562 def add_redirect(old_path, new_path, commit=nil) redirects[old_path] = new_path redirects.dump(commit) end |
#clear_cache ⇒ Object
Public: Refreshes just the cached Git reference data. This should be called after every Gollum update.
Returns nothing.
549 550 551 |
# File 'lib/gollum-lib/wiki.rb', line 549 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.
679 680 681 682 |
# File 'lib/gollum-lib/wiki.rb', line 679 def commit_for(ref) @access.commit(ref) rescue Gollum::Git::NoSuchShaFound end |
#default_committer_email ⇒ Object
Gets the default email for commits.
Returns the String email address.
668 669 670 671 672 |
# File 'lib/gollum-lib/wiki.rb', line 668 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_name ⇒ Object
Gets the default name for commits.
Returns the String name.
660 661 662 663 |
# File 'lib/gollum-lib/wiki.rb', line 660 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.
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/gollum-lib/wiki.rb', line 405 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.
385 386 387 |
# File 'lib/gollum-lib/wiki.rb', line 385 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.
166 167 168 |
# File 'lib/gollum-lib/wiki.rb', line 166 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.
191 192 193 |
# File 'lib/gollum-lib/wiki.rb', line 191 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.
476 477 478 |
# File 'lib/gollum-lib/wiki.rb', line 476 def files(treeish = nil) tree_list(treeish || @ref, false, true) end |
#inspect ⇒ Object
702 703 704 |
# File 'lib/gollum-lib/wiki.rb', line 702 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.
540 541 542 543 |
# File 'lib/gollum-lib/wiki.rb', line 540 def latest_changes(={}) [:max_count] = 10 unless [:max_count] @repo.log(@ref, page_file_dir, ) 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.
530 531 532 |
# File 'lib/gollum-lib/wiki.rb', line 530 def log( = {}) @repo.log(@ref, nil, ()) end |
#normalize(data) ⇒ Object
Normalize the data.
data - The String data to be normalized.
Returns the normalized data String.
622 623 624 |
# File 'lib/gollum-lib/wiki.rb', line 622 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
265 266 267 |
# File 'lib/gollum-lib/wiki.rb', line 265 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.
177 178 179 |
# File 'lib/gollum-lib/wiki.rb', line 177 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.
632 633 634 |
# File 'lib/gollum-lib/wiki.rb', line 632 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.
467 468 469 |
# File 'lib/gollum-lib/wiki.rb', line 467 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.
204 205 206 |
# File 'lib/gollum-lib/wiki.rb', line 204 def preview_page(name, data, format) ::Gollum::PreviewPage.new(self, "#{name}.#{::Gollum::Page.format_to_ext(format.to_sym)}", data, @access.commit(@ref)) end |
#redirects ⇒ Object
553 554 555 556 557 558 559 560 |
# File 'lib/gollum-lib/wiki.rb', line 553 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
567 568 569 570 |
# File 'lib/gollum-lib/wiki.rb', line 567 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.
287 288 289 290 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 |
# File 'lib/gollum-lib/wiki.rb', line 287 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.
456 457 458 459 460 |
# File 'lib/gollum-lib/wiki.rb', line 456 def revert_commit(sha1, sha2 = nil, commit = {}) left, right, = (sha1, sha2, commit) tree, files = repo.git.revert_commit(left, right) commit_and_update_paths(tree, files, ) 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.
436 437 438 439 440 |
# File 'lib/gollum-lib/wiki.rb', line 436 def revert_page(page, sha1, sha2 = nil, commit = {}) return false unless page left, right, = (sha1, sha2, commit) commit_and_update_paths(@repo.git.revert_path(page.path, left, right), [page.path], ) end |
#sanitizer ⇒ Object
Public: Creates a Sanitize instance
Returns a Sanitize instance.
709 710 711 |
# File 'lib/gollum-lib/wiki.rb', line 709 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
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/gollum-lib/wiki.rb', line 498 def search(query) = {: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, ) 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
485 486 487 488 489 490 491 |
# File 'lib/gollum-lib/wiki.rb', line 485 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.
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/gollum-lib/wiki.rb', line 641 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.
691 692 693 694 695 696 697 698 699 700 |
# File 'lib/gollum-lib/wiki.rb', line 691 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.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/gollum-lib/wiki.rb', line 342 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
245 246 247 |
# File 'lib/gollum-lib/wiki.rb', line 245 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.
225 226 227 |
# File 'lib/gollum-lib/wiki.rb', line 225 def write_page(path, format, data, commit = {}) write(merge_path_elements(nil, path, format), data, commit) end |