Skip to content

Commit

Permalink
Merge pull request github#377 from github/implementation-name
Browse files Browse the repository at this point in the history
Add #name to renderers
  • Loading branch information
bkeepers committed Mar 8, 2015
2 parents ad43ec6 + 60119d9 commit 46010af
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/github/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.exist?(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)
Expand Down
5 changes: 3 additions & 2 deletions lib/github/markup/command_implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,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)
Expand Down
4 changes: 4 additions & 0 deletions lib/github/markup/gem_implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def render(content)
load
renderer.call(content)
end

def name
gem_name
end
end
end
end
4 changes: 4 additions & 0 deletions lib/github/markup/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def render(content)
@renderer.call(content)
end

def name
"markdown"
end

private
def try_require(file)
require file
Expand Down
15 changes: 10 additions & 5 deletions lib/github/markup/rdoc.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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

def name
"rdoc"
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions lib/github/markups.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "github/markup/markdown"
require "github/markup/rdoc"
require "shellwords"

markups << GitHub::Markup::Markdown.new
Expand All @@ -7,13 +8,11 @@
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, {
:allow_include_files => false,
Orgmode::Parser.new(content, {
:allow_include_files => false,
:skip_syntax_highlight => true
}).to_html
end
Expand All @@ -30,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 =~ /<!-- start doc -->\s*(.+)\s*<!-- end doc -->/mi
$1
end
Expand Down
14 changes: 13 additions & 1 deletion test/markup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,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('test/fixtures/fail.sh', /fail/)
GitHub::Markup.command('test/fixtures/fail.sh', /fail/, 'fail')
assert GitHub::Markup.can_render?('README.fail')
begin
GitHub::Markup.render('README.fail', "stop swallowing errors")
Expand Down

0 comments on commit 46010af

Please sign in to comment.