Skip to content
Snippets Groups Projects
Commit 50ffcb42 authored by Sunny Ripert's avatar Sunny Ripert
Browse files

Merge pull request #2 from gollum/remote-code

Removed gitcode in favor of uri-based remote_code
parents 20b3d8c2 2201d899
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -5,7 +5,7 @@ require 'pygments'
require 'base64'
 
require File.expand_path '../helpers', __FILE__
require File.expand_path '../gitcode', __FILE__
require File.expand_path '../remote_code', __FILE__
 
# initialize Pygments
Pygments.start
Loading
Loading
@@ -78,7 +78,7 @@ module Gollum
 
data = @data.dup
data = extract_metadata(data)
data = extract_gitcode(data)
data = extract_remote_code(data)
data = extract_code(data)
data = extract_wsd(data)
data = extract_tags(data)
Loading
Loading
@@ -138,7 +138,7 @@ module Gollum
node = Nokogiri::XML::Node.new('ul', doc)
tail = tail.add_child(node)
tail_level += 1
end
end
while tail_level > level
tail = tail.parent
tail_level -= 1
Loading
Loading
@@ -457,33 +457,34 @@ module Gollum
 
#########################################################################
#
# Gitcode - fetch code from github search path and replace the contents
# to a code-block that gets run the next parse.
# Remote code - fetch code from url 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/gollum/master/somefile.txt```
# ```language:https://example.com/somefile.txt```
#
#########################################################################
 
def extract_gitcode data
data.gsub /^[ \t]*``` ?([^:\n\r]+):(?:(github:))?([^`\n\r]+)```/ do
contents = ''
# Use empty string if $2 is nil.
uri = $3 || ''
# Detect local file.
if uri[0..6] != 'gollum/'
if file = self.find_file(uri, @wiki.ref)
contents = file.raw_data
else
# How do we communicate a render error?
next "File not found: #{CGI::escapeHTML(uri)}"
end
def extract_remote_code data
data.gsub /^[ \t]*``` ?([^:\n\r]+):((http)?[^`\n\r]+)```/ do
language = $1
uri = $2
protocol = $3
# Detect local file
if protocol.nil?
if file = self.find_file(uri, @wiki.ref)
contents = file.raw_data
else
# How do we communicate a render error?
next "File not found: #{CGI::escapeHTML(uri)}"
end
else
contents = Gollum::Gitcode.new(uri).contents
contents = Gollum::RemoteCode.new(uri).contents
end
 
"```#{$1}\n#{contents}\n```\n"
"```#{language}\n#{contents}\n```\n"
end
end
 
Loading
Loading
# ~*~ encoding: utf-8 ~*~
require 'net/http'
require 'net/https' # ruby 1.8.7 fix, remove at upgrade
require 'uri'
require 'open-uri'
module Gollum
class RemoteCode
def initialize path
raise(ArgumentError, 'path is nil or empty') if path.nil? or path.empty?
@uri = URI(path)
end
def contents
@contents ||= req @uri
end
private
def req uri, cut = 1
return "Too many redirects or retries" if cut >= 10
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
resp = http.get uri.path, {
'Accept' => 'text/plain',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive',
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0'
}
code = resp.code.to_i
return resp.body if code == 200
return "Not Found" if code == 404
return "Unhandled Response Code #{code}" unless code == 304 or not resp.header['location'].nil?
loc = URI.parse resp.header['location']
uri2 = loc.relative? ? (uri + loc) : loc # overloads (+)
req uri2, (cut + 1)
end
end
end
Loading
Loading
@@ -2,7 +2,7 @@
require File.expand_path( '../helper', __FILE__ )
require File.expand_path( '../wiki_factory', __FILE__ )
 
context "gitcode" do
context "remote_code" do
 
def page_with_content c
index = @wiki.repo.index
Loading
Loading
@@ -20,7 +20,7 @@ context "gitcode" do
 
test 'that the rendered output is correctly fetched and rendered as html code' do
# given
p = page_with_content "a\n\n```html:github:gollum/gollum-lib/master/test/file_view/1_file.txt```\n\nb"
p = page_with_content "a\n\n```html:https://raw.github.com/gollum/gollum-lib/master/test/file_view/1_file.txt```\n\nb"
 
# when rendering the page
rendered = Gollum::Markup.new(p).render
Loading
Loading
@@ -31,13 +31,13 @@ context "gitcode" do
end
 
test 'contents' do
g = Gollum::Gitcode.new 'gollum/gollum-lib/master/test/file_view/1_file.txt'
g = Gollum::RemoteCode.new 'https://raw.github.com/gollum/gollum-lib/master/test/file_view/1_file.txt'
 
expected = %{<ol class=\"tree\">\n <li class=\"file\">\n <a href=\"0\"><span class=\"icon\"></span>0</a>\n </li>\n</ol>\n}
assert_equal expected, g.contents
end
 
test "gitcode relative local file" do
test "remote_code relative local file" do
@wiki.write_page("Bilbo Baggins", :markdown, "a\n```python:file-exists.py```\nb", commit_details)
page = @wiki.page('Bilbo Baggins')
 
Loading
Loading
@@ -51,7 +51,7 @@ context "gitcode" do
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
test "remote_code 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")
Loading
Loading
@@ -63,14 +63,14 @@ context "gitcode" do
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
test "remote_code 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
test "remote_code 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')
 
Loading
Loading
@@ -83,14 +83,14 @@ context "gitcode" do
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
test "remote_code 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
test "remote_code 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
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment