diff --git a/README.md b/README.md index 01cb6a59..d8e55e8f 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,10 @@ Initialize the Gollum::Repo object: wiki = Gollum::Wiki.new("my-gollum-repo.git") # => +By default, internal wiki links are all absolute from the root. To specify a different base path, you can send specify the `:base_path` option: + + wiki = Gollum::Wiki.new("my-gollum-repo.git", :base_path => "/wiki") + Get the latest version of the given human or canonical page name: page = wiki.page('page-name') diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 134b2fe5..f8246058 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -202,11 +202,9 @@ module Gollum parts = tag.split('|') name = parts[0].strip cname = Page.cname((parts[1] || parts[0]).strip) - if @wiki.page(cname) - %{#{name}} - else - %{#{name}} - end + link = ::File.join(@wiki.base_path, cname) + presence = @wiki.page(cname) ? "present" : "absent" + %{#{name}} end # Find the given file in the repo. diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 2eb893e1..05f8ca41 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -6,6 +6,11 @@ module Gollum attr_accessor :page_class, :file_class end + # The String base path to prefix to internal links. For example, when set + # to "/wiki", the page "Hobbit" will be linked as "/wiki/Hobbit". Defaults + # to "/". + attr_reader :base_path + # Public: Initialize a new Gollum Repo. # # repo - The String path to the Git repository that holds the Gollum @@ -18,6 +23,7 @@ module Gollum def initialize(path, options = {}) @path = path @repo = Grit::Repo.new(path) + @base_path = options[:base_path] || "/" @page_class = options[:page_class] || self.class.page_class @file_class = options[:file_class] || self.class.file_class end diff --git a/test/test_markup.rb b/test/test_markup.rb index 5cf35269..822c1c10 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -21,7 +21,7 @@ context "Markup" do page = @wiki.page("Bilbo Baggins") output = Gollum::Markup.new(page).render - assert_equal %{

a Bilbo Baggins b

}, output + assert_equal %{

a Bilbo Baggins b

}, output end test "absent page link" do @@ -29,7 +29,18 @@ context "Markup" do page = @wiki.page("Tolkien") output = Gollum::Markup.new(page).render - assert_equal %{

a J. R. R. Tolkien's b

}, output + assert_equal %{

a J. R. R. Tolkien's b

}, output + end + + test "page link with custom base path" do + ["/wiki", "/wiki/"].each do |path| + @wiki = Gollum::Wiki.new(@path, :base_path => path) + @wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", @commit) + + page = @wiki.page("Bilbo Baggins") + output = Gollum::Markup.new(page).render + assert_equal %{

a Bilbo Baggins b

}, output + end end test "image with http url" do @@ -190,7 +201,7 @@ context "Markup" do test "quoted wiki link" do content = "a '[[Foo]]', b" - output = "

a 'Foo', b

" + output = "

a 'Foo', b

" compare(content, output) end