class Gollum::Page

Attributes

historical[W]

Sets a Boolean determing whether this page is a historical version.

Returns nothing.

parent_page[RW]

Parent page if this is a sub page

Returns a Page

path[R]

Public: The path of the page within the repo.

Returns the String path.

version[RW]

Public: The current version of the page.

Returns the Grit::Commit.

wiki[R]

The underlying wiki repo.

Returns the Gollum::Wiki containing the page.

Public Class Methods

canonicalize_filename(filename) click to toggle source

Reusable filter to turn a filename (without path) into a canonical name. Strips extension, converts dashes to spaces.

Returns the filtered String.

# File lib/gollum-lib/page.rb, line 67
def self.canonicalize_filename(filename)
  strip_filename(filename).gsub('-', ' ')
end
cname(name, char_white_sub = '-', char_other_sub = '-') click to toggle source

Convert a human page name into a canonical page name.

name - The String human page name. char_white_sub - Substitution for whitespace char_other_sub - Substitution for other special chars

Examples

Page.cname("Bilbo Baggins")
# => 'Bilbo-Baggins'

Page.cname("Bilbo Baggins",'_')
# => 'Bilbo_Baggins'

Returns the String canonical name.

# File lib/gollum-lib/page.rb, line 334
def self.cname(name, char_white_sub = '-', char_other_sub = '-')
  name.respond_to?(:gsub) ?
    name.gsub(%r{\s},char_white_sub).gsub(%r{[<>+]}, char_other_sub) :
    ''
end
format_for(filename) click to toggle source

Public: The format of a given filename.

filename - The String filename.

Returns the Symbol format of the page; one of the registered format types

# File lib/gollum-lib/page.rb, line 59
def self.format_for(filename)
  self.parse_filename(filename).last
end
format_to_ext(format) click to toggle source

Convert a format Symbol into an extension String.

format - The format Symbol.

Returns the String extension (no leading period).

# File lib/gollum-lib/page.rb, line 345
def self.format_to_ext(format)
  format == :markdown ? "md" : format.to_s
end
new(wiki) click to toggle source

Public: Initialize a page.

wiki - The Gollum::Wiki in question.

Returns a newly initialized Gollum::Page.

# File lib/gollum-lib/page.rb, line 85
def initialize(wiki)
  @wiki = wiki
  @blob = @header = @footer = @sidebar = nil
  @doc = nil
  @parent_page = nil
end
parse_filename(filename) click to toggle source

Checks a filename against the registered markup extensions

filename - String filename, like “Home.md”

Returns e.g. [“Home”, :markdown], or [] if the extension is unregistered

# File lib/gollum-lib/page.rb, line 23
def self.parse_filename(filename)
  return [] unless filename =~ /^(.+)\.([a-zA-Z]\w*)$/i
  pref, ext = $1, $2

  Gollum::Markup.formats.each_pair do |name, format|
    return [pref, name] if ext =~ format[:regexp]
  end
  []
end
strip_filename(filename) click to toggle source

Reusable filter to strip extension and path from filename

filename - The string path or filename to strip

Returns the stripped String.

# File lib/gollum-lib/page.rb, line 76
def self.strip_filename(filename)
  ::File.basename(filename, ::File.extname(filename))
end
valid_filename?(filename) click to toggle source

Checks if a filename has a valid, registered extension

filename - String filename, like “Home.md”.

Returns the matching String basename of the file without the extension.

# File lib/gollum-lib/page.rb, line 38
def self.valid_filename?(filename)
  self.parse_filename(filename).first
end
valid_page_name?(filename) click to toggle source

Checks if a filename has a valid extension understood by GitHub::Markup. Also, checks if the filename has no “_” in the front (such as _Footer.md).

filename - String filename, like “Home.md”.

Returns the matching String basename of the file without the extension.

# File lib/gollum-lib/page.rb, line 49
def self.valid_page_name?(filename)
  match = valid_filename?(filename)
  filename =~ /^_/ ? false : match
end

Public Instance Methods

escaped_url_path() click to toggle source

Public: The #url_path, but CGI escaped.

Returns the String #url_path

# File lib/gollum-lib/page.rb, line 174
def escaped_url_path
  CGI.escape(self.url_path).gsub('%2F','/')
end
filename() click to toggle source

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

Returns the String name.

# File lib/gollum-lib/page.rb, line 95
def filename
  @blob && @blob.name
end
filename_stripped() click to toggle source

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

Returns the String name.

# File lib/gollum-lib/page.rb, line 102
def filename_stripped
  self.class.strip_filename(filename)
end
find(name, version, dir = nil, exact = false) click to toggle source

Find a page in the given Gollum repo.

name - The human or canonical String page name to find. version - The String version ID to find.

Returns a Gollum::Page or nil if the page could not be found.

# File lib/gollum-lib/page.rb, line 371
def find(name, version, dir = nil, exact = false)
  map = @wiki.tree_map_for(version.to_s)
  if page = find_page_in_tree(map, name, dir, exact)
    page.version    = version.is_a?(Grit::Commit) ?
      version : @wiki.commit_for(version)
    page.historical = page.version.to_s == version.to_s
    page
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
end
find_page_in_tree(map, name, checked_dir = nil, exact = false) click to toggle source

Find a page in a given tree.

map - The Array tree map from Wiki#tree_map. name - The canonical String page name. checked_dir - Optional String of the directory a matching page needs

to be in.  The string should

Returns a Gollum::Page or nil if the page could not be found.

# File lib/gollum-lib/page.rb, line 390
def find_page_in_tree(map, name, checked_dir = nil, exact = false)
  return nil if !map || name.to_s.empty?
  if checked_dir = BlobEntry.normalize_dir(checked_dir)
    checked_dir.downcase!
  end

  checked_dir = '' if exact && checked_dir.nil?

  map.each do |entry|
    next if entry.name.to_s.empty?
    next unless checked_dir.nil? || entry.dir.downcase == checked_dir
    next unless page_match(name, entry.name)
    return entry.page(@wiki, @version)
  end

  return nil # nothing was found
end
find_sub_page(name) click to toggle source

Loads a sub page. Sub page names (footers, headers, sidebars) are prefixed with an underscore to distinguish them from other Pages. If there is not one within the current directory, starts walking up the directory tree to try and find one within parent directories.

name - String page name.

Returns the Page or nil if none exists.

# File lib/gollum-lib/page.rb, line 457
def find_sub_page(name)
  return nil unless self.version
  return nil if self.filename =~ /^_/
  name = "_#{name.to_s.capitalize}"
  return nil if page_match(name, self.filename)

  dirs = self.path.split('/')
  dirs.pop
  map = @wiki.tree_map_for(@wiki.ref, true)
  while !dirs.empty?
    if page = find_page_in_tree(map, name, dirs.join('/'))
      page.parent_page = self
      return page
    end
    dirs.pop
  end

  if page = find_page_in_tree(map, name, '')
    page.parent_page = self
  end
  page
end
format() click to toggle source

Public: The format of the page.

Returns the Symbol format of the page; one of the registered format types

# File lib/gollum-lib/page.rb, line 239
def format
  self.class.format_for(@blob.name)
end
formatted_data(encoding = nil) { |doc| ... } click to toggle source

Public: The formatted contents of the page.

encoding - Encoding Constant or String.

Returns the String data.

# File lib/gollum-lib/page.rb, line 210
def formatted_data(encoding = nil, &block)
  @blob && markup_class.render(historical?, encoding) do |doc|
    @doc = doc
    yield doc if block_given?
  end
end
header() click to toggle source

Public: The header Page.

Returns the header Page or nil if none exists.

# File lib/gollum-lib/page.rb, line 286
def header
  @header ||= find_sub_page(:header)
end
historical?() click to toggle source

Gets a Boolean determining whether this page is a historical version. Historical pages are pulled using exact SHA hashes and format all links with rel=“nofollow”

Returns true if the page is pulled from a named branch or tag, or false.

# File lib/gollum-lib/page.rb, line 309
def historical?
  !!@historical
end
inspect() click to toggle source
# File lib/gollum-lib/page.rb, line 480
def inspect
  %Q(#<#{self.class.name}:#{object_id} #{name} (#{format}) @wiki=#{@wiki.repo.path.inspect}>)
end
markup_class() click to toggle source

Gets the Gollum::Markup instance that will render this page's content.

Returns a Gollum::Markup instance.

# File lib/gollum-lib/page.rb, line 246
def markup_class
  @markup_class ||= @wiki.markup_classes[format].new(self)
end
metadata() click to toggle source

Public: Embedded metadata.

Returns Hash of metadata.

# File lib/gollum-lib/page.rb, line 231
def metadata()
  formatted_data if markup_class.metadata == nil
  markup_class.metadata
end
metadata_title() click to toggle source

Public: Metadata title

Set with <!– — title: New Title –> in page content

Returns the String title or nil if not defined

# File lib/gollum-lib/page.rb, line 162
def metadata_title
  if metadata
    title = metadata['title']
    return title unless title.nil?
  end

  nil
end
name() click to toggle source

Public: The canonical page name without extension, and dashes converted to spaces.

Returns the String name.

# File lib/gollum-lib/page.rb, line 110
def name
  self.class.canonicalize_filename(filename)
end
page_match(name, filename) click to toggle source

Compare the canonicalized versions of the two names.

name - The human or canonical String page name. filename - the String filename on disk (including extension).

Returns a Boolean.

# File lib/gollum-lib/page.rb, line 440
def page_match(name, filename)
  if match = self.class.valid_filename?(filename)
    @wiki.ws_subs.each do |sub|
      return true if Page.cname(name).downcase == Page.cname(match, sub).downcase
    end
  end
  false
end
populate(blob, path=nil) click to toggle source

Populate the Page with information from the Blob.

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

Returns the populated Gollum::Page.

# File lib/gollum-lib/page.rb, line 414
def populate(blob, path=nil)
  @blob = blob
  @path = "#{path}/#{blob.name}"[1..-1]
  self
end
raw_data() click to toggle source

Public: The raw contents of the page.

Returns the String data.

# File lib/gollum-lib/page.rb, line 181
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
sidebar() click to toggle source

Public: The sidebar Page.

Returns the sidebar Page or nil if none exists.

sub_page() click to toggle source

Public: Determines if this is a sub-page Sub-pages have filenames beginning with an underscore

Returns true or false.

# File lib/gollum-lib/page.rb, line 127
def sub_page
  filename =~ /^_/
end
text_data(encoding=nil) click to toggle source

Public: A text data encoded in specified encoding.

encoding - An Encoding or nil

Returns a character encoding aware String.

# File lib/gollum-lib/page.rb, line 197
def text_data(encoding=nil)
  if raw_data.respond_to?(:encoding)
    raw_data.force_encoding(encoding || Encoding::UTF_8)
  else
    raw_data
  end
end
title() click to toggle source

Public: The title will be constructed from the filename by stripping the extension and replacing any dashes with spaces.

Returns the fully sanitized String title.

# File lib/gollum-lib/page.rb, line 119
def title
  Sanitize.clean(name).strip
end
toc_data() click to toggle source

Public: The table of contents of the page.

#formatted_data - page already marked up in html.

Returns the String data.

# File lib/gollum-lib/page.rb, line 222
def toc_data()
  return @parent_page.toc_data if @parent_page and @sub_page
  formatted_data if markup_class.toc == nil
  markup_class.toc
end
tree_path(treemap, tree) click to toggle source

The full directory path for the given tree.

treemap - The Hash treemap containing parentage information. tree - The Grit::Tree for which to compute the path.

Returns the String path.

# File lib/gollum-lib/page.rb, line 426
def tree_path(treemap, tree)
  if ptree = treemap[tree]
    tree_path(treemap, ptree) + '/' + tree.name
  else
    ''
  end
end
url_path() click to toggle source

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

Returns the String #url_path

# File lib/gollum-lib/page.rb, line 139
def url_path
  path = if self.path.include?('/')
    self.path.sub(/\/[^\/]+$/, '/')
  else
    ''
  end

  path << Page.cname(self.name, '-', '-')
  path
end
url_path_title() click to toggle source

Public: Defines title for page.rb

Returns the String title

# File lib/gollum-lib/page.rb, line 153
def url_path_title
  metadata_title || url_path.gsub("-", " ")
end
version_short() click to toggle source

Public: The first 7 characters of the current version.

Returns the first 7 characters of the current version.

# File lib/gollum-lib/page.rb, line 279
def version_short
  version.to_s[0,7]
end
versions(options = {}) click to toggle source

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.
:follow   - Follow's a file across renames, but falls back
            to a slower Grit native call.  (default: false)

Returns an Array of Grit::Commit.

# File lib/gollum-lib/page.rb, line 264
def versions(options = {})
  if options[:follow]
    options[:pretty] = 'raw'
    options.delete :max_count
    options.delete :skip
    log = @wiki.repo.git.native "log", options, @wiki.ref, "--", @path
    Grit::Commit.list_from_string(@wiki.repo, log)
  else
    @wiki.repo.log(@wiki.ref, @path, log_pagination_options(options))
  end
end