Class: Gollum::Page
Constant Summary collapse
- VALID_PAGE_RE =
/^(.+)\.(md|mkdn?|mdown|markdown|ronn)$/i
- FORMAT_NAMES =
{ :markdown => "Markdown", :ronn => "ronn" }
Instance Attribute Summary collapse
-
#historical ⇒ Object
writeonly
Sets a Boolean determing whether this page is a historical version.
-
#path ⇒ Object
readonly
Public: The path of the page within the repo.
-
#version ⇒ Object
Public: The current version of the page.
-
#wiki ⇒ Object
readonly
The underlying wiki repo.
Class Method Summary collapse
-
.canonicalize_filename(filename) ⇒ Object
Reusable filter to turn a filename (without path) into a canonical name.
-
.cname(name) ⇒ Object
Convert a human page name into a canonical page name.
-
.format_for(filename) ⇒ Object
Public: The format of a given filename.
-
.format_to_ext(format) ⇒ Object
Convert a format Symbol into an extension String.
-
.valid_filename?(filename) ⇒ Boolean
Checks if a filename has a valid extension understood by GitHub::Markup.
-
.valid_page_name?(filename) ⇒ Boolean
Checks if a filename has a valid extension understood by GitHub::Markup.
Instance Method Summary collapse
-
#filename ⇒ Object
Public: The on-disk filename of the page including extension.
-
#find(name, version) ⇒ Object
Find a page in the given Gollum repo.
-
#find_page_in_tree(map, name, checked_dir = nil) ⇒ Object
Find a page in a given tree.
-
#find_sub_page(name) ⇒ Object
Loads a sub page.
-
#footer ⇒ Object
Public: The footer Page.
-
#format ⇒ Object
Public: The format of the page.
-
#formatted_data(&block) ⇒ Object
Public: The formatted contents of the page.
-
#historical? ⇒ Boolean
Gets a Boolean determining whether this page is a historical version.
-
#initialize(wiki) ⇒ Page
constructor
Public: Initialize a page.
- #inspect ⇒ Object
-
#markup_class ⇒ Object
Gets the Gollum::Markup instance that will render this page’s content.
-
#name ⇒ Object
Public: The canonical page name without extension, and dashes converted to spaces.
-
#page_match(name, filename) ⇒ Object
Compare the canonicalized versions of the two names.
-
#populate(blob, path = nil) ⇒ Object
Populate the Page with information from the Blob.
-
#raw_data ⇒ Object
Public: The raw contents of the page.
-
#sidebar ⇒ Object
Public: The sidebar Page.
-
#text_data(encoding = nil) ⇒ Object
Public: A text data encoded in specified encoding.
-
#title ⇒ Object
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.
-
#tree_path(treemap, tree) ⇒ Object
The full directory path for the given tree.
-
#versions(options = {}) ⇒ Object
Public: All of the versions that have touched the Page.
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.
67 68 69 70 |
# File 'lib/gollum/page.rb', line 67 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.
14 15 16 |
# File 'lib/gollum/page.rb', line 14 def historical=(value) @historical = value end |
#path ⇒ Object (readonly)
Public: The path of the page within the repo.
Returns the String path.
121 122 123 |
# File 'lib/gollum/page.rb', line 121 def path @path end |
#version ⇒ Object
Public: The current version of the page.
Returns the Grit::Commit.
168 169 170 |
# File 'lib/gollum/page.rb', line 168 def version @version end |
#wiki ⇒ Object (readonly)
The underlying wiki repo.
Returns the Gollum::Wiki containing the page.
257 258 259 |
# File 'lib/gollum/page.rb', line 257 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.
58 59 60 |
# File 'lib/gollum/page.rb', line 58 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.
230 231 232 233 234 |
# File 'lib/gollum/page.rb', line 230 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 | :ronn ]
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gollum/page.rb', line 43 def self.format_for(filename) case filename.to_s when /\.(md|mkdn?|mdown|markdown)$/i :markdown when /\.ronn$/i :ronn 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).
241 242 243 244 245 246 |
# File 'lib/gollum/page.rb', line 241 def self.format_to_ext(format) case format when :markdown then 'mkd' when :ronn then 'ronn' 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.
21 22 23 |
# File 'lib/gollum/page.rb', line 21 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.
32 33 34 35 |
# File 'lib/gollum/page.rb', line 32 def self.valid_page_name?(filename) match = valid_filename?(filename) filename =~ /^_/ ? false : match end |
Instance Method Details
#filename ⇒ Object
Public: The on-disk filename of the page including extension.
Returns the String name.
75 76 77 |
# File 'lib/gollum/page.rb', line 75 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.
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/gollum/page.rb', line 270 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.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/gollum/page.rb', line 289 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.
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/gollum/page.rb', line 351 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 |
#footer ⇒ Object
Public: The footer Page.
Returns the footer Page or nil if none exists.
194 195 196 |
# File 'lib/gollum/page.rb', line 194 def @footer ||= find_sub_page(:footer) end |
#format ⇒ Object
Public: The format of the page.
Returns the Symbol format of the page. One of:
[ :markdown | : ronn ]
154 155 156 |
# File 'lib/gollum/page.rb', line 154 def format self.class.format_for(@blob.name) end |
#formatted_data(&block) ⇒ Object
Public: The formatted contents of the page.
Returns the String data.
146 147 148 |
# File 'lib/gollum/page.rb', line 146 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.
210 211 212 |
# File 'lib/gollum/page.rb', line 210 def historical? !!@historical end |
#inspect ⇒ Object
370 371 372 |
# File 'lib/gollum/page.rb', line 370 def inspect %(#<#{self.class.name}:#{object_id} #{name} (#{format}) @wiki=#{@wiki.repo.path.inspect}>) end |
#markup_class ⇒ Object
Gets the Gollum::Markup instance that will render this page’s content.
Returns a Gollum::Markup instance.
161 162 163 |
# File 'lib/gollum/page.rb', line 161 def markup_class @markup_class ||= @wiki.markup_classes[format].new(self) end |
#name ⇒ Object
Public: The canonical page name without extension, and dashes converted to spaces.
Returns the String name.
83 84 85 |
# File 'lib/gollum/page.rb', line 83 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.
337 338 339 340 341 342 343 |
# File 'lib/gollum/page.rb', line 337 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.
311 312 313 314 315 |
# File 'lib/gollum/page.rb', line 311 def populate(blob, path=nil) @blob = blob @path = "#{path}/#{blob.name}"[1..-1] self end |
#raw_data ⇒ Object
Public: The raw contents of the page.
Returns the String data.
126 127 128 |
# File 'lib/gollum/page.rb', line 126 def raw_data @blob && @blob.data end |
#sidebar ⇒ Object
Public: The sidebar Page.
Returns the sidebar Page or nil if none exists.
201 202 203 |
# File 'lib/gollum/page.rb', line 201 def @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.
135 136 137 138 139 140 141 |
# File 'lib/gollum/page.rb', line 135 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 ⇒ Object
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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/gollum/page.rb', line 94 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.
323 324 325 326 327 328 329 |
# File 'lib/gollum/page.rb', line 323 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.
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/gollum/page.rb', line 179 def versions( = {}) if [:follow] [:pretty] = 'raw' .delete :max_count .delete :skip log = @wiki.repo.git.native "log", , @wiki.ref, "--", @path Grit::Commit.list_from_string(@wiki.repo, log) else @wiki.repo.log(@wiki.ref, @path, ()) end end |