Skip to content

Commit bbf631b

Browse files
committedNov 14, 2022
lint
1 parent 5b807a1 commit bbf631b

18 files changed

+89
-40
lines changed
 

‎lib/commonmarker.rb

+27-25
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@
1212
require "awesome_print"
1313
rescue LoadError; end # rubocop:disable Lint/SuppressedException
1414
module CommonMarker
15-
# Public: Parses a Markdown string into an HTML string.
16-
#
17-
# text - A {String} of text
18-
# option - Either a {Symbol} or {Array of Symbol}s indicating the render options
19-
# extensions - An {Array of Symbol}s indicating the extensions to use
20-
#
21-
# Returns a {String} of converted HTML.
22-
def self.render_html(text, options = :DEFAULT, extensions = [])
23-
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
15+
class << self
16+
# Public: Parses a Markdown string into an HTML string.
17+
#
18+
# text - A {String} of text
19+
# option - Either a {Symbol} or {Array of Symbol}s indicating the render options
20+
# extensions - An {Array of Symbol}s indicating the extensions to use
21+
#
22+
# Returns a {String} of converted HTML.
23+
def render_html(text, options = :DEFAULT, extensions = [])
24+
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
2425

25-
opts = Config.process_options(options, :render)
26-
Node.markdown_to_html(text.encode("UTF-8"), opts, extensions)
27-
end
26+
opts = Config.process_options(options, :render)
27+
Node.markdown_to_html(text.encode("UTF-8"), opts, extensions)
28+
end
2829

29-
# Public: Parses a Markdown string into a `document` node.
30-
#
31-
# string - {String} to be parsed
32-
# option - A {Symbol} or {Array of Symbol}s indicating the parse options
33-
# extensions - An {Array of Symbol}s indicating the extensions to use
34-
#
35-
# Returns the `document` node.
36-
def self.render_doc(text, options = :DEFAULT, extensions = [])
37-
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
30+
# Public: Parses a Markdown string into a `document` node.
31+
#
32+
# string - {String} to be parsed
33+
# option - A {Symbol} or {Array of Symbol}s indicating the parse options
34+
# extensions - An {Array of Symbol}s indicating the extensions to use
35+
#
36+
# Returns the `document` node.
37+
def render_doc(text, options = :DEFAULT, extensions = [])
38+
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
3839

39-
opts = Config.process_options(options, :parse)
40-
text = text.encode("UTF-8")
41-
Node.parse_document(text, text.bytesize, opts, extensions)
42-
end
40+
opts = Config.process_options(options, :parse)
41+
text = text.encode("UTF-8")
42+
Node.parse_document(text, text.bytesize, opts, extensions)
43+
end
44+
end
4345
end

‎lib/commonmarker/config.rb

+15-13
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ module Config
3333
format: [:html, :xml, :commonmark, :plaintext].freeze,
3434
}.freeze
3535

36-
def self.process_options(option, type)
37-
case option
38-
when Symbol
39-
OPTS.fetch(type).fetch(option)
40-
when Array
41-
raise TypeError if option.none?
36+
class << self
37+
def process_options(option, type)
38+
case option
39+
when Symbol
40+
OPTS.fetch(type).fetch(option)
41+
when Array
42+
raise TypeError if option.none?
4243

43-
# neckbearding around. the map will both check the opts and then bitwise-OR it
44-
OPTS.fetch(type).fetch_values(*option).inject(0, :|)
45-
else
46-
raise TypeError, "option type must be a valid symbol or array of symbols within the #{name}::OPTS[:#{type}] context"
44+
# neckbearding around. the map will both check the opts and then bitwise-OR it
45+
OPTS.fetch(type).fetch_values(*option).inject(0, :|)
46+
else
47+
raise TypeError, "option type must be a valid symbol or array of symbols within the #{name}::OPTS[:#{type}] context"
48+
end
49+
rescue KeyError => e
50+
raise TypeError, "option ':#{e.key}' does not exist for #{name}::OPTS[:#{type}]"
4751
end
48-
rescue KeyError => e
49-
raise TypeError, "option ':#{e.key}' does not exist for #{name}::OPTS[:#{type}]"
50-
end
52+
end
5153
end
5254
end

‎lib/commonmarker/renderer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def tagfilter(str)
113113
)
114114
(?=\s|>|/>)
115115
}xi,
116-
'&lt;\1'
116+
'&lt;\1',
117117
)
118118
else
119119
str

‎test/test_basics.rb

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def test_to_html
1313

1414
def test_markdown_to_html
1515
html = CommonMarker.render_html("Hi *there*")
16+
1617
assert_equal("<p>Hi <em>there</em></p>\n", html)
1718
end
1819

‎test/test_commands.rb

+8
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,58 @@
55
class TestCommands < Minitest::Test
66
def test_basic
77
out = make_bin("strong.md")
8+
89
assert_equal("<p>I am <strong>strong</strong></p>", out)
910
end
1011

1112
def test_does_not_have_extensions
1213
out = make_bin("table.md")
14+
1315
assert_includes(out, "| a")
1416
refute_includes(out, "<p><del>hi</del>")
1517
refute_includes(out, "<table> <tr> <th> a </th> <td> c </td>")
1618
end
1719

1820
def test_understands_extensions
1921
out = make_bin("table.md", "--extension=table")
22+
2023
refute_includes(out, "| a")
2124
refute_includes(out, "<p><del>hi</del>")
2225
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>"].each { |html| assert_includes(out, html) }
2326
end
2427

2528
def test_understands_multiple_extensions
2629
out = make_bin("table.md", "--extension=table,strikethrough")
30+
2731
refute_includes(out, "| a")
2832
assert_includes(out, "<p><del>hi</del>")
2933
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>"].each { |html| assert_includes(out, html) }
3034
end
3135

3236
def test_understands_html_format_with_renderer_and_extensions
3337
out = make_bin("table.md", "--to=html --extension=table,strikethrough --html-renderer")
38+
3439
refute_includes(out, "| a")
3540
assert_includes(out, "<p><del>hi</del>")
3641
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>"].each { |html| assert_includes(out, html) }
3742
end
3843

3944
def test_understands_xml_format
4045
out = make_bin("strong.md", "--to=xml")
46+
4147
assert_includes(out, '<?xml version="1.0" encoding="UTF-8"?>')
4248
assert_includes(out, '<text xml:space="preserve">strong</text>')
4349
end
4450

4551
def test_understands_commonmark_format
4652
out = make_bin("strong.md", "--to=commonmark")
53+
4754
assert_equal("I am **strong**", out)
4855
end
4956

5057
def test_understands_plaintext_format
5158
out = make_bin("strong.md", "--to=plaintext")
59+
5260
assert_equal("I am strong", out)
5361
end
5462

‎test/test_commonmark.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_to_commonmark
3131

3232
assert_equal(\
3333
render_doc(@markdown).to_html.squeeze(" ").gsub(HTML_COMMENT, ""),
34-
render_doc(compare).to_html.squeeze(" ").gsub(HTML_COMMENT, "")
34+
render_doc(compare).to_html.squeeze(" ").gsub(HTML_COMMENT, ""),
3535
)
3636
end
3737
end

‎test/test_doc.rb

+4
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,28 @@ def test_get_next
3434

3535
def test_insert_before
3636
paragraph = Node.new(:paragraph)
37+
3738
assert(@first_child.insert_before(paragraph))
3839
assert_match("<p></p>\n<p>Hi <em>there</em>.", @doc.to_html)
3940
end
4041

4142
def test_insert_after
4243
paragraph = Node.new(:paragraph)
44+
4345
assert(@first_child.insert_after(paragraph))
4446
assert_match("<strong>many nodes</strong>!</p>\n<p></p>\n", @doc.to_html)
4547
end
4648

4749
def test_prepend_child
4850
code = Node.new(:code)
51+
4952
assert(@first_child.prepend_child(code))
5053
assert_match("<p><code></code>Hi <em>there</em>.", @doc.to_html)
5154
end
5255

5356
def test_append_child
5457
strong = Node.new(:strong)
58+
5559
assert(@first_child.append_child(strong))
5660
assert_match("!<strong></strong></p>\n", @doc.to_html)
5761
end

‎test/test_encoding.rb

+3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ def test_encoding
88
contents = fixtures_file("curly.md")
99
doc = CommonMarker.render_doc(contents, :SMART)
1010
render = doc.to_html
11+
1112
assert_equal("<p>This curly quote “makes commonmarker throw an exception”.</p>", render.rstrip)
1213

1314
render = doc.to_xml
15+
1416
assert_includes(render, '<text xml:space="preserve">This curly quote “makes commonmarker throw an exception”.</text>')
1517
end
1618

1719
def test_string_content_is_utf8
1820
doc = CommonMarker.render_doc("Hi *there*")
1921
text = doc.first_child.last_child.first_child
22+
2023
assert_equal("there", text.string_content)
2124
assert_equal("UTF-8", text.string_content.encoding.name)
2225
end

‎test/test_extensions.rb

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def test_uses_specified_extensions
1717
CommonMarker.render_html(@markdown, :DEFAULT, [:table]).tap do |out|
1818
refute_includes(out, "| a")
1919
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>", "<strong>x</strong>"].each { |html| assert_includes(out, html) }
20+
2021
assert_includes(out, "~~hi~~")
2122
end
2223

@@ -27,9 +28,11 @@ def test_uses_specified_extensions
2728
end
2829

2930
doc = CommonMarker.render_doc("~a~ ~~b~~ ~~~c~~~", :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
31+
3032
assert_equal("<p>~a~ <del>b</del> ~~~c~~~</p>\n", doc.to_html)
3133

3234
html = CommonMarker.render_html("~a~ ~~b~~ ~~~c~~~", :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
35+
3336
assert_equal("<p>~a~ <del>b</del> ~~~c~~~</p>\n", html)
3437

3538
CommonMarker.render_html(@markdown, :DEFAULT, [:table, :strikethrough]).tap do |out|
@@ -45,16 +48,19 @@ def test_extensions_with_renderers
4548
doc.to_html.tap do |out|
4649
refute_includes(out, "| a")
4750
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>", "<strong>x</strong>"].each { |html| assert_includes(out, html) }
51+
4852
assert_includes(out, "~~hi~~")
4953
end
5054

5155
HtmlRenderer.new.render(doc).tap do |out|
5256
refute_includes(out, "| a")
5357
["<table>", "<tr>", "<th>", "a", "</th>", "<td>", "c", "</td>", "<strong>x</strong>"].each { |html| assert_includes(out, html) }
58+
5459
assert_includes(out, "~~hi~~")
5560
end
5661

5762
doc = CommonMarker.render_doc("~a~ ~~b~~ ~~~c~~~", :STRIKETHROUGH_DOUBLE_TILDE, [:strikethrough])
63+
5864
assert_equal("<p>~a~ <del>b</del> ~~~c~~~</p>\n", HtmlRenderer.new.render(doc))
5965
end
6066

‎test/test_gc.rb

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_drop_child_reference
2424
GC.start
2525
# Test that the cached child object is still valid.
2626
text = doc.first_child.last_child.first_child
27+
2728
assert_equal("there", text.string_content)
2829
end
2930

‎test/test_linebreaks.rb

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
class TestLinebreaks < Minitest::Test
66
def test_hardbreak_no_spaces
77
doc = CommonMarker.render_doc("foo\nbaz")
8+
89
assert_equal("<p>foo<br />\nbaz</p>\n", doc.to_html(:HARDBREAKS))
910
end
1011

1112
def test_hardbreak_with_spaces
1213
doc = CommonMarker.render_doc("foo \nbaz")
14+
1315
assert_equal("<p>foo<br />\nbaz</p>\n", doc.to_html(:HARDBREAKS))
1416
end
1517
end

‎test/test_maliciousness.rb

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def test_rendering_with_bad_type
7474
err = assert_raises(TypeError) do
7575
CommonMarker.render_doc("foo \n baz", :safe)
7676
end
77+
7778
assert_equal("option ':safe' does not exist for CommonMarker::Config::OPTS[:parse]", err.message)
7879

7980
assert_raises(TypeError) do

‎test/test_node.rb

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_walk
1212
@doc.walk do |node|
1313
nodes << node.type
1414
end
15+
1516
assert_equal([:document, :paragraph, :text, :emph, :text, :text], nodes)
1617
end
1718

@@ -20,6 +21,7 @@ def test_each
2021
@doc.first_child.each do |node|
2122
nodes << node.type
2223
end
24+
2325
assert_equal([:text, :emph, :text], nodes)
2426
end
2527

@@ -30,18 +32,21 @@ def test_deprecated_each_child
3032
nodes << node.type
3133
end
3234
end
35+
3336
assert_equal([:text, :emph, :text], nodes)
3437
assert_match(/`each_child` is deprecated/, err)
3538
end
3639

3740
def test_select
3841
nodes = @doc.first_child.select { |node| node.type == :text }
42+
3943
assert_equal(CommonMarker::Node, nodes.first.class)
4044
assert_equal([:text, :text], nodes.map(&:type))
4145
end
4246

4347
def test_map
4448
nodes = @doc.first_child.map(&:type)
49+
4550
assert_equal([:text, :emph, :text], nodes)
4651
end
4752

@@ -58,6 +63,7 @@ def test_to_html
5863
def test_html_renderer
5964
renderer = HtmlRenderer.new
6065
result = renderer.render(@doc)
66+
6167
assert_equal("<p>Hi <em>there</em>, I am mostly text!</p>\n", result)
6268
end
6369

@@ -66,6 +72,7 @@ def test_walk_and_set_string_content
6672
node.string_content = "world" if node.type == :text && node.string_content == "there"
6773
end
6874
result = HtmlRenderer.new.render(@doc)
75+
6976
assert_equal("<p>Hi <em>world</em>, I am mostly text!</p>\n", result)
7077
end
7178

@@ -76,6 +83,7 @@ def test_walk_and_delete_node
7683
node.delete
7784
end
7885
end
86+
7987
assert_equal("<p>Hi there, I am mostly text!</p>\n", @doc.to_html)
8088
end
8189

‎test/test_pathological_inputs.rb

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def markdown(str)
4747
pathological.each_pair do |name, description|
4848
define_method("test_#{name}") do
4949
input, = description
50+
5051
assert markdown(input)
5152
end
5253
end

‎test/test_renderer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setup
1010
def test_html_renderer
1111
renderer = HtmlRenderer.new
1212
result = renderer.render(@doc)
13+
1314
assert_equal("<p>Hi <em>there</em></p>\n", result)
1415
end
1516

@@ -25,6 +26,7 @@ def test_multiple_tables
2526
DOC
2627
doc = CommonMarker.render_doc(content, :DEFAULT, [:autolink, :table, :tagfilter])
2728
results = CommonMarker::HtmlRenderer.new.render(doc)
29+
2830
assert_equal(2, results.scan(/<tbody>/).size)
2931
end
3032

@@ -41,6 +43,7 @@ def text(node)
4143
end
4244

4345
renderer = my_renderer.new
46+
4447
assert_equal(Encoding::UTF_8, renderer.render(@doc).encoding)
4548
assert_equal(renderer.input_encoding, renderer.output_encoding)
4649
end

0 commit comments

Comments
 (0)