compute directory path of pages

This commit is contained in:
Tom Preston-Werner
2010-04-11 12:03:00 -06:00
parent 6c1b65158a
commit e3772b1879
8 changed files with 52 additions and 6 deletions
+25 -4
View File
@@ -2,7 +2,7 @@ module Gollum
class Page
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i
attr_accessor :wiki, :blob, :version
attr_accessor :wiki, :blob, :path, :version
# Initialize a page.
#
@@ -16,10 +16,12 @@ module Gollum
# Populate this 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.
def populate(blob)
def populate(blob, path)
self.blob = blob
self.path = (path + '/' + blob.name)[1..-1]
self
end
@@ -96,14 +98,19 @@ module Gollum
#
# 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?
trees.shift.contents.each do |item|
ptree = trees.shift
ptree.contents.each do |item|
case item
when Grit::Blob
return populate(item) if page_match(name, item.name)
if page_match(name, item.name)
return populate(item, tree_path(treemap, ptree))
end
when Grit::Tree
treemap[item] = ptree
trees << item
end
end
@@ -112,6 +119,20 @@ module Gollum
return nil # nothing was found
end
# 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.
def tree_path(treemap, tree)
if ptree = treemap[tree]
tree_path(treemap, ptree) + '/' + tree.name
else
''
end
end
# Compare the canonicalized versions of the two names.
#
# name - The human or canonical String page name.
+16
View File
@@ -0,0 +1,16 @@
module Gollum
class Version
attr_accessor :commit
def initialize(commit)
self.commit = commit
end
# The SHA1 commit ID.
#
# The String ID.
def id
self.commit.id
end
end
end
@@ -0,0 +1,3 @@
xŽAjB1@»Î)æJ&Ñ™"nÜ \'™‰m!$ÆEo߆nïÁ«Öû÷„°ã9TA0S+I˜¨*i.A«Ï©qh"Ã>bLâžyè2AZ %TöŒH-¡–Jå÷ZI˜
ëêÎå÷ü²7ëpúš¶lî:pœÖÏÝ~¬ÛVë'ÀÀè}ÂaãÉ{·Òõr®òÿz—Eà™
m}¸ü*XƒÏü¶¸?øçOi
+1 -1
View File
@@ -1 +1 @@
df26e61e707116f81ebc6b935ec6d1676b7e96c4
fbabba862dfa7ac35b39042dd4ad780c9f67b8cb
+7 -1
View File
@@ -16,8 +16,14 @@ context "Page" do
assert_equal Gollum::Page, page.class
assert page.raw_data =~ /^# Bilbo Baggins\n\nBilbo Baggins/
assert page.formatted_data =~ /<h1>Bilbo Baggins<\/h1>\n\n<p>Bilbo Baggins/
assert_equal 'Bilbo-Baggins.md', page.path
assert_equal :markdown, page.format
assert_equal 'df26e61e707116f81ebc6b935ec6d1676b7e96c4', page.version.id
assert_equal 'fbabba862dfa7ac35b39042dd4ad780c9f67b8cb', page.version.id
end
test "get nested page" do
page = @wiki.page('Eye Of Sauron')
assert_equal 'Mordor/Eye-Of-Sauron.md', page.path
end
test "no page match" do