From 3a7d3ebe3a9940befb5d62a53056b4de56a0a982 Mon Sep 17 00:00:00 2001 From: dongnhzigexn Date: Thu, 25 Apr 2019 19:57:01 +0900 Subject: [PATCH 1/9] Added: HTML formatter --- lib/pronto.rb | 1 + lib/pronto/assets/logo.png | Bin 0 -> 5390 bytes lib/pronto/assets/output.html.erb | 237 +++++++++++++++++++++++++ lib/pronto/formatter/formatter.rb | 1 + lib/pronto/formatter/html_formatter.rb | 121 +++++++++++++ 5 files changed, 360 insertions(+) create mode 100644 lib/pronto/assets/logo.png create mode 100644 lib/pronto/assets/output.html.erb create mode 100644 lib/pronto/formatter/html_formatter.rb diff --git a/lib/pronto.rb b/lib/pronto.rb index 62502f8c..51d7d82e 100644 --- a/lib/pronto.rb +++ b/lib/pronto.rb @@ -49,6 +49,7 @@ require 'pronto/formatter/bitbucket_pull_request_formatter' require 'pronto/formatter/bitbucket_server_pull_request_formatter' require 'pronto/formatter/checkstyle_formatter' +require 'pronto/formatter/html_formatter' require 'pronto/formatter/null_formatter' require 'pronto/formatter/formatter' diff --git a/lib/pronto/assets/logo.png b/lib/pronto/assets/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..c091ad7f495a97a9631ed51d470df3a52a2aabb6 GIT binary patch literal 5390 zcmZ`+2{=^k`yQI?vScSTSrTUK8jXqU+4mW{VK5l`T9(L?JzGKu+4rSv*|H@vWD6m? z>>{%N>HB`)uYcF~JJ)s2yyt%I`+c7KIq!R}GhrHP3gjeABme+_TuD(@>-<;c;v^zC zzjh^Q@&W)P(ij;T4J8>Fb`2LtD~vrF08k`Pd*-R5Jw_LbYZPznrv|Ze?lL_fX8&|Y zkrt>Kru0YyKO~6DL-w*f4Hb``rA`Z;LMX^Wj)wGcAZnEOJEcZvtEQ~n!epc!@n(tW z&Q!`t7jk-Rsb+M{dee8b9-wg?2z=*;z#|IE4v_s#zT!}b<+N1-# za;@jXfniFhT&mIMn1$u{R)^bF+6O2>-h?*QnI~Q7@WCwh|45*%ZvBMf-ZxY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onr_9pgA6HK9F804XSz18Q_Gm2H!OittC-|@F&U^ch@c$L*W$yG>vR_U7 z%Ny*XatST8tE0X9MGJHtFm7-`*e^qW!vF2aUn&_#tdk4c)%9Ek7yVoEJN73Z@!vFX zaiPBjzk`1Y)Lk&=PR%ds`lqJfu|N69e@pN?_?zIO^%5EwFSI>E7IWUvUoOPOV32UljIB@*DIg`&Sy||4Q>);5VVAg@mg+$`)OyXtW+F$7Tu7#7FEC2HbfRn_fu4V!N z1Tspp(mGyv>#535SacbJ?WTk~F)Y&qW+3Y6uy6*;NFh|qGUy4|QUEx6kE-_;uOO-O zf}Z%S5C}*%OX@Ro;}$`@^izf~Ch5z9RH{OjalI;w3kM+^nLG}YhBfu|CUXHY?$h=A zXSjpaslt_&jc9g%BR*ogcDvOHh3ZB_;B@%rljex#;TrDOAIYyl(M`tQY&4tnqhb2t zX<6hu*?hcqCx_i` zM`@7nuvP)@OE9b}DHO9XvfZ#fmZ_C2q%;GH@1 zc721mKwScBiTY-6R0@P2IIx5p^-qml<{@`ecsMWdcOH5E z1~&+zvR9&H($ZR&!wt%t@*KC99;?)W@Z)!8Gn2oW;l>7A3%sY0t)tGO+7yPZ#iwP< zD~%tTi7`XxL*731|kpbyV>Ed?ITBDVIwyik5oA=aeM`z&safrTXGh%k_N zf53n#{wyL`?xW4L=&2(J`oGPlbbEu2MQwc)}s z)!2HlE*jhLc0o(z?JRVl&UlWlx#~!3VnM6CY$bi$A*d)0ws9N7g8bNdwG!>yK6i@& z-MujS0D6rgZ}j$MWk2%1Zg)~ln#ml^+72Bs0c&&1#Jj!CS7})N*sB`e_Z@mTTCg)p z3uc2NCPfs;aagWC%a}-uihmCkk*WD(jE_>45H^-Wvo8us~op{uyne=-q7CDAy8}jKuIJ@`I9`~pd>@xz#}?s zqw2X<=2Bj59VuBq4u|lSmwOho<`vr=_;q?=% + + + + RuboCop Inspection Report + <%# TODO: Clean up the messy markup and style definitions. %> + + + + + + +
+ <% messages.each do |message| %> +
+

 

+
+
+
+ Line #<%= message.line.new_lineno %> – + <%= message.level %>: + <%= message.msg %> +
+
<%= message.line.line.content %>
+
+
+
+ <% end %> +
+
+ Generated by RuboCop + <%= RuboCop::Version::STRING %> +
+ + + diff --git a/lib/pronto/formatter/formatter.rb b/lib/pronto/formatter/formatter.rb index 310b6065..11f2832c 100644 --- a/lib/pronto/formatter/formatter.rb +++ b/lib/pronto/formatter/formatter.rb @@ -22,6 +22,7 @@ def self.names 'json' => JsonFormatter, 'checkstyle' => CheckstyleFormatter, 'text' => TextFormatter, + 'html' => HtmlFormatter, 'null' => NullFormatter }.freeze end diff --git a/lib/pronto/formatter/html_formatter.rb b/lib/pronto/formatter/html_formatter.rb new file mode 100644 index 00000000..7e1c05c9 --- /dev/null +++ b/lib/pronto/formatter/html_formatter.rb @@ -0,0 +1,121 @@ +# frozen_string_literal: true + +require 'cgi' +require 'erb' +require 'ostruct' +require 'base64' + +module Pronto + module Formatter + class HtmlFormatter < Base + ELLIPSES = '...'.freeze + TEMPLATE_PATH = + File.expand_path('../assets/output.html.erb', __dir__) + + Color = Struct.new(:red, :green, :blue, :alpha) do + def to_s + "rgba(#{values.join(', ')})" + end + + def fade_out(amount) + dup.tap do |color| + color.alpha -= amount + end + end + end + + attr_reader :files, :summary + + def format(messages, _, _) + render_html(messages) + end + + def render_html(messages) + context = ERBContext.new(messages) + + template = File.read(TEMPLATE_PATH, encoding: Encoding::UTF_8) + + erb = if RUBY_VERSION >= '2.6' + ERB.new(template, trim_mode: '-') + else + ERB.new(template, nil, '-') + end + html = erb.result(context.binding) + + html + end + + # This class provides helper methods used in the ERB template. + class ERBContext + # include PathUtil + # include TextUtil + + SEVERITY_COLORS = { + refactor: Color.new(0xED, 0x9C, 0x28, 1.0), + convention: Color.new(0xED, 0x9C, 0x28, 1.0), + warning: Color.new(0x96, 0x28, 0xEF, 1.0), + error: Color.new(0xD2, 0x32, 0x2D, 1.0), + fatal: Color.new(0xD2, 0x32, 0x2D, 1.0) + }.freeze + + LOGO_IMAGE_PATH = + File.expand_path('../assets/logo.png', __dir__) + + attr_reader :messages, :summary + + def initialize(messages) + @messages = messages.sort_by(&:path) + @summary = 'IMPLEMENT LATER' + end + + # Make Kernel#binding public. + def binding + super + end + + def decorated_message(message) + message.msg.gsub(/`(.+?)`/) do + "#{Regexp.last_match(1)}" + end + end + + # def highlighted_source_line(message) + # source_before_highlight(message) + + # hightlight_source_tag(message) + + # source_after_highlight(message) + + # possible_ellipses(message.location) + # end + + # def hightlight_source_tag(message) + # "" \ + # "#{escape(offense.highlighted_area.source)}" \ + # '' + # end + + # def source_before_highlight(message) + # source_line = message.location.source_line + # escape(source_line[0...message.highlighted_area.begin_pos]) + # end + + # def source_after_highlight(offense) + # source_line = offense.location.source_line + # escape(source_line[offense.highlighted_area.end_pos..-1]) + # end + + # def possible_ellipses(location) + # location.first_line == location.last_line ? '' : " #{ELLIPSES}" + # end + + def escape(string) + CGI.escapeHTML(string) + end + + def base64_encoded_logo_image + image = File.read(LOGO_IMAGE_PATH, binmode: true) + Base64.encode64(image) + end + end + end + end +end + From d0c7787070839c495240a8dfbe8c7df77fe06991 Mon Sep 17 00:00:00 2001 From: dongnhzigexn Date: Fri, 26 Apr 2019 11:11:57 +0900 Subject: [PATCH 2/9] Update output html --- lib/pronto/assets/output.html.erb | 45 ++++++++++++++++++++------ lib/pronto/formatter/html_formatter.rb | 19 ++++++++--- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/lib/pronto/assets/output.html.erb b/lib/pronto/assets/output.html.erb index 739c17f4..2ff2d076 100644 --- a/lib/pronto/assets/output.html.erb +++ b/lib/pronto/assets/output.html.erb @@ -130,6 +130,10 @@ #offenses .offense-box .offense-reports .report .location { font-weight: bold; } + #offenses .offense-box .offense-reports .report .runner { + font-weight: bold; + text-transform: capitalize; + } #offenses .offense-box .offense-reports .report .message code { padding: 0.3em; background-color: rgba(0,0,0,0.07); @@ -209,28 +213,49 @@ +
+
+
+ <%= pluralize(files.values.flatten.count, 'offense', no_for_zero: true) %> detected: +
+ +
-
- <% messages.each do |message| %> -
+
+ <% files.each do |path, offenses| %> +

 

+

<%= path %> - <%= pluralize(offenses.count, 'offense') %>

+ <% offenses.each do |offense| %>
- Line #<%= message.line.new_lineno %> – - <%= message.level %>: - <%= message.msg %> + [<%= offense.runner.to_s.split('::').last %>] + Line #<%= offense.line.new_lineno %> – + <%= offense.level %>: + <%= decorated_message(offense.msg) %>
-
<%= message.line.line.content %>
+
<%= offense.line.line.content %>
+ <% end %>
<% end %>
- Generated by RuboCop - <%= RuboCop::Version::STRING %> + Generated by Pronto + <%= Pronto::Version::STRING %>
diff --git a/lib/pronto/formatter/html_formatter.rb b/lib/pronto/formatter/html_formatter.rb index 7e1c05c9..9af2db0a 100644 --- a/lib/pronto/formatter/html_formatter.rb +++ b/lib/pronto/formatter/html_formatter.rb @@ -61,11 +61,10 @@ class ERBContext LOGO_IMAGE_PATH = File.expand_path('../assets/logo.png', __dir__) - attr_reader :messages, :summary + attr_reader :files def initialize(messages) - @messages = messages.sort_by(&:path) - @summary = 'IMPLEMENT LATER' + @files = messages.group_by(&:path) end # Make Kernel#binding public. @@ -73,12 +72,22 @@ def binding super end - def decorated_message(message) - message.msg.gsub(/`(.+?)`/) do + def decorated_message(msg) + msg.gsub(/`(.+?)`/) do "#{Regexp.last_match(1)}" end end + def pluralize(number, thing, options = {}) + if number.zero? && options[:no_for_zero] + "no #{thing}s" + elsif number == 1 + "1 #{thing}" + else + "#{number} #{thing}s" + end + end + # def highlighted_source_line(message) # source_before_highlight(message) + # hightlight_source_tag(message) + From 7351d929c9eaba4eea28eeca8f44e5a4dcb94eb7 Mon Sep 17 00:00:00 2001 From: dongnhzigexn Date: Fri, 26 Apr 2019 12:49:17 +0900 Subject: [PATCH 3/9] Fixed: Encoding incompatible --- lib/pronto/assets/output.html.erb | 2 +- lib/pronto/formatter/html_formatter.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pronto/assets/output.html.erb b/lib/pronto/assets/output.html.erb index 2ff2d076..78251932 100644 --- a/lib/pronto/assets/output.html.erb +++ b/lib/pronto/assets/output.html.erb @@ -246,7 +246,7 @@ <%= offense.level %>: <%= decorated_message(offense.msg) %>
-
<%= offense.line.line.content %>
+
<%= offense.line.line.content.force_encoding('UTF-8') %>
<% end %> diff --git a/lib/pronto/formatter/html_formatter.rb b/lib/pronto/formatter/html_formatter.rb index 9af2db0a..ca93cebf 100644 --- a/lib/pronto/formatter/html_formatter.rb +++ b/lib/pronto/formatter/html_formatter.rb @@ -75,7 +75,7 @@ def binding def decorated_message(msg) msg.gsub(/`(.+?)`/) do "#{Regexp.last_match(1)}" - end + end.force_encoding('UTF-8') end def pluralize(number, thing, options = {}) From 4390c21342b069d42afaed483a7ec319f1f950d0 Mon Sep 17 00:00:00 2001 From: dongnhzigexn Date: Tue, 7 May 2019 12:44:20 +0900 Subject: [PATCH 4/9] Update HTML template --- lib/pronto/assets/output.html.erb | 329 ++++++++---------------------- 1 file changed, 87 insertions(+), 242 deletions(-) diff --git a/lib/pronto/assets/output.html.erb b/lib/pronto/assets/output.html.erb index 78251932..b413ab36 100644 --- a/lib/pronto/assets/output.html.erb +++ b/lib/pronto/assets/output.html.erb @@ -1,262 +1,107 @@ + - - - RuboCop Inspection Report - <%# TODO: Clean up the messy markup and style definitions. %> - - - - -