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
base: master
Are you sure you want to change the base?
Changes from 3 commits
5734ee1
8f626fa
a16f306
44f614d
b6a7a18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.gem | ||
*.rbc | ||
*.swp | ||
.bundle | ||
.config | ||
.yardoc | ||
|
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' | ||
|
||
module Peek | ||
module Rblineprof | ||
|
@@ -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)) | ||
else | ||
code | ||
end | ||
|
@@ -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 | ||
|
||
|
@@ -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>" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be great to use a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'peek/rblineprof/highlighters/pygments_highlighter.rb' | ||
require 'peek/rblineprof/highlighters/rouge_highlighter.rb' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the |
||
|
||
module Peek | ||
module Rblineprof | ||
class SyntaxHighlighter | ||
class_attribute :highlighter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make |
||
|
||
def self.highlight(code, lexer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we'd receive We could make both highlighters inherit from an abstract class in which we could put the |
||
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 |
There was a problem hiding this comment.
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 letSyntaxHighlighter
handle thelexer_for_filename(file_name)
part since:SyntaxHighlighter
more easily