file links
This commit is contained in:
@@ -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
|
||||
|
||||
+25
-14
@@ -77,19 +77,8 @@ module Gollum
|
||||
parts = tag.split('|')
|
||||
name = parts[0].strip
|
||||
|
||||
if name =~ /^\//
|
||||
if file = @wiki.file(name[1..-1], @version)
|
||||
%{<img src="/#{file.name}" />}
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
path = ::File.join(@dir, name)
|
||||
if file = @wiki.file(path, @version)
|
||||
%{<img src="/#{path}" />}
|
||||
else
|
||||
nil
|
||||
end
|
||||
if file = find_file(name)
|
||||
%{<img src="/#{file.path}" />}
|
||||
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)
|
||||
%{<a href="/#{file.path}">#{name}</a>}
|
||||
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)
|
||||
%{<a href="#{cname}">#{name}</a>}
|
||||
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
|
||||
@@ -46,4 +46,26 @@ context "Markup" do
|
||||
assert_equal %{<p>a <img src="/greek/alpha.jpg" /> b</p>\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 %{<p>a <a href="/alpha.jpg">Alpha</a> b</p>\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 %{<p>a <a href="/greek/alpha.jpg">Alpha</a> b</p>\n}, output
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user