lib/rubocop/markdown/preprocess.rb in rubocop-md-0.1.0.pre vs lib/rubocop/markdown/preprocess.rb in rubocop-md-0.1.0
- old
+ new
@@ -2,11 +2,11 @@
module RuboCop
module Markdown
# Transform source Markdown file into valid Ruby file
# by commenting out all non-code lines
- module Preprocess
+ class Preprocess
# This is a regexp to extract code blocks from .md files.
#
# Only recognizes backticks-style code blocks.
#
# Try it: http://rubular.com/r/iJaKBkSrrT
@@ -43,35 +43,10 @@
self.current_step = current_step == (STEPS.size - 1) ? 0 : current_step + 1
end
end
class << self
- # rubocop:disable Metrics/MethodLength
- def call(src)
- parts = src.split(MD_REGEXP)
-
- walker = Walker.new
-
- parts.each do |part|
- if walker.code_body?
- next walker.next! if maybe_ruby?(@syntax) && valid_syntax?(part)
- end
-
- if walker.code_attr?
- @syntax = part.gsub(/(^\s+|\s+$)/, "")
- next walker.next!
- end
-
- comment_lines! part
-
- walker.next!
- end
-
- parts.join
- end
- # rubocop:enable Metrics/MethodLength
-
# Revert preprocess changes.
#
# When autocorrect is applied, RuboCop re-writes the file
# using preproccessed source buffer.
#
@@ -79,28 +54,65 @@
def restore!(file)
contents = File.read(file)
contents.gsub!(/^##{MARKER}/m, "")
File.write(file, contents)
end
+ end
- private
+ attr_reader :config
- # Check codeblock attribute to prevent from parsing
- # non-Ruby snippets and avoid false positives
- def maybe_ruby?(syntax)
- syntax.empty? || RUBY_TYPES.include?(syntax)
- end
+ def initialize(file)
+ @config = Markdown.config_store.for(file)
+ end
- # Try to parse with Ripper.
- # Invalid Ruby (non-Ruby) code returns `nil`.
- def valid_syntax?(src)
- !Ripper.sexp(src).nil?
- end
+ # rubocop:disable Metrics/MethodLength
+ def call(src)
+ parts = src.split(MD_REGEXP)
- def comment_lines!(src)
- return if src =~ /\A\n\z/
- src.gsub!(/^(.)/m, "##{MARKER}\\1")
+ walker = Walker.new
+
+ parts.each do |part|
+ if walker.code_body?
+ next walker.next! if maybe_ruby?(@syntax) && valid_syntax?(part)
+ end
+
+ if walker.code_attr?
+ @syntax = part.gsub(/(^\s+|\s+$)/, "")
+ next walker.next!
+ end
+
+ comment_lines! part
+
+ walker.next!
end
+
+ parts.join
+ end
+ # rubocop:enable Metrics/MethodLength
+
+ private
+
+ # Check codeblock attribute to prevent from parsing
+ # non-Ruby snippets and avoid false positives
+ def maybe_ruby?(syntax)
+ syntax.empty? || RUBY_TYPES.include?(syntax)
+ end
+
+ # Try to parse with Ripper.
+ # Invalid Ruby (non-Ruby) code returns `nil`.
+ def valid_syntax?(src)
+ return true if warn_invalid?
+ !Ripper.sexp(src).nil?
+ end
+
+ # Where to show warning when snippet is not a valid Ruby
+ def warn_invalid?
+ config["Markdown"] && config["Markdown"].fetch("WarnInvalid", false)
+ end
+
+ def comment_lines!(src)
+ return if src =~ /\A\n\z/
+ src.gsub!(/^(.)/m, "##{MARKER}\\1")
end
end
end
end