diff --git a/.gitignore b/.gitignore index 033ec934..62e405fe 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ pkg .bundle Gemfile.lock *.gem +*.swp diff --git a/README.md b/README.md index 47023643..c41348dd 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,27 @@ then that whitespace will be ignored (this makes the blocks easier to read in pl The block must end with three backticks indented at the same level than the opening backticks. +### GITHUB SYNTAX HIGHLIGHTING + +As an extra feature, you can syntax highlight a file from your repository, allowing +you keep some of your sample code in the main repository. The code-snippet is +updated when the wiki is rebuilt. You include github code like this: + + ```html:github/gollum/master/test/file_view/1_file.txt``` + +This will make the builder look at the **github user**, in the **gollum project**, +in the **master branch**, at path **test/file_view/1_file.txt**. It will be +rewritten to: + + ```html +
    +
  1. 0
  2. +
+ ``` + +Which will be parsed as HTML code during the Pygments run, and thereby coloured +appropriately. + ## MATHEMATICAL EQUATIONS diff --git a/lib/gollum/gitcode.rb b/lib/gollum/gitcode.rb new file mode 100644 index 00000000..9c2576d3 --- /dev/null +++ b/lib/gollum/gitcode.rb @@ -0,0 +1,46 @@ +require 'net/http' +require 'uri' +require 'open-uri' + +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', + :scheme => 'https', + :port => 443 }) + end + + def contents + @contents ||= self.req @uri + #req @uri + end + + def unchomp p + p[0] == '/' ? p : ('/' + p) + end + + def req uri, cut = 1 + #puts uri.to_s + 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', + 'Host' => uri.host, + '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 (+) + return req uri2, (cut + 1) + end + end +end diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index e1970338..07df6df6 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -2,6 +2,7 @@ require 'digest/sha1' require 'cgi' require 'pygments' require 'base64' +require File.expand_path( '../gitcode', __FILE__ ) # initialize Pygments Pygments.start @@ -50,6 +51,7 @@ module Gollum data = @data.dup data = extract_metadata(data) + data = extract_gitcode(data) data = extract_code(data) data = extract_tex(data) data = extract_wsd(data) @@ -449,6 +451,20 @@ module Gollum end end + ######################################################################### + # + # Gitcode - fetch code from github search path and replace the contents + # to a code-block that gets run the next parse. + # + ######################################################################### + + def extract_gitcode data + data.gsub /^[ \t]*``` ?(\w+):(.+)```$/ do + gc = Gollum::Gitcode.new $2 + "```#{$1}\n#{gc.contents}\n```" + end + end + ######################################################################### # # Code diff --git a/templates/helper_wiki.rb b/templates/helper_wiki.rb new file mode 100644 index 00000000..16c17cba --- /dev/null +++ b/templates/helper_wiki.rb @@ -0,0 +1,9 @@ +class WikiFactory + def self.create p + path = testpath "examples/test.git" + Grit::Repo.init_bare(@path) + Gollum::Wiki.default_options = {:universal_toc => false} + cleanup = lambda { FileUtils.rm_r File.join(File.dirname(__FILE__), *%w[examples test.git]) } + Gollum::Wiki.new(@path), @path, cleanup + end +end diff --git a/test/test_gitcode.rb b/test/test_gitcode.rb new file mode 100644 index 00000000..14546c06 --- /dev/null +++ b/test/test_gitcode.rb @@ -0,0 +1,52 @@ +# ~*~ encoding: utf-8 ~*~ +require File.expand_path( '../helper', __FILE__ ) +require File.expand_path( '../wiki_factory', __FILE__ ) + +context "gitcode" do + + def page_with_content c + index = @wiki.repo.index + index.add 'Sample-Html.md', c + index.commit 'adding file html sample' + + page = @wiki.page 'Sample Html' + page + end + + setup do + # context + @wiki, @path, @cleanup = WikiFactory.create 'examples/test.git' + + # given + p = page_with_content "a\n\n```html:github/gollum/master/test/file_view/1_file.txt```\n\nb" + + # when rendering the page + @rendered = Gollum::Markup.new(p).render + end + + test 'that the rendered output is correctly fetched and rendered as html code' do + assert_equal %Q{

a

+ +
+
<ol class="tree">
+  <li class="file"><a href="0">0</a></li>
+</ol>
+
+
+ + +

b

}, @rendered + end + + test 'contents' do + g = Gollum::Gitcode.new 'github/gollum/master/test/file_view/1_file.txt' + + assert_equal g.contents, %{
    +
  1. 0
  2. +
} + end + + teardown do + @cleanup.() + end +end diff --git a/test/test_markup.rb b/test/test_markup.rb index c62ffd09..6e7b6a0d 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -1,17 +1,14 @@ # ~*~ encoding: utf-8 ~*~ -require File.expand_path(File.join(File.dirname(__FILE__), "helper")) +require File.expand_path( "../helper", __FILE__ ) +require File.expand_path( "../wiki_factory", __FILE__ ) context "Markup" do setup do - @path = testpath("examples/test.git") - FileUtils.rm_rf(@path) - Grit::Repo.init_bare(@path) - Gollum::Wiki.default_options = {:universal_toc => false} - @wiki = Gollum::Wiki.new(@path) + @wiki, @path, @teardown = WikiFactory.create 'examples/test.git' end teardown do - FileUtils.rm_r(File.join(File.dirname(__FILE__), *%w[examples test.git])) + @teardown.() end test "formats page from Wiki#pages" do diff --git a/test/wiki_factory.rb b/test/wiki_factory.rb new file mode 100644 index 00000000..aade4432 --- /dev/null +++ b/test/wiki_factory.rb @@ -0,0 +1,10 @@ +class WikiFactory + def self.create p + path = testpath "examples/test.git" + Grit::Repo.init_bare(path) + Gollum::Wiki.default_options = {:universal_toc => false} + cleanup = lambda { FileUtils.rm_r File.join(File.dirname(__FILE__), *%w[examples test.git]) } + wiki = Gollum::Wiki.new(path) + return wiki, path, cleanup + end +end