Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Commit

Permalink
Modified gitcode to allow syntax highlighting of arbitrary files
Browse files Browse the repository at this point in the history
  • Loading branch information
dekimsey committed Nov 19, 2012
1 parent f5581c4 commit 7d5311a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
22 changes: 13 additions & 9 deletions lib/gollum/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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```
#
#########################################################################

Expand All @@ -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
Expand Down
60 changes: 60 additions & 0 deletions test/test_gitcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions test/test_markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/wiki_factory.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7d5311a

Please sign in to comment.