Class: Gollum::Page
- Inherits:
-
Object
- Object
- Gollum::Page
- Includes:
- Pagination
- Defined in:
- lib/gollum-lib/page.rb
Instance Attribute Summary collapse
-
#historical ⇒ Object
writeonly
Sets a Boolean determing whether this page is a historical version.
-
#parent_page ⇒ Object
Parent page if this is a sub page.
-
#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, char_white_sub = '-', char_other_sub = '-') ⇒ 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.
-
.parse_filename(filename) ⇒ Object
Checks a filename against the registered markup extensions.
-
.strip_filename(filename) ⇒ Object
Reusable filter to strip extension and path from filename.
-
.valid_filename?(filename) ⇒ Boolean
Checks if a filename has a valid, registered extension.
-
.valid_page_name?(filename) ⇒ Boolean
Checks if a filename has a valid extension understood by GitHub::Markup.
Instance Method Summary collapse
-
#escaped_url_path ⇒ Object
Public: The url_path, but CGI escaped.
-
#filename ⇒ Object
Public: The on-disk filename of the page including extension.
-
#filename_stripped ⇒ Object
Public: The on-disk filename of the page with extension stripped.
-
#find(name, version, dir = nil, exact = false) ⇒ Object
Find a page in the given Gollum repo.
-
#find_page_in_tree(map, name, checked_dir = nil, exact = false) ⇒ 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(encoding = nil, &block) ⇒ Object
Public: The formatted contents of the page.
-
#header ⇒ Object
Public: The header 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.
-
#metadata ⇒ Object
Public: Embedded metadata.
-
#metadata_title ⇒ Object
Public: Metadata title.
-
#name ⇒ Object
Public: The canonical page name without extension, and dashes converted to spaces.
-
#page_match(name, path) ⇒ 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.
-
#sub_page ⇒ Object
Public: Determines if this is a sub-page Sub-pages have filenames beginning with an underscore.
-
#text_data(encoding = nil) ⇒ Object
Public: A text data encoded in specified encoding.
-
#title ⇒ Object
Public: The title will be constructed from the filename by stripping the extension and replacing any dashes with spaces.
-
#toc_data ⇒ Object
Public: The table of contents of the page.
-
#tree_path(treemap, tree) ⇒ Object
The full directory path for the given tree.
-
#url_path ⇒ Object
Public: The url path required to reach this page within the repo.
-
#url_path_title ⇒ Object
Public: Defines title for page.rb.
-
#version_short ⇒ Object
Public: The first 7 characters of the current version.
-
#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.
86 87 88 89 90 91 |
# File 'lib/gollum-lib/page.rb', line 86 def initialize(wiki) @wiki = wiki @blob = @header = @footer = @sidebar = nil @doc = nil @parent_page = nil end |
Instance Attribute Details
#historical=(value) ⇒ Object (writeonly)
Sets a Boolean determing whether this page is a historical version.
Returns nothing.
11 12 13 |
# File 'lib/gollum-lib/page.rb', line 11 def historical=(value) @historical = value end |
#parent_page ⇒ Object
Parent page if this is a sub page
Returns a Page
16 17 18 |
# File 'lib/gollum-lib/page.rb', line 16 def parent_page @parent_page end |
#path ⇒ Object (readonly)
Public: The path of the page within the repo.
Returns the String path.
135 136 137 |
# File 'lib/gollum-lib/page.rb', line 135 def path @path end |
#version ⇒ Object
Public: The current version of the page.
Returns the Grit::Commit.
254 255 256 |
# File 'lib/gollum-lib/page.rb', line 254 def version @version end |
#wiki ⇒ Object (readonly)
The underlying wiki repo.
Returns the Gollum::Wiki containing the page.
359 360 361 |
# File 'lib/gollum-lib/page.rb', line 359 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 dashes to spaces.
Returns the filtered String.
68 69 70 |
# File 'lib/gollum-lib/page.rb', line 68 def self.canonicalize_filename(filename) strip_filename(filename).gsub('-', ' ') end |
.cname(name, char_white_sub = '-', char_other_sub = '-') ⇒ Object
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.
335 336 337 338 339 |
# File 'lib/gollum-lib/page.rb', line 335 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) ⇒ 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
60 61 62 |
# File 'lib/gollum-lib/page.rb', line 60 def self.format_for(filename) self.parse_filename(filename).last 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).
346 347 348 |
# File 'lib/gollum-lib/page.rb', line 346 def self.format_to_ext(format) format == :markdown ? "md" : format.to_s end |
.parse_filename(filename) ⇒ Object
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
24 25 26 27 28 29 30 31 32 |
# File 'lib/gollum-lib/page.rb', line 24 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) ⇒ Object
Reusable filter to strip extension and path from filename
filename - The string path or filename to strip
Returns the stripped String.
77 78 79 |
# File 'lib/gollum-lib/page.rb', line 77 def self.strip_filename(filename) ::File.basename(filename, ::File.extname(filename)) end |
.valid_filename?(filename) ⇒ Boolean
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.
39 40 41 |
# File 'lib/gollum-lib/page.rb', line 39 def self.valid_filename?(filename) self.parse_filename(filename).first 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.
50 51 52 53 |
# File 'lib/gollum-lib/page.rb', line 50 def self.valid_page_name?(filename) match = valid_filename?(filename) filename =~ /^_/ ? false : match end |
Instance Method Details
#escaped_url_path ⇒ Object
Public: The url_path, but CGI escaped.
Returns the String url_path
175 176 177 |
# File 'lib/gollum-lib/page.rb', line 175 def escaped_url_path CGI.escape(self.url_path).gsub('%2F','/') end |
#filename ⇒ Object
Public: The on-disk filename of the page including extension.
Returns the String name.
96 97 98 |
# File 'lib/gollum-lib/page.rb', line 96 def filename @blob && @blob.name end |
#filename_stripped ⇒ Object
Public: The on-disk filename of the page with extension stripped.
Returns the String name.
103 104 105 |
# File 'lib/gollum-lib/page.rb', line 103 def filename_stripped self.class.strip_filename(filename) end |
#find(name, version, dir = nil, exact = false) ⇒ 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.
372 373 374 375 376 377 378 379 380 381 |
# File 'lib/gollum-lib/page.rb', line 372 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) ⇒ 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.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/gollum-lib/page.rb', line 391 def find_page_in_tree(map, name, checked_dir = nil, exact = false) return nil if !map || name.to_s.empty? checked_dir = BlobEntry.normalize_dir(checked_dir) checked_dir = '' if exact && checked_dir.nil? name = ::File.join(checked_dir, name) if checked_dir map.each do |entry| next if entry.name.to_s.empty? path = checked_dir ? ::File.join(entry.dir, entry.name) : entry.name next unless page_match(name, path) return entry.page(@wiki, @version) end return nil # nothing was found end |
#find_sub_page(name) ⇒ Object
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.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# 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 |
#footer ⇒ Object
Public: The footer Page.
Returns the footer Page or nil if none exists.
294 295 296 |
# File 'lib/gollum-lib/page.rb', line 294 def @footer ||= find_sub_page(:footer) end |
#format ⇒ Object
Public: The format of the page.
Returns the Symbol format of the page; one of the registered format types
240 241 242 |
# File 'lib/gollum-lib/page.rb', line 240 def format self.class.format_for(@blob.name) end |
#formatted_data(encoding = nil, &block) ⇒ Object
Public: The formatted contents of the page.
encoding - Encoding Constant or String.
Returns the String data.
211 212 213 214 215 216 |
# File 'lib/gollum-lib/page.rb', line 211 def formatted_data(encoding = nil, &block) @blob && markup_class.render(historical?, encoding) do |doc| @doc = doc yield doc if block_given? end end |
#header ⇒ Object
Public: The header Page.
Returns the header Page or nil if none exists.
287 288 289 |
# File 'lib/gollum-lib/page.rb', line 287 def header @header ||= find_sub_page(: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.
310 311 312 |
# File 'lib/gollum-lib/page.rb', line 310 def historical? !!@historical end |
#inspect ⇒ Object
480 481 482 |
# File 'lib/gollum-lib/page.rb', line 480 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.
247 248 249 |
# File 'lib/gollum-lib/page.rb', line 247 def markup_class @markup_class ||= @wiki.markup_classes[format].new(self) end |
#metadata ⇒ Object
Public: Embedded metadata.
Returns Hash of metadata.
232 233 234 235 |
# File 'lib/gollum-lib/page.rb', line 232 def () formatted_data if markup_class. == nil markup_class. end |
#metadata_title ⇒ Object
Public: Metadata title
Set with <!– — title: New Title –> in page content
Returns the String title or nil if not defined
163 164 165 166 167 168 169 170 |
# File 'lib/gollum-lib/page.rb', line 163 def if title = ['title'] return title unless title.nil? end nil end |
#name ⇒ Object
Public: The canonical page name without extension, and dashes converted to spaces.
Returns the String name.
111 112 113 |
# File 'lib/gollum-lib/page.rb', line 111 def name self.class.canonicalize_filename(filename) end |
#page_match(name, path) ⇒ Object
Compare the canonicalized versions of the two names.
name - The human or canonical String page name. path - the String path on disk (including file extension).
Returns a Boolean.
440 441 442 443 444 445 446 447 |
# File 'lib/gollum-lib/page.rb', line 440 def page_match(name, path) if match = self.class.valid_filename?(path) @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) ⇒ 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.
414 415 416 417 418 |
# 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 ⇒ Object
Public: The raw contents of the page.
Returns the String data.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gollum-lib/page.rb', line 182 def raw_data return nil unless @blob if !@wiki.repo. && @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 ⇒ Object
Public: The sidebar Page.
Returns the sidebar Page or nil if none exists.
301 302 303 |
# File 'lib/gollum-lib/page.rb', line 301 def @sidebar ||= find_sub_page(:sidebar) end |
#sub_page ⇒ Object
Public: Determines if this is a sub-page Sub-pages have filenames beginning with an underscore
Returns true or false.
128 129 130 |
# File 'lib/gollum-lib/page.rb', line 128 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.
198 199 200 201 202 203 204 |
# File 'lib/gollum-lib/page.rb', line 198 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: The title will be constructed from the filename by stripping the extension and replacing any dashes with spaces.
Returns the fully sanitized String title.
120 121 122 |
# File 'lib/gollum-lib/page.rb', line 120 def title Sanitize.clean(name).strip end |
#toc_data ⇒ Object
Public: The table of contents of the page.
formatted_data - page already marked up in html.
Returns the String data.
223 224 225 226 227 |
# File 'lib/gollum-lib/page.rb', line 223 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) ⇒ 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.
426 427 428 429 430 431 432 |
# 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 ⇒ Object
Public: The url path required to reach this page within the repo.
Returns the String url_path
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gollum-lib/page.rb', line 140 def url_path path = if self.path.include?('/') self.path.sub(/\/[^\/]+$/, '/') else '' end path << Page.cname(self.name, '-', '-') path end |
#url_path_title ⇒ Object
Public: Defines title for page.rb
Returns the String title
154 155 156 |
# File 'lib/gollum-lib/page.rb', line 154 def url_path_title || url_path.gsub("-", " ") end |
#version_short ⇒ Object
Public: The first 7 characters of the current version.
Returns the first 7 characters of the current version.
280 281 282 |
# File 'lib/gollum-lib/page.rb', line 280 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 - 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.
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/gollum-lib/page.rb', line 265 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 |