diff --git a/README.md b/README.md index 90c0039c..5f1a34fa 100644 --- a/README.md +++ b/README.md @@ -225,10 +225,10 @@ Initialize the Gollum::Repo object: # Require the Gollum library require 'gollum' - # Create a new Gollum object by initializing it with the path to the + # Create a new Gollum::Wiki object by initializing it with the path to the # Git repository. - gollum = Gollum::Repo.new("my-gollum-repo.git") - # => + gollum = Gollum::Wiki.new("my-gollum-repo.git") + # => Get the latest HTML formatted version of the given canonical page name: diff --git a/lib/gollum.rb b/lib/gollum.rb index df503c09..586abbfd 100644 --- a/lib/gollum.rb +++ b/lib/gollum.rb @@ -2,9 +2,22 @@ require 'grit' # internal -require 'gollum/repo' +require 'gollum/wiki' require 'gollum/page' module Gollum VERSION = '0.0.1' + + # Convert a human page name into a canonical page name. + # + # name - The String human page name. + # + # Examples + # Gollum.canonical_name("Bilbo Baggins") + # # => 'Bilbo-Baggins' + # + # Returns the String canonical name. + def self.canonical_name(name) + name.gsub(/ /, '-').sub(/\.(.+?)$/, '') + end end \ No newline at end of file diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 61886fd7..7085f684 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -1,5 +1,79 @@ module Gollum class Page + attr_accessor :wiki, :data + # Initialize a page. + # + # wiki - The Gollum::Wiki in question. + # + # Returns a newly initialized Gollum::Page. + def initialize(wiki) + self.wiki = wiki + end + + # Populate this Page with information from the Blob. + # + # blob - The Grit::Blob that contains the info. + # + # Returns the populated Gollum::Page. + def populate(blob) + self.data = blob.data + self + end + + # Find a page in the given Gollum repo. + # + # name - The human or canonical String page name to find. + # + # Returns a Gollum::Page or nil if the page could not be found. + def find(name) + commit = self.wiki.repo.commits.first + content = find_page_in_tree(commit.tree, name) + end + + # private + + # Find a page in a given commit. + # + # commit - The Grit::Commit in which to look. + # name - The human or canonical String page name. + # + # Returns a Gollum::Page or nil if the page could not be found. + def find_page_in_commit(commit, name) + + end + + # Find a page in a given tree. + # + # tree - The Grit::Tree in which to look. + # name - The canonical String page name. + # + # Returns a Gollum::Page or nil if the page could not be found. + def find_page_in_tree(tree, name) + trees = [tree] + + while !trees.empty? + trees.shift.contents.each do |item| + case item + when Grit::Blob + return populate(item) if page_match(name, item.name) + when Grit::Tree + trees << item + end + end + end + + return nil # nothing was found + end + + # Compare the canonicalized versions of the two names. + # + # name1 - A human or canonical String page name. + # name2 - A human or canonical String page name. + # + # Returns a Boolean. + def page_match(name1, name2) + Gollum.canonical_name(name1) == Gollum.canonical_name(name2) + end end end \ No newline at end of file diff --git a/lib/gollum/repo.rb b/lib/gollum/wiki.rb similarity index 80% rename from lib/gollum/repo.rb rename to lib/gollum/wiki.rb index b7110e2f..24e2fa9e 100644 --- a/lib/gollum/repo.rb +++ b/lib/gollum/wiki.rb @@ -1,5 +1,5 @@ module Gollum - class Repo + class Wiki attr_accessor :path, :repo # Initialize a new Gollum Repo. @@ -14,11 +14,11 @@ module Gollum # Get the formatted page for a given page name. # - # name - The String name of the wiki page. + # name - The human or canonical String page name of the wiki page. # # Returns a Gollum::Page or nil if no matching page was found. def formatted_page(name) - Page.new() + Page.new(self).find(name) end end end \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index 75f1bcd5..c6deb8c3 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -11,7 +11,11 @@ require 'gollum' # Make sure we're in the test dir, the tests expect that to be the current # directory. -Dir.chdir(File.join(File.dirname(__FILE__), *%w[.])) +TEST_DIR = File.join(File.dirname(__FILE__), *%w[.]) + +def testpath(path) + File.join(TEST_DIR, path) +end # test/spec/mini 3 # http://gist.github.com/25455 diff --git a/test/test_page.rb b/test/test_page.rb index cc602408..1db3576d 100644 --- a/test/test_page.rb +++ b/test/test_page.rb @@ -2,10 +2,16 @@ require 'helper' context "Page" do setup do - @repo = Gollum::Repo.new("examples/lotr.git") + @wiki = Gollum::Wiki.new(testpath("examples/lotr.git")) end test "formatted page" do - page = @repo.formatted_page('Bilbo-Baggins') + page = @wiki.formatted_page('Bilbo Baggins') + assert_equal Gollum::Page, page.class + assert page.data =~ /^# Bilbo Baggins\n\nBilbo Baggins/ + end + + test "no page match" do + assert_nil @wiki.formatted_page('I do not exist') end end \ No newline at end of file diff --git a/test/test_repo.rb b/test/test_wiki.rb similarity index 50% rename from test/test_repo.rb rename to test/test_wiki.rb index 04d03d81..e9df6120 100644 --- a/test/test_repo.rb +++ b/test/test_wiki.rb @@ -1,12 +1,12 @@ require 'helper' -context "Repo" do +context "Wiki" do setup do - @repo = Gollum::Repo.new("examples/lotr.git") + @repo = Gollum::Wiki.new(testpath("examples/lotr.git")) end test "repo path" do - assert_equal "examples/lotr.git", @repo.path + assert_equal testpath("examples/lotr.git"), @repo.path end test "git repo" do