Permalink
Browse files

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

Modified gitcode to allow syntax highlighting of arbitrary files
  • Loading branch information...
2 parents f5581c4 + 7d5311a commit 10c121e603a5118a9fa59d21b647a73c37c96dbd @bootstraponline bootstraponline committed Nov 20, 2012
Showing with 77 additions and 24 deletions.
  1. +13 −9 lib/gollum/markup.rb
  2. +60 −0 test/test_gitcode.rb
  3. +2 −13 test/test_markup.rb
  4. +2 −2 test/wiki_factory.rb
View
@@ -395,12 +395,12 @@ def process_toc_tags(data)
# 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 @@ def find_page_from_name(cname)
#
# 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 @@ def extract_gitcode data
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
View
@@ -34,6 +34,66 @@ def page_with_content c
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
View
@@ -582,24 +582,13 @@ def assert_markup_highlights_code(markup_class, rendered)
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 @@ def assert_markup_highlights_code(markup_class, rendered)
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
View
@@ -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

0 comments on commit 10c121e

Please sign in to comment.