From 9e5907663c80f15328c495c0937bf69199078f7c Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 11:55:51 -0400 Subject: [PATCH 1/2] Refactor rdoc impelementation --- lib/github/markup/rdoc.rb | 11 ++++++----- lib/github/markups.rb | 5 ++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/github/markup/rdoc.rb b/lib/github/markup/rdoc.rb index 5bf14639..dcbdf32c 100644 --- a/lib/github/markup/rdoc.rb +++ b/lib/github/markup/rdoc.rb @@ -1,20 +1,21 @@ +require "github/markup/implementation" require "rdoc" require "rdoc/markup/to_html" module GitHub module Markup - class RDoc - def initialize(content) - @content = content + class RDoc < Implementation + def initialize + super /rdoc/ end - def to_html + def render(content) if ::RDoc::VERSION.to_i >= 4 h = ::RDoc::Markup::ToHtml.new(::RDoc::Options.new) else h = ::RDoc::Markup::ToHtml.new end - h.convert(@content) + h.convert(content) end end end diff --git a/lib/github/markups.rb b/lib/github/markups.rb index eb562909..1915b662 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -1,4 +1,5 @@ require "github/markup/markdown" +require "github/markup/rdoc" require "shellwords" markups << GitHub::Markup::Markdown.new @@ -7,9 +8,7 @@ RedCloth.new(content).to_html end -markup('github/markup/rdoc', /rdoc/) do |content| - GitHub::Markup::RDoc.new(content).to_html -end +markups << GitHub::Markup::RDoc.new markup('org-ruby', /org/) do |content| Orgmode::Parser.new(content, { From 7a06537713a8a6e56192f2f2c8a082319b7b99d5 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 29 Sep 2014 11:56:27 -0400 Subject: [PATCH 2/2] add #name to all markup implementations --- lib/github/markup.rb | 4 ++-- lib/github/markup/command_implementation.rb | 5 +++-- lib/github/markup/gem_implementation.rb | 4 ++++ lib/github/markup/markdown.rb | 4 ++++ lib/github/markup/rdoc.rb | 4 ++++ lib/github/markups.rb | 12 ++++++++---- test/markup_test.rb | 14 +++++++++++++- 7 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/github/markup.rb b/lib/github/markup.rb index 4e3bf917..88d8a45e 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -30,12 +30,12 @@ def markup(file, pattern, opts = {}, &block) markups << GemImplementation.new(pattern, file, &block) end - def command(command, regexp, &block) + def command(command, regexp, name, &block) if File.exists?(file = File.dirname(__FILE__) + "/commands/#{command}") command = file end - markups << CommandImplementation.new(regexp, command, &block) + markups << CommandImplementation.new(regexp, command, name, &block) end def can_render?(filename) diff --git a/lib/github/markup/command_implementation.rb b/lib/github/markup/command_implementation.rb index 170e9b0a..1115535f 100644 --- a/lib/github/markup/command_implementation.rb +++ b/lib/github/markup/command_implementation.rb @@ -7,12 +7,13 @@ class CommandError < RuntimeError end class CommandImplementation < Implementation - attr_reader :command, :block + attr_reader :command, :block, :name - def initialize(regexp, command, &block) + def initialize(regexp, command, name, &block) super regexp @command = command.to_s @block = block + @name = name end def render(content) diff --git a/lib/github/markup/gem_implementation.rb b/lib/github/markup/gem_implementation.rb index a9f80b75..270f5a88 100644 --- a/lib/github/markup/gem_implementation.rb +++ b/lib/github/markup/gem_implementation.rb @@ -21,6 +21,10 @@ def render(content) load renderer.call(content) end + + def name + gem_name + end end end end diff --git a/lib/github/markup/markdown.rb b/lib/github/markup/markdown.rb index 8ad9722f..c3b51bfb 100644 --- a/lib/github/markup/markdown.rb +++ b/lib/github/markup/markdown.rb @@ -44,6 +44,10 @@ def render(content) @renderer.call(content) end + def name + "markdown" + end + private def try_require(file) require file diff --git a/lib/github/markup/rdoc.rb b/lib/github/markup/rdoc.rb index dcbdf32c..5448f455 100644 --- a/lib/github/markup/rdoc.rb +++ b/lib/github/markup/rdoc.rb @@ -17,6 +17,10 @@ def render(content) end h.convert(content) end + + def name + "rdoc" + end end end end diff --git a/lib/github/markups.rb b/lib/github/markups.rb index 1915b662..0165f4a4 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -11,8 +11,8 @@ markups << GitHub::Markup::RDoc.new markup('org-ruby', /org/) do |content| - Orgmode::Parser.new(content, { - :allow_include_files => false, + Orgmode::Parser.new(content, { + :allow_include_files => false, :skip_syntax_highlight => true }).to_html end @@ -29,14 +29,18 @@ Asciidoctor.render(content, :safe => :secure, :attributes => %w(showtitle idprefix idseparator=- env=github env-github source-highlighter=html-pipeline)) end -command("python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", /re?st(\.txt)?/) +command( + "python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", + /re?st(\.txt)?/, + "restructuredtext" +) # pod2html is nice enough to generate a full-on HTML document for us, # so we return the favor by ripping out the good parts. # # Any block passed to `command` will be handed the command's STDOUT for # post processing. -command('/usr/bin/env perl -MPod::Simple::HTML -e Pod::Simple::HTML::go', /pod/) do |rendered| +command('/usr/bin/env perl -MPod::Simple::HTML -e Pod::Simple::HTML::go', /pod/, "pod") do |rendered| if rendered =~ /\s*(.+)\s*/mi $1 end diff --git a/test/markup_test.rb b/test/markup_test.rb index 392424f7..9a54262c 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -43,8 +43,20 @@ def test_knows_what_it_can_and_cannot_render assert_equal true, GitHub::Markup.can_render?('README.litcoffee') end + def test_each_render_has_a_name + assert_equal "markdown", GitHub::Markup.renderer('README.md').name + assert_equal "redcloth", GitHub::Markup.renderer('README.textile').name + assert_equal "rdoc", GitHub::Markup.renderer('README.rdoc').name + assert_equal "org-ruby", GitHub::Markup.renderer('README.org').name + assert_equal "creole", GitHub::Markup.renderer('README.creole').name + assert_equal "wikicloth", GitHub::Markup.renderer('README.wiki').name + assert_equal "asciidoctor", GitHub::Markup.renderer('README.adoc').name + assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst').name + assert_equal "pod", GitHub::Markup.renderer('README.pod').name + end + def test_raises_error_if_command_exits_non_zero - GitHub::Markup.command('echo "failure message">&2 && false', /fail/) + GitHub::Markup.command('echo "failure message">&2 && false', /fail/, "fail") assert GitHub::Markup.can_render?('README.fail') begin GitHub::Markup.render('README.fail', "stop swallowing errors")