lib/gollum/page.rb in gollum-1.0.1 vs lib/gollum/page.rb in gollum-1.1.0

- old
+ new

@@ -12,10 +12,15 @@ :creole => "Creole", :rest => "reStructuredText", :asciidoc => "AsciiDoc", :pod => "Pod" } + # Sets a Boolean determing whether this page is a historical version. + # + # Returns nothing. + attr_writer :historical + # 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. @@ -33,10 +38,18 @@ def self.valid_page_name?(filename) match = valid_filename?(filename) filename =~ /^_/ ? false : match 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) + filename.split('.')[0..-2].join('.').gsub('-', ' ') + end + # Public: Initialize a page. # # wiki - The Gollum::Wiki in question. # # Returns a newly initialized Gollum::Page. @@ -55,11 +68,11 @@ # Public: The canonical page name without extension, and dashes converted # to spaces. # # Returns the String name. def name - filename.split('.')[0..-2].join('.').gsub('-', ' ') + self.class.canonicalize_filename(filename) end # 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 @@ -105,11 +118,11 @@ # Public: The formatted contents of the page. # # Returns the String data. def formatted_data - @blob && Gollum::Markup.new(self).render + @blob && Gollum::Markup.new(self).render(historical?) end # Public: The format of the page. # # Returns the Symbol format of the page. One of: @@ -172,24 +185,28 @@ def footer return nil if page_match('_Footer', self.filename) dirs = self.path.split('/') dirs.pop + map = @wiki.tree_map_for(self.version.id) while !dirs.empty? - tree = self.version.tree / dirs.join('/') - if page = find_page_in_this_tree(tree, dirs.join('/'), '_Footer') + if page = find_page_in_tree(map, '_Footer', dirs.join('/')) return page end dirs.pop end - tree = self.version.tree - if page = find_page_in_this_tree(tree, '', '_Footer') - return page - end + find_page_in_tree(map, '_Footer', '') + end - return nil + # 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? + !!@historical end ######################################################################### # # Class Methods @@ -205,11 +222,13 @@ # Page.cname("Bilbo Baggins") # # => 'Bilbo-Baggins' # # Returns the String canonical name. def self.cname(name) - name.gsub(%r{[ /<>]}, '-') + name.respond_to?(:gsub) ? + name.gsub(%r{[ /<>]}, '-') : + '' end # Convert a format Symbol into an extension String. # # format - The format Symbol. @@ -249,64 +268,38 @@ # 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) - if commit = @wiki.repo.commit(version) - if page = find_page_in_tree(commit.tree, name) - page.version = commit - page - end + map = @wiki.tree_map_for(version) + 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 end + rescue Grit::GitRuby::Repository::NoSuchShaFound end # Find a page in a given tree. # - # tree - The Grit::Tree in which to look. - # name - The canonical String page name. + # 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. - def find_page_in_tree(tree, name) - treemap = {} - trees = [tree] - - while !trees.empty? - ptree = trees.shift - ptree.contents.each do |item| - case item - when Grit::Blob - if page_match(name, item.name) - return self.class.new(@wiki).populate(item, tree_path(treemap, ptree)) - end - when Grit::Tree - treemap[item] = ptree - trees << item - end - end + def find_page_in_tree(map, name, checked_dir = nil) + return nil if name.to_s.empty? + if checked_dir = BlobEntry.normalize_dir(checked_dir) + checked_dir.downcase! end - return nil # nothing was found - end - - # Find a page in a given tree without recursing into subtrees. - # - # tree - The Grit::Tree in which to look. - # dir - The String path of the given Grit::Tree. - # name - The canonical String page name. - # - # Returns a Gollum::Page or nil if the page could not be found. - def find_page_in_this_tree(tree, dir, name) - treemap = {} - tree.contents.each do |item| - case item - when Grit::Blob - if page_match(name, item.name) - path = dir == '' ? '' : ::File.join('/', dir) - page = self.class.new(@wiki).populate(item, path) - page.version = self.version - return page - end - 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 \ No newline at end of file