Add local file support.

Example of absolute path to local file.
```html:/home```

Example of relative path to local file.
```html:home```

1.8.7 string fix. 's'[0..0] instead of 's'[0]

Fix regex.

Add absolute and relative tests.
This commit is contained in:
bootstraponline
2012-09-10 21:37:47 -06:00
parent 424b4d3f4e
commit 05c4bf3374
5 changed files with 58 additions and 10 deletions
-6
View File
@@ -89,12 +89,6 @@ module Precious
redirect File.join(settings.wiki_options[:base_path].to_s, 'Home')
end
# Removes all slashes from the start of string.
def clean_url url
return url if url.nil?
url.gsub('%2F','/').gsub(/^\/+/,'')
end
# path is set to name if path is nil.
# if path is 'a/b' and a and b are dirs, then
# path must have a trailing slash 'a/b/' or
+6
View File
@@ -17,5 +17,11 @@ module Precious
def sanitize_empty_params(param)
[nil,''].include?(param) ? nil : CGI.unescape(param)
end
# Remove all slashes from the start of string.
def clean_url url
return url if url.nil?
url.gsub('%2F','/').gsub(/^\/+/,'')
end
end
end
+1
View File
@@ -7,6 +7,7 @@ module Gollum
class Gitcode
def initialize path
raise(ArgumentError, 'path is nil or empty') if path.nil? or path.empty?
@uri = URI::HTTP.build({
:path => self.unchomp(path),
:host => 'raw.github.com',
+22 -4
View File
@@ -2,7 +2,9 @@ require 'digest/sha1'
require 'cgi'
require 'pygments'
require 'base64'
require File.expand_path( '../gitcode', __FILE__ )
require File.expand_path '../frontend/helpers', __FILE__
require File.expand_path '../gitcode', __FILE__
# initialize Pygments
Pygments.start
@@ -10,6 +12,8 @@ Pygments.start
module Gollum
class Markup
include Precious::Helpers
attr_accessor :toc
attr_reader :metadata
@@ -459,9 +463,23 @@ module Gollum
#########################################################################
def extract_gitcode data
data.gsub /^[ \t]*``` ?(\w+):(.+)```$/ do
gc = Gollum::Gitcode.new $2
"```#{$1}\n#{gc.contents}\n```"
data.gsub /^[ \t]*``` ?([^:\n\r]+):([^`\n\r]+)```/ do
contents = ''
# Use empty string if $2 is nil.
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
else
contents = Gollum::Gitcode.new(uri).contents
end
"```#{$1}\n#{contents}\n```\n"
end
end
+29
View File
@@ -495,6 +495,35 @@ 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\">\n<pre><span class=\"nt\">&lt;p&gt;</span>a b<span class=\"nt\">&lt;/p&gt;</span>\n</pre>\n</div>\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{<p>a\n</p><div class=\"highlight\">\n<pre><span class=\"nt\">&lt;p&gt;</span>a\n!base<span class=\"nt\">&lt;/p&gt;</span>\n</pre>\n</div>\n\n}, output
end
test "embed code page relative link" do
@wiki.write_page("base", :markdown, "a\n!rel\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\">\n<pre><span class=\"nt\">&lt;p&gt;</span>a\n!rel<span class=\"nt\">&lt;/p&gt;</span>\n</pre>\n</div>\n\n}, output
end
#########################################################################
#
# Web Sequence Diagrams