start work on formatted_page
This commit is contained in:
@@ -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::Repo>
|
||||
gollum = Gollum::Wiki.new("my-gollum-repo.git")
|
||||
# => <Gollum::Wiki>
|
||||
|
||||
Get the latest HTML formatted version of the given canonical page name:
|
||||
|
||||
|
||||
+14
-1
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
+5
-1
@@ -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
|
||||
|
||||
+8
-2
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user