diff --git a/README.md b/README.md index c42674c9..70257b9d 100644 --- a/README.md +++ b/README.md @@ -383,6 +383,13 @@ Get a specific version of a given static file: wiki.file('asset.js', '5ec521178e0eec4dc39741a8978a2ba6616d0f0a') +Get an in-memory Page preview (useful for generating previews for web +interfaces): + + preview = wiki.preview_page("My Page", "# Contents", :markdown) + preview.formatted_data + # => "

Contents

" + Methods that write to the repository require a Hash of commit data that takes the following form: diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index a703024a..5e2d1e4e 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -84,6 +84,25 @@ module Gollum @file_class.new(self).find(name, version) end + # Public: Create an in-memory Page with the given data and format. This + # is useful for previewing what content will look like before committing + # it to the repository. + # + # name - The String name of the page. + # format - The Symbol format of the page. + # data - The new String contents of the page. + # + # Returns the in-memory Gollum::Page. + def preview_page(name, data, format) + page = @page_class.new(self) + ext = @page_class.format_to_ext(format.to_sym) + path = @page_class.cname(name) + '.' + ext + blob = OpenStruct.new(:name => path, :data => data) + page.populate(blob, path) + page.version = self.repo.commit("HEAD") + page + end + # Public: Write a new version of a page to the Gollum repo root. # # name - The String name of the page. diff --git a/test/test_wiki.rb b/test/test_wiki.rb index c35fb74c..3205561a 100644 --- a/test/test_wiki.rb +++ b/test/test_wiki.rb @@ -40,6 +40,20 @@ context "Wiki" do end end +context "Wiki page previewing" do + setup do + @path = testpath("examples/lotr.git") + @wiki = Gollum::Wiki.new(@path) + end + + test "preview_page" do + page = @wiki.preview_page("Test", "# Bilbo", :markdown) + assert_equal "# Bilbo", page.raw_data + assert_equal "

Bilbo

", page.formatted_data + assert_equal "Test.md", page.name + end +end + context "Wiki page writing" do setup do @path = testpath("examples/test.git")