Class: Gollum::Page

Inherits:
File
  • Object
show all
Includes:
Pagination
Defined in:
lib/gollum-lib/page.rb

Direct Known Subclasses

PreviewPage

Constant Summary collapse

SUBPAGENAMES =
[:header, :footer, :sidebar]

Constants inherited from File

File::FS_SUPPORT_SYMLINKS

Instance Attribute Summary collapse

Attributes inherited from File

#on_disk, #path, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pagination

included, #log_pagination_options, #page_to_skip

Methods inherited from File

#escaped_url_path, #filename, find, #mime_type, #on_disk?, #on_disk_path, path_compare, protected_files, #raw_data, #sha, #url_path

Constructor Details

#initialize(wiki, blob, path, version, try_on_disk = false) ⇒ Page

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.



78
79
80
81
82
83
84
# 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

Instance Attribute Details

#parent_pageObject

Parent page if this is a sub page

Returns a Page



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

def parent_page
  @parent_page
end

#wikiObject (readonly)

The underlying wiki repo.

Returns the Gollum::Wiki containing the page.



286
287
288
# File 'lib/gollum-lib/page.rb', line 286

def wiki
  @wiki
end

Class Method Details

.format_for(filename) ⇒ Object

Public: The format of a given filename.

filename - The String filename.

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



52
53
54
55
56
57
58
# 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) ⇒ Object

Convert a format Symbol into an extension String.

format - The format Symbol.

Returns the String extension (no leading period).



279
280
281
# 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

.path_match(query, entry, global_match = false, hyphened_tags = false, case_insensitive = false) ⇒ Object

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)



14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

Reusable filter to strip extension and path from filename

filename - The string path or filename to strip

Returns the stripped String.



65
66
67
# 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) ⇒ Boolean

Checks if a filename has a valid, registered extension

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

Returns true or false.

Returns:

  • (Boolean)


32
33
34
# 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) ⇒ Boolean

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.

Returns:

  • (Boolean)


42
43
44
45
# 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

Instance Method Details

#display_metadata?Boolean

Public: Whether or not to display the metadata

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/gollum-lib/page.rb', line 138

def display_metadata?
  return false if (.keys - ['title', 'header_enum']).empty?
  return false if ['display_metadata'] == false
  @wiki.
end

#filename_strippedObject

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

Returns the String name.



94
95
96
# File 'lib/gollum-lib/page.rb', line 94

def filename_stripped
  self.class.strip_filename(filename)
end

#find_sub_pages(subpagenames = SUBPAGENAMES, map = nil) ⇒ Object

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.



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# 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

Public: The footer Page.

Returns the footer Page or nil if none exists.



252
253
254
255
# File 'lib/gollum-lib/page.rb', line 252

def footer
  find_sub_pages unless defined?(@footer)
  @footer
end

#formatObject

Public: The format of the page.

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



203
204
205
# File 'lib/gollum-lib/page.rb', line 203

def format
  self.class.format_for(@blob.name)
end

#formatted_data(encoding = nil, include_levels = 10, &block) ⇒ Object

Public: The formatted contents of the page.

encoding - Encoding Constant or String.

Returns the String data.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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

#headerObject

Public: The header Page.

Returns the header Page or nil if none exists.



244
245
246
247
# File 'lib/gollum-lib/page.rb', line 244

def header
  find_sub_pages unless defined?(@header)
  @header
end

#historical?Boolean

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.

Returns:

  • (Boolean)


270
271
272
# File 'lib/gollum-lib/page.rb', line 270

def historical?
  !!@historical
end

#inspectObject



340
341
342
# File 'lib/gollum-lib/page.rb', line 340

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

#last_versionObject

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

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



229
230
231
232
# 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

#markupObject

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

Returns a Gollum::Markup instance.



210
211
212
# File 'lib/gollum-lib/page.rb', line 210

def markup
  @markup ||= ::Gollum::Markup.new(self)
end

#metadataObject

Public: Embedded metadata.

Returns Hash of metadata.



191
192
193
194
195
196
197
198
# File 'lib/gollum-lib/page.rb', line 191

def 
  unless @metadata
    formatted_data if markup. == nil
    @metadata = @wiki..merge(markup. || {})
  else
    @metadata
  end
end

#metadata_titleObject

Public: Metadata title

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

Returns the String title or nil if not defined



133
134
135
# File 'lib/gollum-lib/page.rb', line 133

def 
   ? ['title'] : nil
end

#nameObject

Public: The canonical page name without extension.

Returns the String name.



101
102
103
# File 'lib/gollum-lib/page.rb', line 101

def name
  self.class.strip_filename(filename)
end

Public: The sidebar Page.

Returns the sidebar Page or nil if none exists.



260
261
262
263
# File 'lib/gollum-lib/page.rb', line 260

def sidebar
  find_sub_pages unless defined?(@sidebar)
  @sidebar
end

#sub_pageObject

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

Returns true or false.



117
118
119
# File 'lib/gollum-lib/page.rb', line 117

def sub_page
  filename =~ /^_/
end

#text_data(encoding = nil) ⇒ Object

Public: A text data encoded in specified encoding.

encoding - An Encoding or nil

Returns a character encoding aware String.



149
150
151
152
153
154
155
# 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

#titleObject

Public: The title will be constructed from the filename by stripping the extension.

Returns the fully sanitized String title.



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

def title
  @wiki.sanitizer.clean(name).strip
end

#toc_dataObject

Public: The table of contents of the page.

formatted_data - page already marked up in html.

Returns the String data.



182
183
184
185
186
# 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) ⇒ Object

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.



294
295
296
297
298
299
300
# 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_titleObject

Public: Defines title for page.rb

Returns the String title



124
125
126
# File 'lib/gollum-lib/page.rb', line 124

def url_path_title
   || name
end

#version_shortObject

Public: The first 7 characters of the current version.

Returns the first 7 characters of the current version.



237
238
239
# File 'lib/gollum-lib/page.rb', line 237

def version_short
  version.to_s[0, 7]
end

#versions(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.
:follow    - Follow's a file across renames, slower.  (default: false)

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



222
223
224
# 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