Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for Rouge syntax highlighting #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

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

module Peek
module Rblineprof
Expand All @@ -15,13 +11,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
code
end
Expand All @@ -33,8 +25,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 @@ -120,7 +112,7 @@ def inject_rblineprof
end
end
output << "<pre class='duration'>#{times.join("\n")}</pre>"
output << "<div class='code'>#{pygmentize(file_name, code.join, 'ruby')}</div>"
output << "<div class='code'>#{highlight(file_name, code.join, true)}</div>"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be great to use a <pre> instead of a <div> here, or at least allow to customize that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I submitted #16 for this proposal.

output << "</div></div>" # .data then .peek-rblineprof-file
end

Expand Down
9 changes: 9 additions & 0 deletions lib/peek/rblineprof/highlighters/pygments_highlighter.rb
@@ -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
@@ -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
@@ -0,0 +1,38 @@
require 'peek/rblineprof/highlighters/pygments_highlighter.rb'
require 'peek/rblineprof/highlighters/rouge_highlighter.rb'
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove the .rb suffix?


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