Class: Gollum::Page
- Includes:
- Pagination
- Defined in:
- lib/gollum-lib/page.rb
Direct Known Subclasses
Constant Summary collapse
- SUBPAGENAMES =
[:header, :footer, :sidebar]
Constants inherited from File
Instance Attribute Summary collapse
-
#parent_page ⇒ Object
Parent page if this is a sub page.
-
#wiki ⇒ Object
readonly
The underlying wiki repo.
Attributes inherited from File
Class Method Summary collapse
-
.format_for(filename) ⇒ Object
Public: The format of a given filename.
-
.format_to_ext(format) ⇒ Object
Convert a format Symbol into an extension String.
- .global_find(wiki, version, query, try_on_disk) ⇒ Object
-
.global_path_match(query, entry, hyphened_tags = false, case_insensitive = false) ⇒ Object
For use with self.global_find: returns true if the given query corresponds to the in-repo path of the BlobEntry.
- .path_compare(query, match_path, hyphened_tags, case_insensitive) ⇒ Object
-
.strip_filename(filename) ⇒ Object
Reusable filter to strip extension and path from filename.
-
.valid_extension?(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
-
#display_metadata? ⇒ Boolean
Public: Whether or not to display the metadata.
-
#filename_stripped ⇒ Object
Public: The on-disk filename of the page with extension stripped.
-
#find_sub_pages(subpagenames = SUBPAGENAMES, map = nil) ⇒ Object
Loads sub pages.
-
#footer ⇒ Object
Public: The footer Page.
-
#format ⇒ Object
Public: The format of the page.
-
#formatted_data(encoding = nil, include_levels = 10, &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, blob, path, version, try_on_disk = false) ⇒ Page
constructor
Public: Initialize a Page.
- #inspect ⇒ Object
-
#last_version ⇒ Object
Public: The last version that has touched the Page.
-
#markup ⇒ 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.
-
#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.
-
#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_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
Methods inherited from File
canonical_path, #escaped_url_path, #filename, find, #mime_type, #on_disk?, #on_disk_path, 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.
95 96 97 98 99 100 101 |
# File 'lib/gollum-lib/page.rb', line 95 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_page ⇒ Object
Parent page if this is a sub page
Returns a Page
106 107 108 |
# File 'lib/gollum-lib/page.rb', line 106 def parent_page @parent_page end |
#wiki ⇒ Object (readonly)
The underlying wiki repo.
Returns the Gollum::Wiki containing the page.
303 304 305 |
# File 'lib/gollum-lib/page.rb', line 303 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
69 70 71 72 73 74 75 |
# File 'lib/gollum-lib/page.rb', line 69 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).
296 297 298 |
# File 'lib/gollum-lib/page.rb', line 296 def self.format_to_ext(format) Gollum::Markup.formats[format] ? Gollum::Markup.formats[format][:extensions].first : nil end |
.global_find(wiki, version, query, try_on_disk) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gollum-lib/page.rb', line 24 def global_find(wiki, version, query, try_on_disk) map = wiki.tree_map_for(version.to_s) begin entry = map.detect do |entry| global_path_match(query, entry, wiki.hyphened_tag_lookup, wiki.case_insensitive_tag_lookup) end entry ? self.new(wiki, entry.blob(wiki.repo), entry.dir, version, try_on_disk) : nil rescue Gollum::Git::NoSuchShaFound nil end end |
.global_path_match(query, entry, hyphened_tags = false, case_insensitive = false) ⇒ Object
For use with self.global_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.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/gollum-lib/page.rb', line 13 def global_path_match(query, entry, = false, case_insensitive = false) return false if "#{entry.name}".empty? return false unless valid_extension?(entry.name) match_path = Pathname.new('/').join(*[ entry.dir, entry.name ].compact ).to_s path_compare(query, match_path, , case_insensitive) end |
.path_compare(query, match_path, hyphened_tags, case_insensitive) ⇒ Object
36 37 38 39 40 |
# File 'lib/gollum-lib/page.rb', line 36 def path_compare(query, match_path, , case_insensitive) return false unless valid_extension?(match_path) cmp = valid_extension?(query) ? match_path : strip_filename(match_path) super(query, cmp, , 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.
82 83 84 |
# File 'lib/gollum-lib/page.rb', line 82 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.
49 50 51 |
# File 'lib/gollum-lib/page.rb', line 49 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.
59 60 61 62 |
# File 'lib/gollum-lib/page.rb', line 59 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
155 156 157 158 159 |
# File 'lib/gollum-lib/page.rb', line 155 def return false if (.keys - ['title', 'header_enum']).empty? return false if ['display_metadata'] == false @wiki. end |
#filename_stripped ⇒ Object
Public: The on-disk filename of the page with extension stripped.
Returns the String name.
111 112 113 |
# File 'lib/gollum-lib/page.rb', line 111 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.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/gollum-lib/page.rb', line 323 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 |
#footer ⇒ Object
Public: The footer Page.
Returns the footer Page or nil if none exists.
269 270 271 272 |
# File 'lib/gollum-lib/page.rb', line 269 def find_sub_pages unless defined?(@footer) @footer end |
#format ⇒ Object
Public: The format of the page.
Returns the Symbol format of the page; one of the registered format types
220 221 222 |
# File 'lib/gollum-lib/page.rb', line 220 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.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/gollum-lib/page.rb', line 179 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 ⇒ Object
Public: The header Page.
Returns the header Page or nil if none exists.
261 262 263 264 |
# File 'lib/gollum-lib/page.rb', line 261 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.
287 288 289 |
# File 'lib/gollum-lib/page.rb', line 287 def historical? !!@historical end |
#inspect ⇒ Object
357 358 359 |
# File 'lib/gollum-lib/page.rb', line 357 def inspect %(#<#{self.class.name}:#{object_id} #{name} (#{format}) @wiki=#{@wiki.repo.path.inspect}>) end |
#last_version ⇒ Object
Public: The last version that has touched the Page. Can be nil.
Returns Gollum::Git::Commit, or nil.
246 247 248 249 |
# File 'lib/gollum-lib/page.rb', line 246 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 ⇒ Object
Gets the Gollum::Markup instance that will render this page’s content.
Returns a Gollum::Markup instance.
227 228 229 |
# File 'lib/gollum-lib/page.rb', line 227 def markup @markup ||= ::Gollum::Markup.new(self) end |
#metadata ⇒ Object
Public: Embedded metadata.
Returns Hash of metadata.
208 209 210 211 212 213 214 215 |
# File 'lib/gollum-lib/page.rb', line 208 def unless @metadata formatted_data if markup. == nil @metadata = @wiki..merge(markup. || {}) else @metadata end end |
#metadata_title ⇒ Object
Public: Metadata title
Set with <!– — title: New Title –> in page content
Returns the String title or nil if not defined
150 151 152 |
# File 'lib/gollum-lib/page.rb', line 150 def ? ['title'] : nil end |
#name ⇒ Object
Public: The canonical page name without extension.
Returns the String name.
118 119 120 |
# File 'lib/gollum-lib/page.rb', line 118 def name self.class.strip_filename(filename) end |
#sidebar ⇒ Object
Public: The sidebar Page.
Returns the sidebar Page or nil if none exists.
277 278 279 280 |
# File 'lib/gollum-lib/page.rb', line 277 def find_sub_pages unless defined?(@sidebar) @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.
134 135 136 |
# File 'lib/gollum-lib/page.rb', line 134 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.
166 167 168 169 170 171 172 |
# File 'lib/gollum-lib/page.rb', line 166 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.
Returns the fully sanitized String title.
126 127 128 |
# File 'lib/gollum-lib/page.rb', line 126 def title @wiki.sanitizer.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.
199 200 201 202 203 |
# File 'lib/gollum-lib/page.rb', line 199 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.
311 312 313 314 315 316 317 |
# File 'lib/gollum-lib/page.rb', line 311 def tree_path(treemap, tree) if (ptree = treemap[tree]) "#{tree_path(treemap, ptree)}#{::File::SEPARATOR}#{tree.name}" else '' end end |
#url_path_title ⇒ Object
Public: Defines title for page.rb
Returns the String title
141 142 143 |
# File 'lib/gollum-lib/page.rb', line 141 def url_path_title || name end |
#version_short ⇒ Object
Public: The first 7 characters of the current version.
Returns the first 7 characters of the current version.
254 255 256 |
# File 'lib/gollum-lib/page.rb', line 254 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.
239 240 241 |
# File 'lib/gollum-lib/page.rb', line 239 def versions( = {}) @wiki.repo.git.versions_for_path(@path, @wiki.ref, ()) end |