file links
This commit is contained in:
@@ -8,6 +8,7 @@ module Gollum
|
|||||||
def initialize(wiki)
|
def initialize(wiki)
|
||||||
@wiki = wiki
|
@wiki = wiki
|
||||||
@blob = nil
|
@blob = nil
|
||||||
|
@path = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: The on-disk filename of the file.
|
# Public: The on-disk filename of the file.
|
||||||
@@ -27,6 +28,9 @@ module Gollum
|
|||||||
# Public: The Grit::Commit version of the file.
|
# Public: The Grit::Commit version of the file.
|
||||||
attr_reader :version
|
attr_reader :version
|
||||||
|
|
||||||
|
# Public: The String path of the file.
|
||||||
|
attr_reader :path
|
||||||
|
|
||||||
#########################################################################
|
#########################################################################
|
||||||
#
|
#
|
||||||
# Internal Methods
|
# Internal Methods
|
||||||
@@ -43,6 +47,7 @@ module Gollum
|
|||||||
commit = @wiki.repo.commit(version)
|
commit = @wiki.repo.commit(version)
|
||||||
if blob = commit.tree / name
|
if blob = commit.tree / name
|
||||||
@blob = blob
|
@blob = blob
|
||||||
|
@path = name
|
||||||
@version = commit
|
@version = commit
|
||||||
self
|
self
|
||||||
else
|
else
|
||||||
|
|||||||
+24
-13
@@ -77,19 +77,8 @@ module Gollum
|
|||||||
parts = tag.split('|')
|
parts = tag.split('|')
|
||||||
name = parts[0].strip
|
name = parts[0].strip
|
||||||
|
|
||||||
if name =~ /^\//
|
if file = find_file(name)
|
||||||
if file = @wiki.file(name[1..-1], @version)
|
%{<img src="/#{file.path}" />}
|
||||||
%{<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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -100,8 +89,16 @@ module Gollum
|
|||||||
# Returns the String HTML if the tag is a valid file link tag or nil
|
# Returns the String HTML if the tag is a valid file link tag or nil
|
||||||
# if it is not.
|
# if it is not.
|
||||||
def process_file_link_tag(tag)
|
def process_file_link_tag(tag)
|
||||||
|
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
|
nil
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Attempt to process the tag as a page link tag.
|
# 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)
|
cname = Gollum::canonical_name((parts[1] || parts[0]).strip)
|
||||||
%{<a href="#{cname}">#{name}</a>}
|
%{<a href="#{cname}">#{name}</a>}
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
@@ -46,4 +46,26 @@ context "Markup" do
|
|||||||
assert_equal %{<p>a <img src="/greek/alpha.jpg" /> b</p>\n}, output
|
assert_equal %{<p>a <img src="/greek/alpha.jpg" /> b</p>\n}, output
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user