Skip to content

Commit

Permalink
add #name to all markup implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Sep 29, 2014
1 parent 9e59076 commit 7a06537
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 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.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)
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 @@ -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)
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
4 changes: 4 additions & 0 deletions lib/github/markup/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def render(content)
end
h.convert(content)
end

def name
"rdoc"
end
end
end
end
12 changes: 8 additions & 4 deletions lib/github/markups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =~ /<!-- 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 @@ -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")
Expand Down

0 comments on commit 7a06537

Please sign in to comment.