Merge pull request #585 from dekimsey/gitcode-embed-any-file

Modified gitcode to allow syntax highlighting of arbitrary files
This commit is contained in:
bootstraponline
2012-11-19 16:11:21 -08:00
4 changed files with 77 additions and 24 deletions
+13 -9
View File
@@ -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
+60
View File
@@ -34,6 +34,66 @@ context "gitcode" do
assert_equal g.contents, %{<ol class=\"tree\">\n <li class=\"file\">\n <a href=\"0\"><span class=\"icon\"></span>0</a>\n </li>\n</ol>\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{<p>a\n</p><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>\n\n<span class="k">print</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxint</span>\n</pre></div>\n\n<p>b</p>}, 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{<p>a\n</p><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>\n\n<span class="k">print</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxint</span>\n</pre></div>\n\n<p>b</p>}, 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{<p>a\nFile not found: no-file-exists.py\nb</p>}, 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{<p>a\n</p><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>\n\n<span class="k">print</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span>\n</pre></div>\n\n<p>b</p>}, 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{<p>a\nFile not found: /monkey/no-file-exists.py\nb</p>}, output
end
test "gitcode error generates santized html" do
@wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:<script>foo</script>```\nb", commit_details)
page = @wiki.page('Bilbo Baggins')
output = page.formatted_data
assert_equal %Q{<p>a\nFile not found: &lt;script&gt;foo&lt;/script&gt;\nb</p>}, output
end
teardown do
@cleanup.call
end
+2 -13
View File
@@ -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 <script></script> 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{<p>a b</p>}, output_script
assert_equal %Q{<div class=\"highlight\"><pre><span class=\"nt\">&lt;p&gt;</span>a b<span class=\"nt\">&lt;/p&gt;</span>\n</pre></div>}, 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{<p>a\n</p><div class=\"highlight\"><pre><span class=\"nt\">&lt;p&gt;</span>a\n!base<span class=\"nt\">&lt;/p&gt;</span>\n</pre></div>\n}, output
assert_equal %Q{<p>a\nFile not found: /base</p>}, 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{<p>a\n</p><div class=\"highlight\"><pre><span class=\"nt\">&lt;p&gt;</span>a\n!rel<span class=\"nt\">&lt;/p&gt;</span>\n</pre></div>\n}, output
assert_equal %Q{<p>a\nFile not found: base</p>}, output
end
test "code block in unsupported language" do
+2 -2
View File
@@ -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