Last active
December 17, 2015 06:08
-
-
Save jurriaan/5562843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'rubocop' | |
require 'tmpdir' | |
url = 'https://raw.github.com/bbatsov/ruby-style-guide/master/README.md' | |
data = open(url).read | |
code_blocks = data.scan(/```ruby[\r\n]+(.*?)```/mi) | |
puts "Found #{code_blocks.length} code blocks" | |
good_blocks = [] | |
# bad_regexp = /\#[^$]*?bad[^$]*?$/ | |
code_blocks.each do |block| | |
block = block.first | |
if block =~ /\#\s*good/ | |
# leading code | |
match = block.match(/\A(.*?)(?:\#[^$]*?bad[^$]*?$|\#\s*good)/mi) | |
good_block = '' | |
good_block << match[1] if !match.nil? && match[1].strip.length > 0 | |
# good blocks | |
good_block << block.scan(/(^\s+\#\s*good.*?)(?:\Z|\#[^$]*?bad[^$]*?$)/mi) | |
.flatten.join | |
# trailing code | |
match2 = block.match(/(?:\#[^$]*?bad[^$]*?$)((?!good)*?)\Z/mi) | |
good_block << match2[1] unless match2.nil? | |
good_blocks << good_block | |
elsif block !~ /\#[^$]*?bad[^$]*?$/ # not good and not bad = good | |
# good_blocks << block | |
# not always the case.. make the style_guide more consistent | |
end | |
end | |
good_blocks = good_blocks.join("\n") | |
good_blocks.gsub!(/^\s*\.+\s*$/mi, '') # remove ... in functions | |
good_blocks.gsub!(/[ \t]+$/mi, "\n") # remove excess whitespace | |
good_blocks.gsub!(/\n\n\n+/mi, "\n\n") # remove excess newlines | |
Dir.mktmpdir do |dir| | |
Dir.chdir(dir) | |
puts "Writing output to style_guide.rb.." | |
open('style_guide.rb', 'w') do |f| | |
f << good_blocks | |
end | |
cli = Rubocop::CLI.new | |
cli.run(['style_guide.rb']) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment