Class: Gollum::Page

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

Constant Summary collapse

VALID_PAGE_RE =
/^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|creole|re?st(\.txt)?|asciidoc|pod|(media)?wiki)$/i
FORMAT_NAMES =
{ :markdown  => "Markdown",
:textile   => "Textile",
:rdoc      => "RDoc",
:org       => "Org-mode",
:creole    => "Creole",
:rest      => "reStructuredText",
:asciidoc  => "AsciiDoc",
:mediawiki => "MediaWiki",
:pod       => "Pod" }

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(wiki) ⇒ Page

Public: Initialize a page.

wiki - The Gollum::Wiki in question.

Returns a newly initialized Gollum::Page.



91
92
93
94
# File 'lib/gollum/page.rb', line 91

def initialize(wiki)
  @wiki = wiki
  @blob = @footer = @sidebar = nil
end

Instance Attribute Details

#historical=(value) ⇒ Object (writeonly)

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

Returns nothing.



21
22
23
# File 'lib/gollum/page.rb', line 21

def historical=(value)
  @historical = value
end

#pathObject (readonly)

Public: The path of the page within the repo.

Returns the String path.



145
146
147
# File 'lib/gollum/page.rb', line 145

def path
  @path
end

#versionObject

Public: The current version of the page.

Returns the Grit::Commit.



193
194
195
# File 'lib/gollum/page.rb', line 193

def version
  @version
end

#wikiObject (readonly)

The underlying wiki repo.

Returns the Gollum::Wiki containing the page.



289
290
291
# File 'lib/gollum/page.rb', line 289

def wiki
  @wiki
end

Class Method Details

.canonicalize_filename(filename) ⇒ Object

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

Returns the filtered String.



82
83
84
# File 'lib/gollum/page.rb', line 82

def self.canonicalize_filename(filename)
  filename.split('.')[0..-2].join('.').gsub('-', ' ')
end

.cname(name) ⇒ Object

Convert a human page name into a canonical page name.

name - The String human page name.

Examples

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

Returns the String canonical name.



255
256
257
258
259
# File 'lib/gollum/page.rb', line 255

def self.cname(name)
  name.respond_to?(:gsub)      ?
    name.gsub(%r{[ /<>]}, '-') :
    ''
end

.format_for(filename) ⇒ Object

Public: The format of a given filename.

filename - The String filename.

Returns the Symbol format of the page. One of:

[ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
  :roff ]


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gollum/page.rb', line 51

def self.format_for(filename)
  case filename.to_s
    when /\.(md|mkdn?|mdown|markdown)$/i
      :markdown
    when /\.(textile)$/i
      :textile
    when /\.(rdoc)$/i
      :rdoc
    when /\.(org)$/i
      :org
    when /\.(creole)$/i
      :creole
    when /\.(re?st(\.txt)?)$/i
      :rest
    when /\.(asciidoc)$/i
      :asciidoc
    when /\.(pod)$/i
      :pod
    when /\.(\d)$/i
      :roff
    when /\.(media)?wiki$/i
      :mediawiki
    else
      nil
  end
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).



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/gollum/page.rb', line 266

def self.format_to_ext(format)
  case format
    when :markdown  then 'md'
    when :textile   then 'textile'
    when :rdoc      then 'rdoc'
    when :org       then 'org'
    when :creole    then 'creole'
    when :rest      then 'rest'
    when :asciidoc  then 'asciidoc'
    when :pod       then 'pod'
    when :mediawiki then 'mediawiki'
  end
end

.valid_filename?(filename) ⇒ Boolean

Checks if a filename has a valid extension understood by GitHub::Markup.

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

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/gollum/page.rb', line 28

def self.valid_filename?(filename)
  filename && filename.to_s =~ VALID_PAGE_RE && $1
end

.valid_page_name?(filename) ⇒ Boolean

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.

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/gollum/page.rb', line 39

def self.valid_page_name?(filename)
  match = valid_filename?(filename)
  filename =~ /^_/ ? false : match
end

Instance Method Details

#filenameObject

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

Returns the String name.



99
100
101
# File 'lib/gollum/page.rb', line 99

def filename
  @blob && @blob.name
end

#find(name, version) ⇒ Object

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.



302
303
304
305
306
307
308
309
310
311
# File 'lib/gollum/page.rb', line 302

def find(name, version)
  map = @wiki.tree_map_for(version.to_s)
  if page = find_page_in_tree(map, name)
    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) ⇒ Object

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.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/gollum/page.rb', line 321

def find_page_in_tree(map, name, checked_dir = nil)
  return nil if !map || name.to_s.empty?
  if checked_dir = BlobEntry.normalize_dir(checked_dir)
    checked_dir.downcase!
  end

  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) ⇒ Object

Loads a sub page. Sub page nanes (footers) are prefixed with an underscore to distinguish them from other Pages.

name - String page name.

Returns the Page or nil if none exists.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/gollum/page.rb', line 383

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(self.version.id)
  while !dirs.empty?
    if page = find_page_in_tree(map, name, dirs.join('/'))
      return page
    end
    dirs.pop
  end

  find_page_in_tree(map, name, '')
end

Public: The footer Page.

Returns the footer Page or nil if none exists.



219
220
221
# File 'lib/gollum/page.rb', line 219

def footer
  @footer ||= find_sub_page(:footer)
end

#formatObject

Public: The format of the page.

Returns the Symbol format of the page. One of:

[ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
  :roff ]


179
180
181
# File 'lib/gollum/page.rb', line 179

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

#formatted_data(&block) ⇒ Object

Public: The formatted contents of the page.

Returns the String data.



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

def formatted_data(&block)
  @blob && markup_class.render(historical?, &block)
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)


235
236
237
# File 'lib/gollum/page.rb', line 235

def historical?
  !!@historical
end

#inspectObject



402
403
404
# File 'lib/gollum/page.rb', line 402

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

#markup_classObject

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

Returns a Gollum::Markup instance.



186
187
188
# File 'lib/gollum/page.rb', line 186

def markup_class
  @markup_class ||= @wiki.markup_classes[format].new(self)
end

#nameObject

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

Returns the String name.



107
108
109
# File 'lib/gollum/page.rb', line 107

def name
  self.class.canonicalize_filename(filename)
end

#page_match(name, filename) ⇒ Object

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.



369
370
371
372
373
374
375
# File 'lib/gollum/page.rb', line 369

def page_match(name, filename)
  if match = self.class.valid_filename?(filename)
    Page.cname(name).downcase == Page.cname(match).downcase
  else
    false
  end
end

#populate(blob, path = nil) ⇒ Object

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.



343
344
345
346
347
# File 'lib/gollum/page.rb', line 343

def populate(blob, path=nil)
  @blob = blob
  @path = "#{path}/#{blob.name}"[1..-1]
  self
end

#raw_dataObject

Public: The raw contents of the page.

Returns the String data.



150
151
152
# File 'lib/gollum/page.rb', line 150

def raw_data
  @blob && @blob.data
end

Public: The sidebar Page.

Returns the sidebar Page or nil if none exists.



226
227
228
# File 'lib/gollum/page.rb', line 226

def sidebar
  @sidebar ||= find_sub_page(:sidebar)
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.



159
160
161
162
163
164
165
# File 'lib/gollum/page.rb', line 159

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: If the first element of a formatted page is an <h1> tag it can be considered the title of the page and used in the display. If the first element is NOT an <h1> tag, the title will be constructed from the filename by stripping the extension and replacing any dashes with spaces.

Returns the fully sanitized String title.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gollum/page.rb', line 118

def title
  doc = Nokogiri::HTML(%{<div id="gollum-root">} + self.formatted_data + %{</div>})

  header =
  case self.format
    when :asciidoc
      doc.css("div#gollum-root > div#header > h1:first-child")
    when :org
      doc.css("div#gollum-root > p.title:first-child")
    when :pod
      doc.css("div#gollum-root > a.dummyTopAnchor:first-child + h1")
    when :rest
      doc.css("div#gollum-root > div > div > h1:first-child")
    else
      doc.css("div#gollum-root > h1:first-child")
  end

  if !header.empty?
    Sanitize.clean(header.to_html)
  else
    Sanitize.clean(name)
  end.strip
end

#tree_path(treemap, tree) ⇒ Object

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.



355
356
357
358
359
360
361
# File 'lib/gollum/page.rb', line 355

def tree_path(treemap, tree)
  if ptree = treemap[tree]
    tree_path(treemap, ptree) + '/' + tree.name
  else
    ''
  end
end

#versions(options = {}) ⇒ Object

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.



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/gollum/page.rb', line 204

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