From 857c6b5eb87b9a9888f5db718bf6f0a265825085 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Mon, 19 Apr 2010 11:24:55 -0700 Subject: [PATCH] file links --- lib/gollum/file.rb | 5 +++++ lib/gollum/markup.rb | 39 +++++++++++++++++++++++++-------------- test/test_markup.rb | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/lib/gollum/file.rb b/lib/gollum/file.rb index 03c1700d..25eee288 100644 --- a/lib/gollum/file.rb +++ b/lib/gollum/file.rb @@ -8,6 +8,7 @@ module Gollum def initialize(wiki) @wiki = wiki @blob = nil + @path = nil end # Public: The on-disk filename of the file. @@ -27,6 +28,9 @@ module Gollum # Public: The Grit::Commit version of the file. attr_reader :version + # Public: The String path of the file. + attr_reader :path + ######################################################################### # # Internal Methods @@ -43,6 +47,7 @@ module Gollum commit = @wiki.repo.commit(version) if blob = commit.tree / name @blob = blob + @path = name @version = commit self else diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index f1e091a7..27ed110f 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -77,19 +77,8 @@ module Gollum parts = tag.split('|') name = parts[0].strip - if name =~ /^\// - if file = @wiki.file(name[1..-1], @version) - %{} - else - nil - end - else - path = ::File.join(@dir, name) - if file = @wiki.file(path, @version) - %{} - else - nil - end + if file = find_file(name) + %{} end end @@ -100,7 +89,15 @@ module Gollum # Returns the String HTML if the tag is a valid file link tag or nil # if it is not. def process_file_link_tag(tag) - nil + parts = tag.split('|') + name = parts[0].strip + path = parts[1] && parts[1].strip + + if name && path && file = find_file(path) + %{#{name}} + else + nil + end end # Attempt to process the tag as a page link tag. @@ -115,5 +112,19 @@ module Gollum cname = Gollum::canonical_name((parts[1] || parts[0]).strip) %{#{name}} end + + # Find the given file in the repo. + # + # name - The String absolute or relative path of the file. + # + # Returns the Gollum::File or nil if none was found. + def find_file(name) + if name =~ /^\// + @wiki.file(name[1..-1], @version) + else + path = ::File.join(@dir, name) + @wiki.file(path, @version) + end + end end end \ No newline at end of file diff --git a/test/test_markup.rb b/test/test_markup.rb index 7d33e648..da07578c 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -46,4 +46,26 @@ context "Markup" do assert_equal %{

a b

\n}, output end + test "file link with absolute path" do + index = @wiki.repo.index + index.add("alpha.jpg", "hi") + index.commit("Add alpha.jpg") + @wiki.write_page("Bilbo Baggins", :markdown, "a [[Alpha|/alpha.jpg]] b", @commit) + + page = @wiki.page("Bilbo Baggins") + output = Gollum::Markup.new(page).render + assert_equal %{

a Alpha b

\n}, output + end + + test "file link with relative path" do + index = @wiki.repo.index + index.add("greek/alpha.jpg", "hi") + index.add("greek/Bilbo-Baggins.md", "a [[Alpha|alpha.jpg]] b") + index.commit("Add alpha.jpg") + + page = @wiki.page("Bilbo Baggins") + output = Gollum::Markup.new(page).render + assert_equal %{

a Alpha b

\n}, output + end + end