class Gollum::Page

Constants

SUBPAGENAMES

Attributes

parent_page[RW]

Parent page if this is a sub page

Returns a Page

wiki[R]

The underlying wiki repo.

Returns the Gollum::Wiki containing the page.

Public Class Methods

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 52
def self.format_for(filename)
  ext = ::File.extname(filename.to_s).sub(/^\./,'')
  Gollum::Markup.formats.each_pair do |name, format|
    return name if format[:extensions].include?(ext)
  end
  nil
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 279
def self.format_to_ext(format)
  Gollum::Markup.formats[format] ? Gollum::Markup.formats[format][:extensions].first : nil
end
new(wiki, blob, path, version, try_on_disk = false) click to toggle source

Public: Initialize a Page.

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

Returns a newly initialized Gollum::Page.

Calls superclass method Gollum::File::new
# File lib/gollum-lib/page.rb, line 78
def initialize(wiki, blob, path, version, try_on_disk = false)
  super
  @formatted_data = nil
  @doc            = nil
  @parent_page    = nil
  @historical     = @version.to_s == version.to_s
end
path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false) click to toggle source

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

query - The String path to match. entry - The BlobEntry to check against. global_match - If true, find a File matching path's filename, but not its directory (so anywhere in the repo)

# File lib/gollum-lib/page.rb, line 14
def path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false)
  return false if "#{entry.name}".empty?
  return false unless valid_extension?(entry.name)
  entry_name = valid_extension?(query) ? entry.name : strip_filename(entry.name)     
  match_path = ::File.join([
    '/',
    global_match ? nil : entry.dir,
  entry_name
  ].compact)
  path_compare(query, match_path, hyphened_tags, case_insensitive)
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 65
def self.strip_filename(filename)
  ::File.basename(filename.to_s, ::File.extname(filename.to_s))
end
valid_extension?(filename) click to toggle source

Checks if a filename has a valid, registered extension

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

Returns true or false.

# File lib/gollum-lib/page.rb, line 32
def self.valid_extension?(filename)
  Gollum::Markup.extensions.include?(::File.extname(filename.to_s).sub(/^\./,''))
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 is subpages (such as _Footer.md).

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

Returns true or false.

# File lib/gollum-lib/page.rb, line 42
def self.valid_page_name?(filename)
  subpage_names = SUBPAGENAMES.map(&:capitalize).join("|")
  filename =~ /^_(#{subpage_names})/ ? false : self.valid_extension?(filename)
end

Public Instance Methods

display_metadata?() click to toggle source

Public: Whether or not to display the metadata

# File lib/gollum-lib/page.rb, line 138
def display_metadata?
  return false if (metadata.keys - ['title', 'header_enum']).empty?
  return false if metadata['display_metadata'] == false
  @wiki.display_metadata
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 94
def filename_stripped
  self.class.strip_filename(filename)
end
find_sub_pages(subpagenames = SUBPAGENAMES, map = nil) click to toggle source

Loads sub pages. 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.

# File lib/gollum-lib/page.rb, line 306
def find_sub_pages(subpagenames = SUBPAGENAMES, map = nil)
  subpagenames.each{|subpagename| instance_variable_set("@#{subpagename}", nil)}
  return nil if self.filename =~ /^_/ || ! self.version

  map ||= @wiki.tree_map_for(@wiki.ref, true)
  valid_names = subpagenames.map(&:capitalize).join("|")
  # From Ruby 2.2 onwards map.select! could be used
  map = map.select{|entry| entry.name =~ /^_(#{valid_names})/ }
  return if map.empty?

  subpagenames.each do |subpagename|
    dir = ::Pathname.new(self.path)
    while dir = dir.parent do
      subpageblob = map.find do |blob_entry|

        filename = "_#{subpagename.to_s.capitalize}"
        searchpath = dir == Pathname.new('.') ? Pathname.new(filename) : dir + filename
        entrypath = ::Pathname.new(blob_entry.path)
        # Ignore extentions
        entrypath = entrypath.dirname + entrypath.basename(entrypath.extname)
        entrypath == searchpath
      end

      if subpageblob
        instance_variable_set("@#{subpagename}", subpageblob.page(@wiki, @version) )
        instance_variable_get("@#{subpagename}").parent_page = self
        break
      end

      break if dir == Pathname.new('.')
    end
  end
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 203
def format
  self.class.format_for(@blob.name)
end
formatted_data(encoding = nil, include_levels = 10) { |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 162
def formatted_data(encoding = nil, include_levels = 10, &block)
  return nil unless @blob

  if @formatted_data && @doc then
    yield @doc if block_given?
  else
    @formatted_data = markup.render(historical?, encoding, include_levels) do |doc|
      @doc = doc
      yield doc if block_given?
    end
  end

  @formatted_data
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 244
def header
  find_sub_pages unless defined?(@header)
  @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 270
def historical?
  !!@historical
end
inspect() click to toggle source
# File lib/gollum-lib/page.rb, line 340
def inspect
  %(#<#{self.class.name}:#{object_id} #{name} (#{format}) @wiki=#{@wiki.repo.path.inspect}>)
end
last_version() click to toggle source

Public: The last version that has touched the Page. Can be nil.

Returns Gollum::Git::Commit, or nil.

# File lib/gollum-lib/page.rb, line 229
def last_version
  return @last_version if defined? @last_version
  @last_version = @wiki.repo.git.versions_for_path(@path, @wiki.ref, {:max_count => 1}).first
end
markup() 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 210
def markup
  @markup ||= ::Gollum::Markup.new(self)
end
metadata() click to toggle source

Public: Embedded metadata.

Returns Hash of metadata.

# File lib/gollum-lib/page.rb, line 191
def metadata
  unless @metadata
    formatted_data if markup.metadata == nil
    @metadata = @wiki.metadata.merge(markup.metadata || {})
  else
    @metadata
  end
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 133
def metadata_title
  metadata ? metadata['title'] : nil
end
name() click to toggle source

Public: The canonical page name without extension.

Returns the String name.

# File lib/gollum-lib/page.rb, line 101
def name
  self.class.strip_filename(filename)
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 117
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 149
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.

Returns the fully sanitized String title.

# File lib/gollum-lib/page.rb, line 109
def title
  @wiki.sanitizer.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 182
def toc_data
  return @parent_page.toc_data if @parent_page and @sub_page
  formatted_data if markup.toc == nil
  markup.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 Gollum::Git::Tree for which to compute the path.

Returns the String path.

# File lib/gollum-lib/page.rb, line 294
def tree_path(treemap, tree)
  if (ptree = treemap[tree])
    "#{tree_path(treemap, ptree)}#{::File::SEPARATOR}#{tree.name}"
  else
    ''
  end
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 124
def url_path_title
  metadata_title || name
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 237
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_num  - The Integer page number (default: 1).
:per_page  - The Integer max count of items to return.
:follow    - Follow's a file across renames, slower.  (default: false)

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

# File lib/gollum-lib/page.rb, line 222
def versions(options = {})
  @wiki.repo.git.versions_for_path(@path, @wiki.ref, log_pagination_options(options))
end