lib/rubocop/markdown/preprocess.rb in rubocop-md-0.2.0 vs lib/rubocop/markdown/preprocess.rb in rubocop-md-0.3.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require "ripper"
module RuboCop
module Markdown
# Transform source Markdown file into valid Ruby file
@@ -8,13 +10,13 @@
# This is a regexp to extract code blocks from .md files.
#
# Only recognizes backticks-style code blocks.
#
# Try it: http://rubular.com/r/iJaKBkSrrT
- MD_REGEXP = /^([ \t]*`{3,4})([\w[[:blank:]]]*\n)([\s\S]+?)(^[ \t]*\1[[:blank:]]*\n?)/m
+ MD_REGEXP = /^([ \t]*`{3,4})([\w[[:blank:]]]*\n)([\s\S]+?)(^[ \t]*\1[[:blank:]]*\n?)/m.freeze
- MARKER = "<--rubocop/md-->".freeze
+ MARKER = "<--rubocop/md-->"
# See https://github.com/github/linguist/blob/v5.3.3/lib/linguist/languages.yml#L3925
RUBY_TYPES = %w[
ruby
jruby
@@ -105,25 +107,27 @@
# Try to parse with Ripper
# Invalid Ruby code (or non-Ruby) returns `nil`.
# Return true if it's explicit Ruby and warn_invalid?
def valid_syntax?(syntax, src)
return true if ruby?(syntax) && warn_invalid?
+
!Ripper.sexp(src).nil?
end
# Whether to show warning when snippet is not a valid Ruby
def warn_invalid?
- config["Markdown"] && config["Markdown"].fetch("WarnInvalid", true)
+ config["Markdown"]&.fetch("WarnInvalid", true)
end
# Whether to try to detect Ruby by parsing codeblock.
# If it's set to false we lint only implicitly specified Ruby blocks.
def autodetect?
- config["Markdown"] && config["Markdown"].fetch("Autodetect", true)
+ config["Markdown"]&.fetch("Autodetect", true)
end
def comment_lines!(src)
return if src =~ /\A\n\z/
+
src.gsub!(/^(.)/m, "##{MARKER}\\1")
end
end
end
end