Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.gem
*.rbc
*.swp
.bundle
.config
.yardoc
Expand Down
22 changes: 7 additions & 15 deletions lib/peek/rblineprof/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
begin
require 'pygments.rb'
rescue LoadError
# Doesn't have pygments.rb installed
end
require 'peek/rblineprof/syntax_highlighter'

require 'rack/utils'

Expand All @@ -17,13 +13,9 @@ module ControllerHelpers

protected

def pygmentize?
defined?(Pygments)
end

def pygmentize(file_name, code, lexer = nil)
if pygmentize? && lexer.present?
Pygments.highlight(code, :lexer => lexer_for_filename(file_name))
def highlight(file_name, code, lexer = false)
if lexer
SyntaxHighlighter.highlight(code, lexer_for_filename(file_name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest passing SyntaxHighlighter.highlight(file_name, code) and let SyntaxHighlighter handle the lexer_for_filename(file_name) part since:

  • it's not the responsibility of the controller helper
  • it will allow to monkey-patch SyntaxHighlighter more easily

else
"<pre>#{Rack::Utils.escape_html(code)}</pre>"
end
Expand All @@ -35,8 +27,8 @@ def rblineprof_enabled?

def lexer_for_filename(file_name)
case file_name
when /\.rb$/ then 'ruby'
when /\.erb$/ then 'erb'
when /\.rb$/ then :ruby
when /\.erb$/ then :erb
end
end

Expand Down Expand Up @@ -122,7 +114,7 @@ def inject_rblineprof
end
end
output << "<pre class='duration'>#{times.join("\n")}</pre>"
output << "<pre class='code highlight'>#{pygmentize(file_name, code.join, 'ruby')}</pre>"
output << "<pre class='code highlight'>#{highlight(file_name, code.join, true)}</pre>"
output << "</div></div>" # .data then .peek-rblineprof-file
end

Expand Down
9 changes: 9 additions & 0 deletions lib/peek/rblineprof/highlighters/pygments_highlighter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Peek
module Rblineprof
class PygmentsHighlighter
def self.process(code, lexer)
Pygments.highlight(code, :lexer => lexer)
end
end
end
end
23 changes: 23 additions & 0 deletions lib/peek/rblineprof/highlighters/rouge_highlighter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Peek
module Rblineprof
class RougeHighlighter
def self.process(code, lexer)
lexer = find_lexer(lexer)

return code if lexer == :no_lexer

formatter = Rouge::Formatters::HTML.new(:line_numbers => true)
formatter.format(lexer.new.lex(code))
end

def self.find_lexer(lexer)
lexers = {
ruby: Rouge::Lexers::Ruby,
erb: Rouge::Lexers::ERB
}

lexers.fetch(lexer.to_sym, :no_lexer)
end
end
end
end
38 changes: 38 additions & 0 deletions lib/peek/rblineprof/syntax_highlighter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'peek/rblineprof/highlighters/pygments_highlighter'
require 'peek/rblineprof/highlighters/rouge_highlighter'

module Peek
module Rblineprof
class SyntaxHighlighter
class_attribute :highlighter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make SyntaxHighlighter.highlighter lazy so that we can cleanly set it to a custom highlighter in an initializer? We have this need for GitLab where we're using a custom Gitlab::Highlight class.


def self.highlight(code, lexer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we'd receive file_name, code and pass them to highlighter.process.

We could make both highlighters inherit from an abstract class in which we could put the lexer_for_filename(file_name) method to stay DRY?

return code unless highlighter

highlighter.process(code, lexer)
end
end
end
end

module Peek
module Rblineprof
begin
require 'pygments.rb'

SyntaxHighlighter.highlighter = PygmentsHighlighter
rescue LoadError
# Doesn't have pygments.rb installed
end

if SyntaxHighlighter.highlighter.nil?
begin
require 'rouge'

SyntaxHighlighter.highlighter = RougeHighlighter
rescue LoadError
# Doesn't have rouge installed
end
end
end
end