properly define the public api

This commit is contained in:
Tom Preston-Werner
2010-04-17 16:41:05 -04:00
parent 78a9396421
commit 9037b06e62
3 changed files with 47 additions and 26 deletions
+9 -7
View File
@@ -1,23 +1,25 @@
module Gollum module Gollum
class File class File
attr_accessor :wiki, :blob, :version
# Initialize a file. # Initialize a file.
# #
# wiki - The Gollum::Wiki in question. # wiki - The Gollum::Wiki in question.
# #
# Returns a newly initialized Gollum::File. # Returns a newly initialized Gollum::File.
def initialize(wiki) def initialize(wiki)
self.wiki = wiki @wiki = wiki
@blob = nil
end end
# The raw contents of the page. # The raw contents of the page.
# #
# Returns the String data. # Returns the String data.
def raw_data def raw_data
self.blob.data rescue nil @blob.data rescue nil
end end
# The Grit::Commit version of the file.
attr_reader :version
######################################################################### #########################################################################
# #
# Private # Private
@@ -31,10 +33,10 @@ module Gollum
# #
# Returns a Gollum::File or nil if the file could not be found. # Returns a Gollum::File or nil if the file could not be found.
def find(name, version) def find(name, version)
commit = self.wiki.repo.commit(version) commit = @wiki.repo.commit(version)
if blob = commit.tree / name if blob = commit.tree / name
self.blob = blob @blob = blob
self.version = commit @version = commit
self self
else else
nil nil
+19 -11
View File
@@ -2,36 +2,38 @@ module Gollum
class Page class Page
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|re?st(\.txt)?|asciidoc|pod|\d)$/i
attr_accessor :wiki, :blob, :path, :version
# Initialize a page. # Initialize a page.
# #
# wiki - The Gollum::Wiki in question. # wiki - The Gollum::Wiki in question.
# #
# Returns a newly initialized Gollum::Page. # Returns a newly initialized Gollum::Page.
def initialize(wiki) def initialize(wiki)
self.wiki = wiki @wiki = wiki
@blob = nil
end end
# The on-disk filename of the page. # The on-disk filename of the page.
# #
# Returns the String name. # Returns the String name.
def name def name
self.blob.name rescue nil @blob.name rescue nil
end end
# The String full path of the page.
attr_reader :path
# The raw contents of the page. # The raw contents of the page.
# #
# Returns the String data. # Returns the String data.
def raw_data def raw_data
self.blob.data rescue nil @blob.data rescue nil
end end
# The formatted contents of the page. # The formatted contents of the page.
# #
# Returns the String data. # Returns the String data.
def formatted_data def formatted_data
GitHub::Markup.render(self.blob.name, self.blob.data) rescue nil GitHub::Markup.render(@blob.name, @blob.data) rescue nil
end end
# The format of the page. # The format of the page.
@@ -40,7 +42,7 @@ module Gollum
# [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod | # [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
# :roff ] # :roff ]
def format def format
case blob.name case @blob.name
when /\.(md|mkdn?|mdown|markdown)$/i when /\.(md|mkdn?|mdown|markdown)$/i
:markdown :markdown
when /\.(textile)$/i when /\.(textile)$/i
@@ -62,11 +64,14 @@ module Gollum
end end
end end
# The Grit::Commit version of the page.
attr_reader :version
# All of the versions that have touched this Page. # All of the versions that have touched this Page.
# #
# Returns an Array of Grit::Commit. # Returns an Array of Grit::Commit.
def versions def versions
@wiki.repo.log('master', self.path) @wiki.repo.log('master', @path)
end end
######################################################################### #########################################################################
@@ -75,6 +80,9 @@ module Gollum
# #
######################################################################### #########################################################################
# Private: The Grit::Commit version of the page.
attr_writer :version
# Private: Convert a format Symbol into an extension String. # Private: Convert a format Symbol into an extension String.
# #
# format - The format Symbol. # format - The format Symbol.
@@ -99,7 +107,7 @@ module Gollum
# #
# Returns a Gollum::Page or nil if the page could not be found. # Returns a Gollum::Page or nil if the page could not be found.
def find(name, version) def find(name, version)
commit = self.wiki.repo.commit(version) commit = @wiki.repo.commit(version)
if page = find_page_in_tree(commit.tree, name) if page = find_page_in_tree(commit.tree, name)
page.version = commit page.version = commit
page page
@@ -143,8 +151,8 @@ module Gollum
# #
# Returns the populated Gollum::Page. # Returns the populated Gollum::Page.
def populate(blob, path) def populate(blob, path)
self.blob = blob @blob = blob
self.path = (path + '/' + blob.name)[1..-1] @path = (path + '/' + blob.name)[1..-1]
self self
end end
+19 -8
View File
@@ -1,15 +1,13 @@
module Gollum module Gollum
class Wiki class Wiki
attr_accessor :path, :repo
# Initialize a new Gollum Repo. # Initialize a new Gollum Repo.
# #
# repo - The String path to the Git repository that holds the Gollum site. # repo - The String path to the Git repository that holds the Gollum site.
# #
# Returns a fresh Gollum::Repo. # Returns a fresh Gollum::Repo.
def initialize(path) def initialize(path)
self.path = path @path = path
self.repo = Grit::Repo.new(path) @repo = Grit::Repo.new(path)
end end
# Get the formatted page for a given page name. # Get the formatted page for a given page name.
@@ -45,10 +43,10 @@ module Gollum
def write_page(name, format, data, commit = {}) def write_page(name, format, data, commit = {})
ext = Page.format_to_ext(format) ext = Page.format_to_ext(format)
path = Gollum.canonical_name(name) + '.' + ext path = Gollum.canonical_name(name) + '.' + ext
index = self.repo.index index = @repo.index
index.add(path, data) index.add(path, data)
actor = Grit::Actor.new(commit[:name], commit[:email]) actor = Grit::Actor.new(commit[:name], commit[:email])
parent = self.repo.commit('master') parent = @repo.commit('master')
index.commit(commit[:message], [parent], actor) index.commit(commit[:message], [parent], actor)
end end
@@ -64,11 +62,24 @@ module Gollum
# #
# Returns the String SHA1 of the newly written version. # Returns the String SHA1 of the newly written version.
def update_page(page, data, commit = {}) def update_page(page, data, commit = {})
index = self.repo.index index = @repo.index
index.add(page.path, data) index.add(page.path, data)
actor = Grit::Actor.new(commit[:name], commit[:email]) actor = Grit::Actor.new(commit[:name], commit[:email])
parent = self.repo.commit('master') parent = @repo.commit('master')
index.commit(commit[:message], [parent], actor) index.commit(commit[:message], [parent], actor)
end end
#########################################################################
#
# Private
#
#########################################################################
# Private: The Grit::Repo associated with this wiki.
attr_reader :repo
# Private: The String path to the Git repository that holds the Gollum
# site.
attr_reader :path
end end
end end