diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 7602b7fd..7b3d60b0 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -395,12 +395,12 @@ module Gollum # name - The String absolute or relative path of the file. # # Returns the Gollum::File or nil if none was found. - def find_file(name) + def find_file(name, version=@version) if name =~ /^\// - @wiki.file(name[1..-1], @version) + @wiki.file(name[1..-1], version) else path = @dir == '.' ? name : ::File.join(@dir, name) - @wiki.file(path, @version) + @wiki.file(path, version) end end @@ -435,6 +435,10 @@ module Gollum # # Gitcode - fetch code from github search path and replace the contents # to a code-block that gets run the next parse. + # Acceptable formats: + # ```language:local-file.ext``` + # ```language:/abs/other-file.ext``` + # ```language:github/gollum/master/somefile.txt``` # ######################################################################### @@ -445,12 +449,12 @@ module Gollum uri = $2 || '' # Detect local file. if uri[0..6] != 'github/' - if uri[0..0] != '/' # relative file - contents = @wiki.page(uri).formatted_data - else # use full path - contents = @wiki.paged( extract_name( clean_url( uri ) ), - '/' + clean_url( extract_path( uri ) ) ).formatted_data - end + if file = self.find_file(uri, @wiki.ref) + contents = file.raw_data + else + # How do we communicate a render error? + next "File not found: #{Rack::Utils::escape_html(uri)}" + end else contents = Gollum::Gitcode.new(uri).contents end diff --git a/test/test_gitcode.rb b/test/test_gitcode.rb index 807e3c02..0c52e401 100644 --- a/test/test_gitcode.rb +++ b/test/test_gitcode.rb @@ -34,6 +34,66 @@ context "gitcode" do assert_equal g.contents, %{
    \n
  1. \n 0\n
  2. \n
\n} end + test "gitcode relative local file" do + @wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:file-exists.py```\nb", commit_details) + page = @wiki.page('Bilbo Baggins') + + index = @wiki.repo.index + index.add("file-exists.py", "import sys\n\nprint sys.maxint\n") + index.commit("Add file-exists.py") + + @wiki.clear_cache + + output = page.formatted_data + assert_equal %Q{

a\n

import sys\n\nprint sys.maxint\n
\n\n

b

}, output + end + + test "gitcode relative local file in subdir" do + index = @wiki.repo.index + index.add("foo/file-exists.py", "import sys\n\nprint sys.maxint\n") + index.commit("Add file-exists.py") + + @wiki.write_page("Pippin", :markdown, "a\n```python:file-exists.py```\nb", commit_details, 'foo') + + page = @wiki.paged('Pippin', 'foo') + output = page.formatted_data + assert_equal %Q{

a\n

import sys\n\nprint sys.maxint\n
\n\n

b

}, output + end + + test "gitcode relative no file" do + @wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:no-file-exists.py```\nb", commit_details) + page = @wiki.page('Bilbo Baggins') + output = page.formatted_data + assert_equal %Q{

a\nFile not found: no-file-exists.py\nb

}, output + end + + test "gitcode absolute local file" do + @wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:/monkey/file-exists.py```\nb", commit_details) + page = @wiki.page('Bilbo Baggins') + + index = @wiki.repo.index + index.add("monkey/file-exists.py", "import sys\n\nprint sys.platform\n") + index.commit("Add monkey/file-exists.py") + @wiki.clear_cache + + output = page.formatted_data + assert_equal %Q{

a\n

import sys\n\nprint sys.platform\n
\n\n

b

}, output + end + + test "gitcode absolute no file" do + @wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:/monkey/no-file-exists.py```\nb", commit_details) + page = @wiki.page('Bilbo Baggins') + output = page.formatted_data + assert_equal %Q{

a\nFile not found: /monkey/no-file-exists.py\nb

}, output + end + + test "gitcode error generates santized html" do + @wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:```\nb", commit_details) + page = @wiki.page('Bilbo Baggins') + output = page.formatted_data + assert_equal %Q{

a\nFile not found: <script>foo</script>\nb

}, output + end + teardown do @cleanup.call end diff --git a/test/test_markup.rb b/test/test_markup.rb index 7e0b4d6b..a8a10e18 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -582,24 +582,13 @@ np.array([[2,2],[1,3]],np.float) assert_match /\(\[\[/, rendered, "#{markup_class} parses out wiki links\n#{rendered}" end - test "embed code is escaped" do - @wiki.write_page("script", :markdown, "a b", commit_details) - @wiki.write_page("page", :markdown, "```html:script```", commit_details) - - output_script = @wiki.page("script").formatted_data - output_page = @wiki.page("page").formatted_data - - assert_equal %Q{

a b

}, output_script - assert_equal %Q{
<p>a  b</p>\n
}, output_page - end - test "embed code page absolute link" do @wiki.write_page("base", :markdown, "a\n!base\b", commit_details) @wiki.write_page("a", :markdown, "a\n```html:/base```\b", commit_details) page = @wiki.page("a") output = page.formatted_data - assert_equal %Q{

a\n

<p>a\n!base</p>\n
\n}, output + assert_equal %Q{

a\nFile not found: /base

}, output end test "embed code page relative link" do @@ -608,7 +597,7 @@ np.array([[2,2],[1,3]],np.float) page = @wiki.page("a") output = page.formatted_data - assert_equal %Q{

a\n

<p>a\n!rel</p>\n
\n}, output + assert_equal %Q{

a\nFile not found: base

}, output end test "code block in unsupported language" do diff --git a/test/wiki_factory.rb b/test/wiki_factory.rb index 0fd7df4f..a6c81174 100644 --- a/test/wiki_factory.rb +++ b/test/wiki_factory.rb @@ -1,8 +1,8 @@ class WikiFactory - def self.create p + def self.create p, opt={} path = testpath "examples/test.git" Grit::Repo.init_bare(path) - Gollum::Wiki.default_options = {:universal_toc => false} + Gollum::Wiki.default_options = {:universal_toc => false}.merge(opt) cleanup = Proc.new { FileUtils.rm_r File.join(File.dirname(__FILE__), *%w[examples test.git]) } wiki = Gollum::Wiki.new(path) # set 'wiki-' prefix on ids for tests