-
Notifications
You must be signed in to change notification settings - Fork 28
added support for Rouge syntax highlighting #5
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
base: master
Are you sure you want to change the base?
Changes from all 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 |
---|---|---|
@@ -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' | ||
require 'peek/rblineprof/highlighters/rouge_highlighter' | ||
|
||
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