From 45cad6b262cb507d7ed787cecc3e86f78ea23f5e Mon Sep 17 00:00:00 2001 From: SkyCrawl Date: Wed, 6 May 2015 00:27:49 +0200 Subject: [PATCH 1/4] Adding a convenience "render by symbol" --- .gitignore | 2 ++ lib/github/markup.rb | 38 ++++++++++++++++++++++++++++++-------- lib/github/markups.rb | 32 ++++++++++++++++++++++++-------- test/markup_test.rb | 8 ++++++-- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index a08b381b..2535ee2a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ pkg/ .bundle Gemfile.lock vendor/ +.project +.buildpath diff --git a/lib/github/markup.rb b/lib/github/markup.rb index a2ead501..3551720b 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -4,14 +4,19 @@ module GitHub module Markup extend self - @@markups = [] + + @@markups = {} def markups @@markups end + + def markup_impls + markups.values + end def preload! - markups.each do |markup| + markup_impls.each do |markup| markup.load end end @@ -25,17 +30,34 @@ def render(filename, content = nil) content end end - - def markup(file, pattern, opts = {}, &block) - markups << GemImplementation.new(pattern, file, &block) + + def render(symbol, content) + if content.nil? + raise ArgumentError, 'Can not render a nil.' + elsif markups.has_key?(symbol) + markups[symbol].render(content) + else + content + end + end + + def markup(symbol, file, pattern, opts = {}, &block) + markup(symbol, GemImplementation.new(pattern, file, &block)) + end + + def markup(symbol, impl) + if markups.has_key?(symbol) + raise ArgumentError, "The '#{symbol}' symbol is already defined." + end + markups[symbol] = impl end - def command(command, regexp, name, &block) + def command(symbol, command, regexp, name, &block) if File.exist?(file = File.dirname(__FILE__) + "/commands/#{command}") command = file end - markups << CommandImplementation.new(regexp, command, name, &block) + markup(symbol, CommandImplementation.new(regexp, command, name, &block)) end def can_render?(filename) @@ -43,7 +65,7 @@ def can_render?(filename) end def renderer(filename) - markups.find { |impl| + markup_impls.find { |impl| impl.match?(filename) } end diff --git a/lib/github/markups.rb b/lib/github/markups.rb index 0165f4a4..3a05e5ed 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -2,34 +2,50 @@ require "github/markup/rdoc" require "shellwords" -markups << GitHub::Markup::Markdown.new +module GitHub + module Markups + # all of supported markups: + MARKUP_ASCIIDOC = :asciidoc + MARKUP_CREOLE = :creole + MARKUP_MARKDOWN = :markdown + MARKUP_MEDIAWIKI = :mediawiki + MARKUP_ORG = :org + MARKUP_POD = :pod + MARKUP_RDOC = :rdoc + MARKUP_RST = :rst + MARKUP_TEXTILE = :textile + end +end + +markup(GitHub::Markups::MARKUP_MARKDOWN, GitHub::Markup::Markdown.new) -markup(:redcloth, /textile/) do |content| +markup(GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/) do |content| RedCloth.new(content).to_html end -markups << GitHub::Markup::RDoc.new +markup(GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new) -markup('org-ruby', /org/) do |content| +markup(GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/) do |content| Orgmode::Parser.new(content, { :allow_include_files => false, :skip_syntax_highlight => true }).to_html end -markup(:creole, /creole/) do |content| +markup(GitHub::Markups::MARKUP_CREOLE, :creole, /creole/) do |content| Creole.creolize(content) end -markup(:wikicloth, /mediawiki|wiki/) do |content| +markup(GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/) do |content| WikiCloth::WikiCloth.new(:data => content).to_html(:noedit => true) end -markup(:asciidoctor, /adoc|asc(iidoc)?/) do |content| +markup(GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/) do |content| Asciidoctor.render(content, :safe => :secure, :attributes => %w(showtitle idprefix idseparator=- env=github env-github source-highlighter=html-pipeline)) end command( + GitHub::Markups::MARKUP_RST, "python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", /re?st(\.txt)?/, "restructuredtext" @@ -40,7 +56,7 @@ # # 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/, "pod") do |rendered| +command(GitHub::Markups::MARKUP_POD, '/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 4c8fdf45..c850b12c 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -75,7 +75,7 @@ def call message end end - + def test_knows_what_it_can_and_cannot_render assert_equal false, GitHub::Markup.can_render?('README.html') assert_equal true, GitHub::Markup.can_render?('README.markdown') @@ -96,9 +96,13 @@ def test_each_render_has_a_name assert_equal "restructuredtext", GitHub::Markup.renderer('README.rst').name assert_equal "pod", GitHub::Markup.renderer('README.pod').name end + + def test_rendering_by_symbol + assert_equal "test", GitHub::Markup.render(GitHub::Markups::MARKUP_MARKDOWN, '**test**') + end def test_raises_error_if_command_exits_non_zero - GitHub::Markup.command('test/fixtures/fail.sh', /fail/, 'fail') + GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, 'fail') assert GitHub::Markup.can_render?('README.fail') begin GitHub::Markup.render('README.fail', "stop swallowing errors") From d286e878f1afbd2ffa5ac77029e2e16fb97b6147 Mon Sep 17 00:00:00 2001 From: SkyCrawl Date: Wed, 6 May 2015 00:48:53 +0200 Subject: [PATCH 2/4] Bug fixes --- lib/github/markup.rb | 19 ++++++++++++++++--- lib/github/markups.rb | 33 +++++++++------------------------ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/github/markup.rb b/lib/github/markup.rb index 3551720b..24e6baa1 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -2,6 +2,19 @@ require "github/markup/gem_implementation" module GitHub + module Markups + # all of supported markups: + MARKUP_ASCIIDOC = :asciidoc + MARKUP_CREOLE = :creole + MARKUP_MARKDOWN = :markdown + MARKUP_MEDIAWIKI = :mediawiki + MARKUP_ORG = :org + MARKUP_POD = :pod + MARKUP_RDOC = :rdoc + MARKUP_RST = :rst + MARKUP_TEXTILE = :textile + end + module Markup extend self @@ -42,10 +55,10 @@ def render(symbol, content) end def markup(symbol, file, pattern, opts = {}, &block) - markup(symbol, GemImplementation.new(pattern, file, &block)) + markup_impl(symbol, GemImplementation.new(pattern, file, &block)) end - def markup(symbol, impl) + def markup_impl(symbol, impl) if markups.has_key?(symbol) raise ArgumentError, "The '#{symbol}' symbol is already defined." end @@ -57,7 +70,7 @@ def command(symbol, command, regexp, name, &block) command = file end - markup(symbol, CommandImplementation.new(regexp, command, name, &block)) + markup_impl(symbol, CommandImplementation.new(regexp, command, name, &block)) end def can_render?(filename) diff --git a/lib/github/markups.rb b/lib/github/markups.rb index 3a05e5ed..664a4a79 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -2,50 +2,35 @@ require "github/markup/rdoc" require "shellwords" -module GitHub - module Markups - # all of supported markups: - MARKUP_ASCIIDOC = :asciidoc - MARKUP_CREOLE = :creole - MARKUP_MARKDOWN = :markdown - MARKUP_MEDIAWIKI = :mediawiki - MARKUP_ORG = :org - MARKUP_POD = :pod - MARKUP_RDOC = :rdoc - MARKUP_RST = :rst - MARKUP_TEXTILE = :textile - end -end - -markup(GitHub::Markups::MARKUP_MARKDOWN, GitHub::Markup::Markdown.new) +markup_impl(::GitHub::Markups::MARKUP_MARKDOWN, ::GitHub::Markup::Markdown.new) -markup(GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/) do |content| +markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/) do |content| RedCloth.new(content).to_html end -markup(GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new) +markup_impl(::GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new) -markup(GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/) do |content| +markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/) do |content| Orgmode::Parser.new(content, { :allow_include_files => false, :skip_syntax_highlight => true }).to_html end -markup(GitHub::Markups::MARKUP_CREOLE, :creole, /creole/) do |content| +markup(::GitHub::Markups::MARKUP_CREOLE, :creole, /creole/) do |content| Creole.creolize(content) end -markup(GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/) do |content| +markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/) do |content| WikiCloth::WikiCloth.new(:data => content).to_html(:noedit => true) end -markup(GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/) do |content| +markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/) do |content| Asciidoctor.render(content, :safe => :secure, :attributes => %w(showtitle idprefix idseparator=- env=github env-github source-highlighter=html-pipeline)) end command( - GitHub::Markups::MARKUP_RST, + ::GitHub::Markups::MARKUP_RST, "python2 -S #{Shellwords.escape(File.dirname(__FILE__))}/commands/rest2html", /re?st(\.txt)?/, "restructuredtext" @@ -56,7 +41,7 @@ module Markups # # Any block passed to `command` will be handed the command's STDOUT for # post processing. -command(GitHub::Markups::MARKUP_POD, '/usr/bin/env perl -MPod::Simple::HTML -e Pod::Simple::HTML::go', /pod/, "pod") do |rendered| +command(::GitHub::Markups::MARKUP_POD, '/usr/bin/env perl -MPod::Simple::HTML -e Pod::Simple::HTML::go', /pod/, "pod") do |rendered| if rendered =~ /\s*(.+)\s*/mi $1 end From 9205275c1a132cce2236171ac82dd93ba187c572 Mon Sep 17 00:00:00 2001 From: SkyCrawl Date: Wed, 6 May 2015 01:12:20 +0200 Subject: [PATCH 3/4] Bug fixes --- lib/github/markup.rb | 2 +- test/markup_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/github/markup.rb b/lib/github/markup.rb index 24e6baa1..ddc7312f 100644 --- a/lib/github/markup.rb +++ b/lib/github/markup.rb @@ -44,7 +44,7 @@ def render(filename, content = nil) end end - def render(symbol, content) + def render_s(symbol, content) if content.nil? raise ArgumentError, 'Can not render a nil.' elsif markups.has_key?(symbol) diff --git a/test/markup_test.rb b/test/markup_test.rb index c850b12c..d0b6d09a 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -98,7 +98,7 @@ def test_each_render_has_a_name end def test_rendering_by_symbol - assert_equal "test", GitHub::Markup.render(GitHub::Markups::MARKUP_MARKDOWN, '**test**') + assert_equal '

test

', GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, '`test`').strip end def test_raises_error_if_command_exits_non_zero From 3cc56d2eff215b033954c690cb01df1d7d70dddf Mon Sep 17 00:00:00 2001 From: SkyCrawl Date: Wed, 6 May 2015 18:56:32 +0200 Subject: [PATCH 4/4] Updating README to reflect recet changes --- README.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 92e570c3..fe2318ec 100644 --- a/README.md +++ b/README.md @@ -38,18 +38,31 @@ gem install github-markup Usage ----- +Basic form: + ```ruby require 'github/markup' -GitHub::Markup.render('README.markdown', "* One\n* Two") + +GitHub::Markup.render('README.markdown', '* One\n* Two') ``` -Or, more realistically: +More realistic form: ```ruby require 'github/markup' + GitHub::Markup.render(file, File.read(file)) ``` +And a convenience form: + +```ruby +require 'github/markup' + +GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, '* One\n* Two') +``` + + Contributing ------------