lib/gollum/page.rb in gollum-1.1.1 vs lib/gollum/page.rb in gollum-1.2.0
- old
+ new
@@ -2,19 +2,20 @@
class Page
include Pagination
Wiki.page_class = self
- VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|creole|re?st(\.txt)?|asciidoc|pod)$/i
- FORMAT_NAMES = { :markdown => "Markdown",
- :textile => "Textile",
- :rdoc => "RDoc",
- :org => "Org-mode",
- :creole => "Creole",
- :rest => "reStructuredText",
- :asciidoc => "AsciiDoc",
- :pod => "Pod" }
+ 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" }
# Sets a Boolean determing whether this page is a historical version.
#
# Returns nothing.
attr_writer :historical
@@ -38,10 +39,44 @@
def self.valid_page_name?(filename)
match = valid_filename?(filename)
filename =~ /^_/ ? false : match
end
+ # 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 ]
+ 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
+
# Reusable filter to turn a filename (without path) into a canonical name.
# Strips extension, converts spaces to dashes.
#
# Returns the filtered String.
def self.canonicalize_filename(filename)
@@ -53,21 +88,21 @@
# wiki - The Gollum::Wiki in question.
#
# Returns a newly initialized Gollum::Page.
def initialize(wiki)
@wiki = wiki
- @blob = nil
+ @blob = @footer = @sidebar = nil
end
# Public: The on-disk filename of the page including extension.
#
# Returns the String name.
def filename
@blob && @blob.name
end
- # Public: The canonical page name without extension, and dashes converted
+ # Public: The canonical page name without extension, and dashes converted
# to spaces.
#
# Returns the String name.
def name
self.class.canonicalize_filename(filename)
@@ -99,11 +134,11 @@
if !header.empty?
Sanitize.clean(header.to_html)
else
Sanitize.clean(name)
- end
+ end.strip
end
# Public: The path of the page within the repo.
#
# Returns the String path.
@@ -114,45 +149,37 @@
# Returns the String data.
def raw_data
@blob && @blob.data
end
+ # Public: A text data encoded in specified encoding.
+ #
+ # encoding - An Encoding or nil
+ #
+ # Returns a character encoding aware String.
+ def text_data(encoding=nil)
+ if raw_data.respond_to?(:encoding)
+ raw_data.force_encoding(encoding || Encoding::UTF_8)
+ else
+ raw_data
+ end
+ end
+
# Public: The formatted contents of the page.
#
# Returns the String data.
- def formatted_data
- @blob && Gollum::Markup.new(self).render(historical?)
+ def formatted_data(&block)
+ @blob && @wiki.markup_class.new(self).render(historical?, &block)
end
# Public: The format of the page.
#
# Returns the Symbol format of the page. One of:
# [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
# :roff ]
def format
- case @blob.name
- 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
- else
- nil
- end
+ self.class.format_for(@blob.name)
end
# Public: The current version of the page.
#
# Returns the Grit::Commit.
@@ -161,11 +188,11 @@
# 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
+ # :follow - Follow's a file across renames, but falls back
# to a slower Grit native call. (default: false)
#
# Returns an Array of Grit::Commit.
def versions(options = {})
if options[:follow]
@@ -181,26 +208,21 @@
# Public: The footer Page.
#
# Returns the footer Page or nil if none exists.
def footer
- return nil if page_match('_Footer', self.filename)
+ @footer ||= find_sub_page(:footer)
+ end
- dirs = self.path.split('/')
- dirs.pop
- map = @wiki.tree_map_for(self.version.id)
- while !dirs.empty?
- if page = find_page_in_tree(map, '_Footer', dirs.join('/'))
- return page
- end
- dirs.pop
- end
-
- find_page_in_tree(map, '_Footer', '')
+ # Public: The sidebar Page.
+ #
+ # Returns the sidebar Page or nil if none exists.
+ def sidebar
+ @sidebar ||= find_sub_page(:sidebar)
end
- # Gets a Boolean determining whether this page is a historical version.
+ # 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.
def historical?
@@ -234,18 +256,19 @@
# format - The format Symbol.
#
# Returns the String extension (no leading period).
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 :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
#########################################################################
#
@@ -268,30 +291,30 @@
# 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.
def find(name, version)
- map = @wiki.tree_map_for(version)
+ map = @wiki.tree_map_for(version.to_s)
if page = find_page_in_tree(map, name)
- sha = @wiki.ref_map[version] || version
- page.version = Grit::Commit.create(@wiki.repo, :id => sha)
- page.historical = sha == version
+ 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 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
+ # to be in. The string should
#
# Returns a Gollum::Page or nil if the page could not be found.
def find_page_in_tree(map, name, checked_dir = nil)
- return nil if name.to_s.empty?
+ return nil if !map || name.to_s.empty?
if checked_dir = BlobEntry.normalize_dir(checked_dir)
checked_dir.downcase!
end
map.each do |entry|
@@ -308,13 +331,13 @@
#
# blob - The Grit::Blob that contains the info.
# path - The String directory path of the page file.
#
# Returns the populated Gollum::Page.
- def populate(blob, path)
+ def populate(blob, path=nil)
@blob = blob
- @path = (path + '/' + blob.name)[1..-1]
+ @path = "#{path}/#{blob.name}"[1..-1]
self
end
# The full directory path for the given tree.
#
@@ -341,7 +364,31 @@
Page.cname(name).downcase == Page.cname(match).downcase
else
false
end
end
+
+ # 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.
+ def find_sub_page(name)
+ 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
end
-end
\ No newline at end of file
+end