diff --git a/lib/gollum/frontend/app.rb b/lib/gollum/frontend/app.rb index 05468abe..dbc98212 100644 --- a/lib/gollum/frontend/app.rb +++ b/lib/gollum/frontend/app.rb @@ -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 diff --git a/lib/gollum/frontend/helpers.rb b/lib/gollum/frontend/helpers.rb index 845a0c7a..b8ec4afb 100644 --- a/lib/gollum/frontend/helpers.rb +++ b/lib/gollum/frontend/helpers.rb @@ -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 diff --git a/lib/gollum/gitcode.rb b/lib/gollum/gitcode.rb index 63f5d9e0..a4a8c064 100644 --- a/lib/gollum/gitcode.rb +++ b/lib/gollum/gitcode.rb @@ -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', diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 07df6df6..9351376d 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -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 diff --git a/test/test_markup.rb b/test/test_markup.rb index 411fef34..5ac12435 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -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 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{
a b
}, output_script + assert_equal %Q{<p>a b</p>\n\na\n
<p>a\n!base</p>\n\na\n
<p>a\n!rel</p>\n\n