diff --git a/README.md b/README.md index 9e9a60c2..10b8e840 100644 --- a/README.md +++ b/README.md @@ -280,20 +280,22 @@ Get a specific version of a given static file: gollum.file('asset.js', '5ec521178e0eec4dc39741a8978a2ba6616d0f0a') -Write a new version of a given canonical page file (the file will be created -if it does not already exist) and commit the change. +Write a new version of a page (the file will be created if it does not already +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 -message: +Update an existing page (keeps the same name, format, and directory location). - 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: - gollum.delete_page('page-name') + gollum.delete_page('Page Name') Once again, you can optionally specify a commit message: - gollum.delete_page('page-name', 'commit message') \ No newline at end of file + gollum.delete_page('Page Name', 'commit message') \ No newline at end of file diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 6548d79c..02bc0a91 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -4,6 +4,23 @@ module Gollum 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. # # wiki - The Gollum::Wiki in question. diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 2e8f8583..962d9257 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -31,5 +31,24 @@ module Gollum def file(name, version = 'master') File.new(self).find(name, version) 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 \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index c6deb8c3..0b11383e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -2,6 +2,7 @@ require 'rubygems' require 'test/unit' require 'shoulda' require 'mocha' +require 'fileutils' dir = File.dirname(File.expand_path(__FILE__)) $LOAD_PATH.unshift(File.join(dir, '..', 'lib')) diff --git a/test/test_wiki.rb b/test/test_wiki.rb index d6f2c400..b8bb81c1 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -2,14 +2,38 @@ require File.join(File.dirname(__FILE__), *%w[helper]) context "Wiki" do setup do - @repo = Gollum::Wiki.new(testpath("examples/lotr.git")) + @wiki = Gollum::Wiki.new(testpath("examples/lotr.git")) end test "repo path" do - assert_equal testpath("examples/lotr.git"), @repo.path + assert_equal testpath("examples/lotr.git"), @wiki.path end 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 \ No newline at end of file