module RailsbpInBrowser

Constants

VERSION

Public Class Methods

check_and_create_dir(dirname) click to toggle source
# File lib/railsbp_in_browser.rb, line 17
def check_and_create_dir dirname
  unless File.directory?(dirname)
    Dir.mkdir(dirname)
  end
end
generate_html() click to toggle source
# File lib/railsbp_in_browser.rb, line 23
def generate_html
  puts "Generating the code climate ..."
  `rails_best_practices . > rails_best_practices_output.html`

  output_file_path = "#{ENV['HOME']}/tmp/code_climate.html"
  dirname = File.dirname output_file_path
  check_and_create_dir dirname
  f = File.open("rails_best_practices_output.html")
  of = File.open(output_file_path, 'w')
  reg = /.*\[31m(.*):([0-9]+) - (.*)\e.*/
  f.each_line do |line|
    next unless reg.match(line)
    match_line = $2
    match_file = $1
    article_name = $3
    if get_article_name(article_name)
      of.puts("<h2><a target='_blank' href=#{get_article_name(article_name)}>#{article_name}</a></h2>")
    else
      of.puts("<h2>#{article_name}</h2>")
    end
    of.puts("<p>#{match_file.gsub(Dir.pwd, '')}:<strong style=\"background-color:darkseagreen\">#{match_line}</strong></p>")
    ffa = File.open(match_file).to_a
    of.puts("<div style=\"background-color:lightgrey;margin-left:50\">")
    start = match_line.to_i - 5 > 0 ? match_line.to_i - 5 : 0
    (start .. match_line.to_i + 5).each do |index|
      if ffa[index] && (index + 1) == match_line.to_i
        of.puts("<code style=\"background-color:darkseagreen\">#{index+1} - #{ffa[index].gsub('<', '&lt;').gsub('>', '&gt;')}</code><br>")
      elsif ffa[index]
        of.puts("<code>#{index+1} - #{ffa[index].gsub('<', '&lt;').gsub('>', '&gt;')}</code><br>")
      end
    end
    of.puts("</div>")
  end

  f.close
  of.close

  puts "Open report in the Browser"
  Launchy.open(output_file_path)

  `rm rails_best_practices_output.html`
end
get_article_name(str) click to toggle source
# File lib/railsbp_in_browser.rb, line 7
def get_article_name str
  reg = /([a-zA-Z0-9 _]+)\(?.*/
  key = if reg.match str
    $1.strip.downcase
  else
    ""
  end
  OnlineDocs::DOC_MAPPINGS[key]
end