Skip to content

Commit

Permalink
Merge branch 'SkyCrawl-symbol-convenience-render-method'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Izumi committed Mar 16, 2017
2 parents 19b7e43 + 59d7609 commit e8fd011
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@ pkg/
.bundle
Gemfile.lock
vendor/
.project
.buildpath
17 changes: 15 additions & 2 deletions README.md
Expand Up @@ -39,18 +39,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
------------

Expand Down
51 changes: 43 additions & 8 deletions lib/github/markup.rb
Expand Up @@ -2,16 +2,34 @@
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
@@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
Expand All @@ -25,25 +43,42 @@ def render(filename, content = nil)
content
end
end

def markup(file, pattern, opts = {}, &block)
markups << GemImplementation.new(pattern, file, &block)

def render_s(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_impl(symbol, GemImplementation.new(pattern, file, &block))
end

def markup_impl(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_impl(symbol, CommandImplementation.new(regexp, command, name, &block))
end

def can_render?(filename)
!!renderer(filename)
end

def renderer(filename)
markups.find { |impl|
markup_impls.find { |impl|
impl.match?(filename)
}
end
Expand Down
17 changes: 9 additions & 8 deletions lib/github/markups.rb
Expand Up @@ -2,40 +2,41 @@
require "github/markup/rdoc"
require "shellwords"

markups << GitHub::Markup::Markdown.new
markup_impl(::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_impl(::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::WikiCloth.new(:data => content)
WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS << 'tt' unless WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.include?('tt')
wikicloth.to_html(:noedit => true)
end

markup(:asciidoctor, /adoc|asc(iidoc)?/) do |content|
markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/) do |content|
Asciidoctor::Compliance.unique_id_start_index = 1
Asciidoctor.convert(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"
)

command(:pod2html, /pod/, "pod")
command(::GitHub::Markups::MARKUP_POD, :pod2html, /pod/, "pod")
8 changes: 6 additions & 2 deletions test/markup_test.rb
Expand Up @@ -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')
Expand All @@ -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 '<p><code>test</code></p>', GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, '`test`').strip
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")
Expand Down

0 comments on commit e8fd011

Please sign in to comment.