Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop updates #111

Merged
merged 2 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Naming/FileName:

AllCops:
Exclude:
- 'ext/**/*'
- 'vendor/**/*'
- 'tmp/**/*'
- "ext/**/*"
- "vendor/**/*"
- "tmp/**/*"
- "test/progit/**/*"
18 changes: 15 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require 'rake/clean'
require 'rake/extensiontask'
require 'digest/md5'


host_os = RbConfig::CONFIG['host_os']
require 'devkit' if host_os == 'mingw32'

Expand Down Expand Up @@ -36,7 +35,7 @@ end
task 'test:unit' => :compile

desc 'Run unit and conformance tests'
task test: %w(test:unit)
task test: %w[test:unit]

require 'rubocop/rake_task'

Expand All @@ -47,7 +46,7 @@ task :benchmark do
if ENV['FETCH_PROGIT']
`rm -rf test/progit`
`git clone https://github.com/progit/progit.git test/progit`
langs = %w(ar az be ca cs de en eo es es-ni fa fi fr hi hu id it ja ko mk nl no-nb pl pt-br ro ru sr th tr uk vi zh zh-tw)
langs = %w[ar az be ca cs de en eo es es-ni fa fi fr hi hu id it ja ko mk nl no-nb pl pt-br ro ru sr th tr uk vi zh zh-tw]
langs.each do |lang|
`cat test/progit/#{lang}/*/*.markdown >> test/benchinput.md`
end
Expand Down Expand Up @@ -77,8 +76,21 @@ RDoc::Task.new do |rd|
rd.options << '--fileboxes'
end

desc 'Generate the documentation and run a web server'
task serve: [:rdoc] do
require 'webrick'

puts 'Navigate to http://localhost:3000 to see the docs'

server = WEBrick::HTTPServer.new Port: 3000
server.mount '/', WEBrick::HTTPServlet::FileHandler, 'docs'
trap('INT') { server.stop }
server.start
end

desc 'Generate and publish docs to gh-pages'
task publish: [:rdoc] do
require 'tmpdir'
require 'shellwords'

Dir.mktmpdir do |tmp|
Expand Down
19 changes: 8 additions & 11 deletions commonmarker.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# encoding: utf-8
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'commonmarker/version'

Expand All @@ -10,32 +9,30 @@ Gem::Specification.new do |s|
s.version = CommonMarker::VERSION
s.summary = 'CommonMark parser and renderer. Written in C, wrapped in Ruby.'
s.description = 'A fast, safe, extensible parser for CommonMark. This wraps the official libcmark library.'
s.authors = ['Garen Torikian', 'Ashe Connor']
s.authors = ['Garen Torikian', 'Ashe Connor']
s.homepage = 'https://github.com/gjtorikian/commonmarker'
s.license = 'MIT'
s.required_ruby_version = '>= 2.0.0'

s.files = %w(LICENSE.txt README.md Rakefile commonmarker.gemspec bin/commonmarker)
s.files = %w[LICENSE.txt README.md Rakefile commonmarker.gemspec bin/commonmarker]
s.files += Dir.glob('lib/**/*.rb')
s.files += Dir.glob('ext/commonmarker/*.*')
s.test_files = Dir.glob('test/**/*').reject { |f| f == 'test/benchinput.md' || f.start_with?('test/progit/') }
s.extensions = ['ext/commonmarker/extconf.rb']

s.test_files = s.files.grep(%r{^test/})
s.executables = ['commonmarker']
s.require_paths = %w(lib ext)
s.require_paths = %w[lib ext]

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

s.add_dependency 'ruby-enum', '~> 0.5'

s.add_development_dependency 'awesome_print'
s.add_development_dependency 'json', '~> 1.8.1'
s.add_development_dependency 'minitest', '~> 5.6'
s.add_development_dependency 'minitest-focus', '~> 1.1'
s.add_development_dependency 'rake-compiler', '~> 0.9'
s.add_development_dependency 'json', '~> 1.8.1'
s.add_development_dependency 'awesome_print'
s.add_development_dependency 'rake'
s.add_development_dependency 'rdoc', '~> 5.1'
s.add_development_dependency 'rake-compiler', '~> 0.9'
s.add_development_dependency 'rdoc', '~> 6.2'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'rubocop-standard'
end
9 changes: 5 additions & 4 deletions lib/commonmarker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

begin
require 'awesome_print'
rescue LoadError; end

rescue LoadError; end # rubocop:disable Lint/SuppressedException
module CommonMarker
# Public: Parses a Markdown string into an HTML string.
#
Expand All @@ -21,7 +20,8 @@ module CommonMarker
#
# Returns a {String} of converted HTML.
def self.render_html(text, options = :DEFAULT, extensions = [])
fail TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)

opts = Config.process_options(options, :render)
text = text.encode('UTF-8')
html = Node.markdown_to_html(text, opts, extensions)
Expand All @@ -36,7 +36,8 @@ def self.render_html(text, options = :DEFAULT, extensions = [])
#
# Returns the `document` node.
def self.render_doc(text, options = :DEFAULT, extensions = [])
fail TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)

opts = Config.process_options(options, :parse)
text = text.encode('UTF-8')
Node.parse_document(text, text.bytesize, opts, extensions)
Expand Down
9 changes: 5 additions & 4 deletions lib/commonmarker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ def self.process_options(option, type)
elsif option.is_a?(Array)
option = [nil] if option.empty?
# neckbearding around. the map will both check the opts and then bitwise-OR it
option.map { |o| check_option(o, type); type.to_h[o] }.inject(0, :|)
option.map do |o|
check_option(o, type)
type.to_h[o]
end.inject(0, :|)
else
raise TypeError, "option type must be a valid symbol or array of symbols within the #{type} context"
end
end

def self.check_option(option, type)
unless type.key?(option)
raise TypeError, "option ':#{option}' does not exist for #{type}"
end
raise TypeError, "option ':#{option}' does not exist for #{type}" unless type.key?(option)
end
end
end
2 changes: 1 addition & 1 deletion lib/commonmarker/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def to_plaintext(options = :DEFAULT, width = 120)
end

# Public: Iterate over the children (if any) of the current pointer.
def each(&block)
def each
return enum_for(:each) unless block_given?

child = first_child
Expand Down
22 changes: 11 additions & 11 deletions lib/commonmarker/node/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ module Inspect
PP_INDENT_SIZE = 2

def inspect
PP.pp(self, String.new, Float::INFINITY)
PP.pp(self, +'', Float::INFINITY)
end

# @param [PrettyPrint] pp
def pretty_print(pp)
pp.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", '>') do
pp.breakable
def pretty_print(printer)
printer.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", '>') do
printer.breakable

attrs = %i[
sourcepos
Expand All @@ -34,22 +34,22 @@ def pretty_print(pp)
end
end.compact

pp.seplist(attrs) do |name, value|
pp.text "#{name}="
pp.pp value
printer.seplist(attrs) do |name, value|
printer.text "#{name}="
printer.pp value
end

if first_child
pp.breakable
pp.group(PP_INDENT_SIZE) do
printer.breakable
printer.group(PP_INDENT_SIZE) do
children = []
node = first_child
while node
children << node
node = node.next
end
pp.text 'children='
pp.pp children
printer.text 'children='
printer.pp children
end
end
end
Expand Down
12 changes: 7 additions & 5 deletions lib/commonmarker/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Renderer
attr_accessor :in_tight, :warnings, :in_plain
def initialize(options: :DEFAULT, extensions: [])
@opts = Config.process_options(options, :render)
@stream = StringIO.new(''.dup.force_encoding('utf-8'))
@stream = StringIO.new(+'')
@need_blocksep = false
@warnings = Set.new []
@in_tight = false
Expand All @@ -34,7 +34,7 @@ def render(node)
@node = node
if node.type == :document
document(node)
return @stream.string
@stream.string
elsif @in_plain && node.type != :text && node.type != :softbreak
node.each { |child| render(child) }
else
Expand All @@ -55,11 +55,11 @@ def code_block(node)
code_block(node)
end

def reference_def(_node)
end
def reference_def(_node); end

def cr
return if @stream.string.empty? || @stream.string[-1] == "\n"

out("\n")
end

Expand Down Expand Up @@ -111,14 +111,16 @@ def tagfilter(str)
)
(?=\s|>|/>)
}xi,
'&lt;\1')
'&lt;\1'
)
else
str
end
end

def sourcepos(node)
return '' unless option_enabled?(:SOURCEPOS)

s = node.sourcepos
" data-sourcepos=\"#{s[:start_line]}:#{s[:start_column]}-" \
"#{s[:end_line]}:#{s[:end_column]}\""
Expand Down
49 changes: 20 additions & 29 deletions lib/commonmarker/renderer/html_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def render(node)

def document(_)
super
if @written_footnote_ix
out("</ol>\n</section>\n")
end
out("</ol>\n</section>\n") if @written_footnote_ix
end

def header(node)
Expand Down Expand Up @@ -71,12 +69,13 @@ def list_item(node)

def tasklist(node)
return '' unless tasklist?(node)

state = if checked?(node)
'checked="" disabled=""'
else
'disabled=""'
'checked="" disabled=""'
else
'disabled=""'
end
return "><input type=\"checkbox\" #{state} /"
"><input type=\"checkbox\" #{state} /"
end

def blockquote(node)
Expand All @@ -97,9 +96,7 @@ def code_block(node)
block do
if option_enabled?(:GITHUB_PRE_LANG)
out("<pre#{sourcepos(node)}")
if node.fence_info && !node.fence_info.empty?
out(' lang="', node.fence_info.split(/\s+/)[0], '"')
end
out(' lang="', node.fence_info.split(/\s+/)[0], '"') if node.fence_info && !node.fence_info.empty?
out('><code>')
else
out("<pre#{sourcepos(node)}><code")
Expand Down Expand Up @@ -142,9 +139,7 @@ def strong(_)

def link(node)
out('<a href="', node.url.nil? ? '' : escape_href(node.url), '"')
if node.title && !node.title.empty?
out(' title="', escape_html(node.title), '"')
end
out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
out('>', :children, '</a>')
end

Expand All @@ -153,9 +148,7 @@ def image(node)
plain do
out(' alt="', :children, '"')
end
if node.title && !node.title.empty?
out(' title="', escape_html(node.title), '"')
end
out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
out(' />')
end

Expand All @@ -169,7 +162,7 @@ def code(node)
out('</code>')
end

def linebreak(node)
def linebreak(_node)
out("<br />\n")
end

Expand Down Expand Up @@ -210,9 +203,9 @@ def table_row(node)

def table_cell(node)
align = case @alignments[@column_index]
when :left; ' align="left"'
when :right; ' align="right"'
when :center; ' align="center"'
when :left then ' align="left"'
when :right then ' align="right"'
when :center then ' align="center"'
else; ''
end
out(@in_header ? "<th#{align}#{sourcepos(node)}>" : "<td#{align}#{sourcepos(node)}>", :children, @in_header ? "</th>\n" : "</td>\n")
Expand All @@ -228,28 +221,27 @@ def footnote_reference(node)
end

def footnote_definition(_)
if !@footnote_ix
unless @footnote_ix
out("<section class=\"footnotes\">\n<ol>\n")
@footnote_ix = 0
end

@footnote_ix += 1
out("<li id=\"fn#@footnote_ix\">\n", :children)
if out_footnote_backref
out("\n")
end
out("<li id=\"fn#{@footnote_ix}\">\n", :children)
out("\n") if out_footnote_backref
out("</li>\n")
#</ol>
#</section>
# </ol>
# </section>
end

private

def out_footnote_backref
return false if @written_footnote_ix == @footnote_ix

@written_footnote_ix = @footnote_ix

out("<a href=\"#fnref#@footnote_ix\" class=\"footnote-backref\">↩</a>")
out("<a href=\"#fnref#{@footnote_ix}\" class=\"footnote-backref\">↩</a>")
true
end

Expand All @@ -260,6 +252,5 @@ def tasklist?(node)
def checked?(node)
node.tasklist_state == 'checked'
end

end
end