Skip to content

Commit de903b8

Browse files
author
Ashe Connor
authored
Merge pull request #1236 from github/cleanup-markup-options
markup options, cleanup
2 parents e8a9b37 + 8f5eea1 commit de903b8

File tree

9 files changed

+57
-51
lines changed

9 files changed

+57
-51
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gem "posix-spawn", :platforms => :ruby
55
gem "redcarpet", :platforms => :ruby
66
gem "kramdown", :platforms => :jruby
77
gem "RedCloth"
8-
gem "commonmarker", "~> 0.14.12"
8+
gem "commonmarker", "~> 0.18.1"
99
gem "rdoc", "~>3.6"
1010
gem "org-ruby", "= 0.9.9"
1111
gem "creole", "~>0.3.6"

lib/github/markup.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,49 @@ module Markups
2020
MARKUP_RST = :rst
2121
MARKUP_TEXTILE = :textile
2222
end
23-
23+
2424
module Markup
2525
extend self
26-
26+
2727
@@markups = {}
2828

2929
def markups
3030
@@markups
3131
end
32-
32+
3333
def markup_impls
3434
markups.values
3535
end
3636

3737
def preload!
38-
markup_impls.each do |markup|
39-
markup.load
40-
end
38+
markup_impls.each(&:load)
4139
end
4240

43-
def render(filename, content, symlink = false)
44-
if impl = renderer(filename, content, symlink)
45-
impl.render(filename, content)
41+
def render(filename, content, symlink: false, options: {})
42+
if (impl = renderer(filename, content, symlink: symlink))
43+
impl.render(filename, content, options: options)
4644
else
4745
content
4846
end
4947
end
50-
48+
5149
def render_s(symbol, content)
52-
if content.nil?
53-
raise ArgumentError, 'Can not render a nil.'
54-
elsif markups.has_key?(symbol)
50+
raise ArgumentError, 'Can not render a nil.' if content.nil?
51+
52+
if markups.key?(symbol)
5553
markups[symbol].render(nil, content)
5654
else
5755
content
5856
end
5957
end
6058

6159
def markup(symbol, gem_name, regexp, languages, opts = {}, &block)
62-
markup_impl(symbol, GemImplementation.new(regexp, languages, gem_name, &block))
60+
impl = GemImplementation.new(regexp, languages, gem_name, &block)
61+
markup_impl(symbol, impl)
6362
end
64-
63+
6564
def markup_impl(symbol, impl)
66-
if markups.has_key?(symbol)
65+
if markups.key?(symbol)
6766
raise ArgumentError, "The '#{symbol}' symbol is already defined."
6867
end
6968
markups[symbol] = impl
@@ -74,25 +73,26 @@ def command(symbol, command, regexp, languages, name, &block)
7473
command = file
7574
end
7675

77-
markup_impl(symbol, CommandImplementation.new(regexp, languages, command, name, &block))
76+
impl = CommandImplementation.new(regexp, languages, command, name, &block)
77+
markup_impl(symbol, impl)
7878
end
7979

80-
def can_render?(filename, content, symlink = false)
81-
!!renderer(filename, content, symlink)
80+
def can_render?(filename, content, symlink: false)
81+
renderer(filename, content, symlink: symlink) != nil
8282
end
8383

84-
def renderer(filename, content, symlink = false)
85-
language = language(filename, content, symlink)
86-
markup_impls.find { |impl|
84+
def renderer(filename, content, symlink: false)
85+
language = language(filename, content, symlink: symlink)
86+
markup_impls.find do |impl|
8787
impl.match?(filename, language)
88-
}
88+
end
8989
end
9090

91-
def language(filename, content, symlink = false)
92-
if defined?(::Linguist)
93-
blob = Linguist::Blob.new(filename, content, symlink: symlink)
94-
return Linguist.detect(blob, allow_empty: true)
95-
end
91+
def language(filename, content, symlink: false)
92+
return unless defined?(::Linguist)
93+
94+
blob = Linguist::Blob.new(filename, content, symlink: symlink)
95+
Linguist.detect(blob, allow_empty: true)
9696
end
9797

9898
# Define markups

lib/github/markup/command_implementation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(regexp, languages, command, name, &block)
2222
@name = name
2323
end
2424

25-
def render(filename, content)
25+
def render(filename, content, options: {})
2626
rendered = execute(command, content)
2727
rendered = rendered.to_s.empty? ? content : rendered
2828
call_block(rendered, content)

lib/github/markup/gem_implementation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ def initialize(regexp, languages, gem_name, &renderer)
1212
end
1313

1414
def load
15-
return if @loaded
15+
return if defined?(@loaded) && @loaded
1616
require gem_name
1717
@loaded = true
1818
end
1919

20-
def render(filename, content)
20+
def render(filename, content, options: {})
2121
load
22-
renderer.call(filename, content)
22+
renderer.call(filename, content, options: options)
2323
end
2424

2525
def name

lib/github/markup/implementation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def load
1616
# no-op by default
1717
end
1818

19-
def render(filename, content)
19+
def render(filename, content, options: {})
2020
raise NotImplementedError, "subclasses of GitHub::Markup::Implementation must define #render"
2121
end
2222

lib/github/markup/markdown.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ module GitHub
44
module Markup
55
class Markdown < Implementation
66
MARKDOWN_GEMS = {
7-
"commonmarker" => proc { |content|
8-
CommonMarker.render_html(content, :GITHUB_PRE_LANG, [:tagfilter, :autolink, :table, :strikethrough])
7+
"commonmarker" => proc { |content, options: {}|
8+
commonmarker_opts = [:GITHUB_PRE_LANG].concat(options.fetch(:commonmarker_opts, []))
9+
CommonMarker.render_html(content, commonmarker_opts, [:tagfilter, :autolink, :table, :strikethrough])
910
},
10-
"github/markdown" => proc { |content|
11+
"github/markdown" => proc { |content, options: {}|
1112
GitHub::Markdown.render(content)
1213
},
13-
"redcarpet" => proc { |content|
14+
"redcarpet" => proc { |content, options: {}|
1415
Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(content)
1516
},
16-
"rdiscount" => proc { |content|
17+
"rdiscount" => proc { |content, options: {}|
1718
RDiscount.new(content).to_html
1819
},
19-
"maruku" => proc { |content|
20+
"maruku" => proc { |content, options: {}|
2021
Maruku.new(content).to_html
2122
},
22-
"kramdown" => proc { |content|
23+
"kramdown" => proc { |content, options: {}|
2324
Kramdown::Document.new(content).to_html
2425
},
25-
"bluecloth" => proc { |content|
26+
"bluecloth" => proc { |content, options: {}|
2627
BlueCloth.new(content).to_html
2728
},
2829
}
@@ -44,9 +45,9 @@ def load
4445
raise LoadError, "no suitable markdown gem found"
4546
end
4647

47-
def render(filename, content)
48+
def render(filename, content, options: {})
4849
load
49-
@renderer.call(content)
50+
@renderer.call(content, options: options)
5051
end
5152

5253
def name

lib/github/markup/rdoc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize
99
super(/rdoc/, ["RDoc"])
1010
end
1111

12-
def render(filename, content)
12+
def render(filename, content, options: {})
1313
if ::RDoc::VERSION.to_i >= 4
1414
h = ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
1515
else

lib/github/markups.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44

55
markup_impl(::GitHub::Markups::MARKUP_MARKDOWN, ::GitHub::Markup::Markdown.new)
66

7-
markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/, ["Textile"]) do |filename, content|
7+
markup(::GitHub::Markups::MARKUP_TEXTILE, :redcloth, /textile/, ["Textile"]) do |filename, content, options: {}|
88
RedCloth.new(content).to_html
99
end
1010

1111
markup_impl(::GitHub::Markups::MARKUP_RDOC, GitHub::Markup::RDoc.new)
1212

13-
markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/, ["Org"]) do |filename, content|
13+
markup(::GitHub::Markups::MARKUP_ORG, 'org-ruby', /org/, ["Org"]) do |filename, content, options: {}|
1414
Orgmode::Parser.new(content, {
1515
:allow_include_files => false,
1616
:skip_syntax_highlight => true
1717
}).to_html
1818
end
1919

20-
markup(::GitHub::Markups::MARKUP_CREOLE, :creole, /creole/, ["Creole"]) do |filename, content|
20+
markup(::GitHub::Markups::MARKUP_CREOLE, :creole, /creole/, ["Creole"]) do |filename, content, options: {}|
2121
Creole.creolize(content)
2222
end
2323

24-
markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/, ["MediaWiki"]) do |filename, content|
24+
markup(::GitHub::Markups::MARKUP_MEDIAWIKI, :wikicloth, /mediawiki|wiki/, ["MediaWiki"]) do |filename, content, options: {}|
2525
wikicloth = WikiCloth::WikiCloth.new(:data => content)
2626
WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS << 'tt' unless WikiCloth::WikiBuffer::HTMLElement::ESCAPED_TAGS.include?('tt')
2727
wikicloth.to_html(:noedit => true)
2828
end
2929

30-
markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/, ["AsciiDoc"]) do |filename, content|
30+
markup(::GitHub::Markups::MARKUP_ASCIIDOC, :asciidoctor, /adoc|asc(iidoc)?/, ["AsciiDoc"]) do |filename, content, options: {}|
3131
attributes = {
3232
'showtitle' => '@',
3333
'idprefix' => '',

test/markup_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_raises_error_if_command_exits_non_zero
102102
GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, ['Java'], 'fail')
103103
assert GitHub::Markup.can_render?('README.java', 'stop swallowing errors')
104104
begin
105-
GitHub::Markup.render('README.java', "stop swallowing errors", false)
105+
GitHub::Markup.render('README.java', "stop swallowing errors", symlink: false)
106106
rescue GitHub::Markup::CommandError => e
107107
assert_equal "failure message", e.message
108108
else
@@ -114,4 +114,9 @@ def test_preserve_markup
114114
content = "Noël"
115115
assert_equal content.encoding.name, GitHub::Markup.render('Foo.rst', content).encoding.name
116116
end
117+
118+
def test_commonmarker_options
119+
assert_equal "<p>hello <!-- raw HTML omitted --> world</p>\n", GitHub::Markup.render("test.md", "hello <bad> world")
120+
assert_equal "<p>hello <bad> world</p>\n", GitHub::Markup.render("test.md", "hello <bad> world", options: {commonmarker_opts: [:UNSAFE]})
121+
end
117122
end

0 commit comments

Comments
 (0)