implement Wiki#write_page
This commit is contained in:
@@ -280,20 +280,22 @@ Get a specific version of a given static file:
|
|||||||
|
|
||||||
gollum.file('asset.js', '5ec521178e0eec4dc39741a8978a2ba6616d0f0a')
|
gollum.file('asset.js', '5ec521178e0eec4dc39741a8978a2ba6616d0f0a')
|
||||||
|
|
||||||
Write a new version of a given canonical page file (the file will be created
|
Write a new version of a page (the file will be created if it does not already
|
||||||
if it does not already exist) and commit the change.
|
exist) and commit the change. The file will be written at the repo root.
|
||||||
|
|
||||||
gollum.write_page('page-name', 'My wiki page contents')
|
commit = { :message => 'commit message',
|
||||||
|
:name => 'Tom Preston-Werner',
|
||||||
|
:email => 'tom@github.com' }
|
||||||
|
gollum.write_page('Page Name', :markdown, 'Page contents', commit)
|
||||||
|
|
||||||
Write a new version of a given canonical page file and specify a commit
|
Update an existing page (keeps the same name, format, and directory location).
|
||||||
message:
|
|
||||||
|
|
||||||
gollum.write_page('page-name', 'My wiki page contents', 'commit message')
|
gollum.update_page(page, 'Page contents', commit)
|
||||||
|
|
||||||
To delete a page and commit the change:
|
To delete a page and commit the change:
|
||||||
|
|
||||||
gollum.delete_page('page-name')
|
gollum.delete_page('Page Name')
|
||||||
|
|
||||||
Once again, you can optionally specify a commit message:
|
Once again, you can optionally specify a commit message:
|
||||||
|
|
||||||
gollum.delete_page('page-name', 'commit message')
|
gollum.delete_page('Page Name', 'commit message')
|
||||||
@@ -4,6 +4,23 @@ module Gollum
|
|||||||
|
|
||||||
attr_accessor :wiki, :blob, :path, :version
|
attr_accessor :wiki, :blob, :path, :version
|
||||||
|
|
||||||
|
# Convert a format Symbol into an extension String.
|
||||||
|
#
|
||||||
|
# format - The format Symbol.
|
||||||
|
#
|
||||||
|
# Returns the String extension (no leading period).
|
||||||
|
def self.format_to_ext(format)
|
||||||
|
case format
|
||||||
|
when :markdown then 'md'
|
||||||
|
when :textile then 'textile'
|
||||||
|
when :rdoc then 'rdoc'
|
||||||
|
when :org then 'org'
|
||||||
|
when :rest then 'rest'
|
||||||
|
when :asciidoc then 'asciidoc'
|
||||||
|
when :pod then 'pod'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Initialize a page.
|
# Initialize a page.
|
||||||
#
|
#
|
||||||
# wiki - The Gollum::Wiki in question.
|
# wiki - The Gollum::Wiki in question.
|
||||||
|
|||||||
@@ -31,5 +31,24 @@ module Gollum
|
|||||||
def file(name, version = 'master')
|
def file(name, version = 'master')
|
||||||
File.new(self).find(name, version)
|
File.new(self).find(name, version)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Write a new version of a page to the Gollum repo root.
|
||||||
|
#
|
||||||
|
# name - The String name of the page.
|
||||||
|
# data - The new String contents of the page.
|
||||||
|
# commit - The commit Hash details { :message => String,
|
||||||
|
# :author => String,
|
||||||
|
# :email => String }
|
||||||
|
#
|
||||||
|
# Returns the String SHA1 of the newly written version.
|
||||||
|
def write_page(name, format, data, commit = {})
|
||||||
|
ext = Page.format_to_ext(format)
|
||||||
|
path = Gollum.canonical_name(name) + '.' + ext
|
||||||
|
index = self.repo.index
|
||||||
|
index.add(path, data)
|
||||||
|
actor = Grit::Actor.new(commit[:name], commit[:email])
|
||||||
|
parent = self.repo.commit('master')
|
||||||
|
index.commit(commit[:message], [parent], actor)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -2,6 +2,7 @@ require 'rubygems'
|
|||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'shoulda'
|
require 'shoulda'
|
||||||
require 'mocha'
|
require 'mocha'
|
||||||
|
require 'fileutils'
|
||||||
|
|
||||||
dir = File.dirname(File.expand_path(__FILE__))
|
dir = File.dirname(File.expand_path(__FILE__))
|
||||||
$LOAD_PATH.unshift(File.join(dir, '..', 'lib'))
|
$LOAD_PATH.unshift(File.join(dir, '..', 'lib'))
|
||||||
|
|||||||
+27
-3
@@ -2,14 +2,38 @@ require File.join(File.dirname(__FILE__), *%w[helper])
|
|||||||
|
|
||||||
context "Wiki" do
|
context "Wiki" do
|
||||||
setup do
|
setup do
|
||||||
@repo = Gollum::Wiki.new(testpath("examples/lotr.git"))
|
@wiki = Gollum::Wiki.new(testpath("examples/lotr.git"))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "repo path" do
|
test "repo path" do
|
||||||
assert_equal testpath("examples/lotr.git"), @repo.path
|
assert_equal testpath("examples/lotr.git"), @wiki.path
|
||||||
end
|
end
|
||||||
|
|
||||||
test "git repo" do
|
test "git repo" do
|
||||||
assert_equal Grit::Repo, @repo.repo.class
|
assert_equal Grit::Repo, @wiki.repo.class
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "Wiki page writing" do
|
||||||
|
setup do
|
||||||
|
@path = testpath("examples/test.git")
|
||||||
|
FileUtils.rm_rf(@path)
|
||||||
|
Grit::Repo.init_bare(@path)
|
||||||
|
@wiki = Gollum::Wiki.new(@path)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "write_page" do
|
||||||
|
commit = { :message => "Gollum page",
|
||||||
|
:name => "Tom Preston-Werner",
|
||||||
|
:email => "tom@github.com" }
|
||||||
|
@wiki.write_page("Gollum", :markdown, "# Gollum", commit)
|
||||||
|
assert_equal 1, @wiki.repo.commits.size
|
||||||
|
assert_equal "Gollum page", @wiki.repo.commits.first.message
|
||||||
|
assert_equal "Tom Preston-Werner", @wiki.repo.commits.first.author.name
|
||||||
|
assert_equal "tom@github.com", @wiki.repo.commits.first.author.email
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
FileUtils.rm_r(File.join(File.dirname(__FILE__), *%w[examples test.git]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user