diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 4cdeab15..f92f2207 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -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. diff --git a/lib/gollum/version.rb b/lib/gollum/version.rb new file mode 100644 index 00000000..8620d25b --- /dev/null +++ b/lib/gollum/version.rb @@ -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 \ No newline at end of file diff --git a/test/examples/lotr.git/objects/0e/eab62a59300666b4093cf2cfa196c1fedd0e71 b/test/examples/lotr.git/objects/0e/eab62a59300666b4093cf2cfa196c1fedd0e71 new file mode 100644 index 00000000..cb608cb8 Binary files /dev/null and b/test/examples/lotr.git/objects/0e/eab62a59300666b4093cf2cfa196c1fedd0e71 differ diff --git a/test/examples/lotr.git/objects/13/304aef8994111be14b5168e5d09bc090e9e5c7 b/test/examples/lotr.git/objects/13/304aef8994111be14b5168e5d09bc090e9e5c7 new file mode 100644 index 00000000..bb0d2154 Binary files /dev/null and b/test/examples/lotr.git/objects/13/304aef8994111be14b5168e5d09bc090e9e5c7 differ diff --git a/test/examples/lotr.git/objects/d1/a6fb8d766ce6eab2ec0a8f72fdd3171253138d b/test/examples/lotr.git/objects/d1/a6fb8d766ce6eab2ec0a8f72fdd3171253138d new file mode 100644 index 00000000..e423efab Binary files /dev/null and b/test/examples/lotr.git/objects/d1/a6fb8d766ce6eab2ec0a8f72fdd3171253138d differ diff --git a/test/examples/lotr.git/objects/fb/abba862dfa7ac35b39042dd4ad780c9f67b8cb b/test/examples/lotr.git/objects/fb/abba862dfa7ac35b39042dd4ad780c9f67b8cb new file mode 100644 index 00000000..53393848 --- /dev/null +++ b/test/examples/lotr.git/objects/fb/abba862dfa7ac35b39042dd4ad780c9f67b8cb @@ -0,0 +1,3 @@ +xAjB1@)J&љ"n \'m!$Eon9TA0S+I*i.Aϩqh">bLy2AZ %TH-JZI +7pl:p~V'}a{rzE +m}*X?Oi \ No newline at end of file diff --git a/test/examples/lotr.git/refs/heads/master b/test/examples/lotr.git/refs/heads/master index 07f43ccb..d3d48f48 100644 --- a/test/examples/lotr.git/refs/heads/master +++ b/test/examples/lotr.git/refs/heads/master @@ -1 +1 @@ -df26e61e707116f81ebc6b935ec6d1676b7e96c4 +fbabba862dfa7ac35b39042dd4ad780c9f67b8cb diff --git a/test/test_page.rb b/test/test_page.rb index cf547b59..7e09b688 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -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 =~ /

Bilbo Baggins<\/h1>\n\n

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