Skip to content

Commit

Permalink
Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Feb 10, 2021
1 parent b1abff8 commit 9fa7f94
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 93 deletions.
8 changes: 1 addition & 7 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
inherit_gem:
rubocop-standard:
- config/default.yml

Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

Naming/FileName:
Enabled: false
- config/minitest.yml

AllCops:
Exclude:
Expand Down
4 changes: 2 additions & 2 deletions bin/commonmarker
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ doc = CommonMarker.render_doc(ARGF.read, options.active_parse_options, options.a

if options.renderer
renderer = CommonMarker::HtmlRenderer.new(extensions: options.active_extensions)
STDOUT.write(renderer.render(doc))
$stdout.write(renderer.render(doc))
else
STDOUT.write(doc.to_html(options.active_render_options, options.active_extensions))
$stdout.write(doc.to_html(options.active_render_options, options.active_extensions))
end
1 change: 1 addition & 0 deletions commonmarker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |s|

s.executables = ['commonmarker']
s.require_paths = %w[lib ext]
s.required_ruby_version = ['>= 2.4.10', '< 4.0']

s.rdoc_options += ['-x', 'ext/commonmarker/cmark/.*']

Expand Down
5 changes: 3 additions & 2 deletions lib/commonmarker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ class Render

def self.process_options(option, type)
type = Config.const_get(type.capitalize)
if option.is_a?(Symbol)
case option
when Symbol
check_option(option, type)
type.to_h[option]
elsif option.is_a?(Array)
when Array
option = [nil] if option.empty?
# neckbearding around. the map will both check the opts and then bitwise-OR it
option.map do |o|
Expand Down
2 changes: 1 addition & 1 deletion lib/commonmarker/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Node
#
# blk - A {Proc} representing the action to take for each child
def walk(&block)
return enum_for(:walk) unless block_given?
return enum_for(:walk) unless block

yield self
each do |child|
Expand Down
8 changes: 5 additions & 3 deletions lib/commonmarker/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module CommonMarker
class Renderer
attr_accessor :in_tight, :warnings, :in_plain

def initialize(options: :DEFAULT, extensions: [])
@opts = Config.process_options(options, :render)
@stream = StringIO.new(+'')
Expand All @@ -18,11 +19,12 @@ def initialize(options: :DEFAULT, extensions: [])

def out(*args)
args.each do |arg|
if arg == :children
case arg
when :children
@node.each { |child| out(child) }
elsif arg.is_a?(Array)
when Array
arg.each { |x| render(x) }
elsif arg.is_a?(Node)
when Node
render(arg)
else
@stream.write(arg)
Expand Down
20 changes: 10 additions & 10 deletions test/test_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
class TestCommands < Minitest::Test
def test_basic
out = make_bin('strong.md')
assert_equal out, '<p>I am <strong>strong</strong></p>'
assert_equal('<p>I am <strong>strong</strong></p>', out)
end

def test_does_not_have_extensions
out = make_bin('table.md')
assert out.include?('| a')
refute out.include?('<p><del>hi</del>')
refute out.include?('<table> <tr> <th> a </th> <td> c </td>')
assert_includes out, '| a'
refute_includes out, '<p><del>hi</del>'
refute_includes out, '<table> <tr> <th> a </th> <td> c </td>'
end

def test_understands_extensions
out = make_bin('table.md', '--extension=table')
refute out.include?('| a')
refute out.include?('<p><del>hi</del>')
%w[<table> <tr> <th> a </th> <td> c </td>].each { |html| assert out.include?(html) }
refute_includes out, '| a'
refute_includes out, '<p><del>hi</del>'
%w[<table> <tr> <th> a </th> <td> c </td>].each { |html| assert_includes out, html }
end

def test_understands_multiple_extensions
out = make_bin('table.md', '--extension=table,strikethrough')
refute out.include?('| a')
assert out.include?('<p><del>hi</del>')
%w[<table> <tr> <th> a </th> <td> c </td>].each { |html| assert out.include?(html) }
refute_includes out, '| a'
assert_includes out, '<p><del>hi</del>'
%w[<table> <tr> <th> a </th> <td> c </td>].each { |html| assert_includes out, html }
end
end
4 changes: 2 additions & 2 deletions test/test_commonmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_to_commonmark
compare = render_doc(@markdown).to_commonmark

assert_equal \
render_doc(@markdown).to_html.gsub(/ +/, ' ').gsub(HTML_COMMENT, ''),
render_doc(compare).to_html.gsub(/ +/, ' ').gsub(HTML_COMMENT, '')
render_doc(@markdown).to_html.squeeze(' ').gsub(HTML_COMMENT, ''),
render_doc(compare).to_html.squeeze(' ').gsub(HTML_COMMENT, '')
end
end
58 changes: 29 additions & 29 deletions test/test_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,114 +17,114 @@ def setup
end

def test_get_type
assert_equal @doc.type, :document
assert_equal(:document, @doc.type)
end

def test_get_type_string
assert_equal @doc.type_string, 'document'
assert_equal('document', @doc.type_string)
end

def test_get_first_child
assert_equal @first_child.type, :paragraph
assert_equal(:paragraph, @first_child.type)
end

def test_get_next
assert_equal @first_child.first_child.next.type, :emph
assert_equal(:emph, @first_child.first_child.next.type)
end

def test_insert_before
paragraph = Node.new(:paragraph)
assert_equal @first_child.insert_before(paragraph), true
assert(@first_child.insert_before(paragraph))
assert_match "<p></p>\n<p>Hi <em>there</em>.", @doc.to_html
end

def test_insert_after
paragraph = Node.new(:paragraph)
assert_equal @first_child.insert_after(paragraph), true
assert(@first_child.insert_after(paragraph))
assert_match "<strong>many nodes</strong>!</p>\n<p></p>\n", @doc.to_html
end

def test_prepend_child
code = Node.new(:code)
assert_equal @first_child.prepend_child(code), true
assert(@first_child.prepend_child(code))
assert_match '<p><code></code>Hi <em>there</em>.', @doc.to_html
end

def test_append_child
strong = Node.new(:strong)
assert_equal @first_child.append_child(strong), true
assert(@first_child.append_child(strong))
assert_match "!<strong></strong></p>\n", @doc.to_html
end

def test_get_last_child
assert_equal @last_child.type, :paragraph
assert_equal(:paragraph, @last_child.type)
end

def test_get_parent
assert_equal @first_child.first_child.next.parent.type, :paragraph
assert_equal(:paragraph, @first_child.first_child.next.parent.type)
end

def test_get_previous
assert_equal @first_child.first_child.next.previous.type, :text
assert_equal(:text, @first_child.first_child.next.previous.type)
end

def test_get_url
assert_equal @link.url, 'https://www.github.com'
assert_equal('https://www.github.com', @link.url)
end

def test_set_url
assert_equal @link.url = 'https://www.mozilla.org', 'https://www.mozilla.org'
assert_equal('https://www.mozilla.org', @link.url = 'https://www.mozilla.org')
end

def test_get_title
assert_equal @image.title, 'Favicon'
assert_equal('Favicon', @image.title)
end

def test_set_title
assert_equal @image.title = 'Octocat', 'Octocat'
assert_equal('Octocat', @image.title = 'Octocat')
end

def test_get_header_level
assert_equal @header.header_level, 3
assert_equal(3, @header.header_level)
end

def test_set_header_level
assert_equal @header.header_level = 6, 6
assert_equal(6, @header.header_level = 6)
end

def test_get_list_type
assert_equal @ul_list.list_type, :bullet_list
assert_equal @ol_list.list_type, :ordered_list
assert_equal(:bullet_list, @ul_list.list_type)
assert_equal(:ordered_list, @ol_list.list_type)
end

def test_set_list_type
assert_equal @ul_list.list_type = :ordered_list, :ordered_list
assert_equal @ol_list.list_type = :bullet_list, :bullet_list
assert_equal(:ordered_list, @ul_list.list_type = :ordered_list)
assert_equal(:bullet_list, @ol_list.list_type = :bullet_list)
end

def test_get_list_start
assert_equal @ol_list.list_start, 1
assert_equal(1, @ol_list.list_start)
end

def test_set_list_start
assert_equal @ol_list.list_start = 8, 8
assert_equal(8, @ol_list.list_start = 8)
end

def test_get_list_tight
assert_equal @ul_list.list_tight, true
assert_equal @ol_list.list_tight, true
assert(@ul_list.list_tight)
assert(@ol_list.list_tight)
end

def test_set_list_tight
assert_equal @ul_list.list_tight = false, false
assert_equal @ol_list.list_tight = false, false
refute(@ul_list.list_tight = false)
refute(@ol_list.list_tight = false)
end

def test_get_fence_info
assert_equal @fence.fence_info, 'ruby'
assert_equal('ruby', @fence.fence_info)
end

def test_set_fence_info
assert_equal @fence.fence_info = 'javascript', 'javascript'
assert_equal('javascript', @fence.fence_info = 'javascript')
end
end
6 changes: 3 additions & 3 deletions test/test_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def test_encoding
contents = fixtures_file('curly.md')
doc = CommonMarker.render_doc(contents, :SMART)
render = doc.to_html
assert_equal render.rstrip, '<p>This curly quote “makes commonmarker throw an exception”.</p>'
assert_equal('<p>This curly quote “makes commonmarker throw an exception”.</p>', render.rstrip)
end

def test_string_content_is_utf8
doc = CommonMarker.render_doc('Hi *there*')
text = doc.first_child.last_child.first_child
assert_equal text.string_content, 'there'
assert_equal text.string_content.encoding.name, 'UTF-8'
assert_equal('there', text.string_content)
assert_equal('UTF-8', text.string_content.encoding.name)
end
end
40 changes: 20 additions & 20 deletions test/test_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,50 @@ def setup

def test_uses_specified_extensions
CommonMarker.render_html(@markdown, :DEFAULT, %i[]).tap do |out|
assert out.include?('| a')
assert out.include?('| <strong>x</strong>')
assert out.include?('~~hi~~')
assert_includes out, '| a'
assert_includes out, '| <strong>x</strong>'
assert_includes out, '~~hi~~'
end

CommonMarker.render_html(@markdown, :DEFAULT, %i[table]).tap do |out|
refute out.include?('| a')
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert out.include?(html) }
assert out.include?('~~hi~~')
refute_includes out, '| a'
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end

CommonMarker.render_html(@markdown, :DEFAULT, %i[strikethrough]).tap do |out|
assert out.include?('| a')
refute out.include?('~~hi~~')
assert out.include?('<del>hi</del>')
assert_includes out, '| a'
refute_includes out, '~~hi~~'
assert_includes out, '<del>hi</del>'
end

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

CommonMarker.render_html(@markdown, :DEFAULT, %i[table strikethrough]).tap do |out|
refute out.include?('| a')
refute out.include?('| <strong>x</strong>')
refute out.include?('~~hi~~')
refute_includes out, '| a'
refute_includes out, '| <strong>x</strong>'
refute_includes out, '~~hi~~'
end
end

def test_extensions_with_renderers
doc = CommonMarker.render_doc(@markdown, :DEFAULT, %i[table])

doc.to_html.tap do |out|
refute out.include?('| a')
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert out.include?(html) }
assert out.include?('~~hi~~')
refute_includes out, '| a'
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end

HtmlRenderer.new.render(doc).tap do |out|
refute out.include?('| a')
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert out.include?(html) }
assert out.include?('~~hi~~')
refute_includes out, '| a'
%w[<table> <tr> <th> a </th> <td> c </td> <strong>x</strong>].each { |html| assert_includes out, html }
assert_includes out, '~~hi~~'
end

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

def test_bad_extension_specifications
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def open_spec_file(filename)
example_number += 1
end_line = line_number
tests << {
markdown: markdown_lines.join('').tr('→', "\t"),
html: html_lines.join('').tr('→', "\t").rstrip,
markdown: markdown_lines.join.tr('→', "\t"),
html: html_lines.join.tr('→', "\t").rstrip,
example: example_number,
start_line: start_line,
end_line: end_line,
Expand Down
4 changes: 2 additions & 2 deletions test/test_maliciousness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_rendering_with_bad_type
err = assert_raises TypeError do
CommonMarker.render_html("foo \n baz", [:SMART])
end
assert_equal err.message, 'option \':SMART\' does not exist for CommonMarker::Config::Render'
assert_equal('option \':SMART\' does not exist for CommonMarker::Config::Render', err.message)

assert_raises TypeError do
CommonMarker.render_doc("foo \n baz", 123)
Expand All @@ -79,7 +79,7 @@ def test_rendering_with_bad_type
err = assert_raises TypeError do
CommonMarker.render_doc("foo \n baz", :safe)
end
assert_equal err.message, 'option \':safe\' does not exist for CommonMarker::Config::Parse'
assert_equal('option \':safe\' does not exist for CommonMarker::Config::Parse', err.message)

assert_raises TypeError do
CommonMarker.render_doc("foo \n baz", :totes_fake)
Expand Down
Loading

0 comments on commit 9fa7f94

Please sign in to comment.