Permalink
Browse files

Explicitly pass in code to matching

  • Loading branch information...
1 parent 61ed40e commit 9d32e7d5f0b5791615ba41878a8e690a49e7cc56 @tnm committed Jul 13, 2012
Showing with 16 additions and 32 deletions.
  1. +6 −6 lib/pygments/mentos.py
  2. +2 −16 lib/pygments/popen.rb
  3. +8 −10 test/test_pygments.rb
View
@@ -42,7 +42,7 @@ class Mentos(object):
def __init__(self):
pass
- def return_lexer(self, args, inputs):
+ def return_lexer(self, args, inputs, code):
"""
Accepting a variety of possible inputs, return a Lexer object.
@@ -70,14 +70,14 @@ def return_lexer(self, args, inputs):
# If we have code and a filename, pygments allows us to guess
# with both. This is better than just guessing with code.
- if 'code' in inputs:
- return lexers.guess_lexer_for_filename(name, inputs['code'])
+ if code:
+ return lexers.guess_lexer_for_filename(name, code)
else:
return lexers.get_lexer_for_filename(name)
# If all we got is code, try anyway.
if args:
- return lexers.guess_lexer(args[0])
+ return lexers.guess_lexer(code)
else:
return {"error": "No lexer"}
@@ -97,7 +97,7 @@ def highlight_text(self, code, formatter_name, args, kwargs):
_format_name = "html"
# Return a lexer object
- lexer = self.return_lexer(args, kwargs)
+ lexer = self.return_lexer(args, kwargs, code)
# Make sure we sucessfuly got a lexer
if lexer:
@@ -159,7 +159,7 @@ def get_data(self, method, args, kwargs, text=None):
res = fmt.get_style_defs(args[1])
elif method == 'lexer_name_for':
- lexer = self.return_lexer(args, kwargs)
+ lexer = self.return_lexer(args, kwargs, text)
# Return error if its an error
if isinstance(lexer, dict):
View
@@ -27,21 +27,6 @@ def start(pygments_path = File.expand_path('../../../vendor/pygments-main/', __F
# A pipe to the mentos python process. #POSIX::Spawn#popen4 gives us
# the pid and three IO objects to write and read..
@pid, @in, @out, @err = popen4(File.expand_path('../mentos.py', __FILE__))
-
- # Deal with dying child processes.
- Signal.trap('CHLD') do
-
- # Once waitpid() returns the pid (i.e., the child process exited),
- # we can safely set our pid variable to nil. Next time a Pygments.rb
- # method gets called, the child will be spawned again, so we don't
- # need to spawn a new child in this block right now. For extra safety,
- # if an ECHILD (no children) is set by waitpid(), don't die horribly;
- # still set the @pid to nil.
- begin
- @pid = nil if @pid && Process.waitpid(@pid, Process::WNOHANG) == @pid
- rescue Errno::ECHILD
- end
- end
end
# Stop the child process by issuing a brutal kill.
@@ -230,7 +215,6 @@ def mentos(method, args=[], kwargs={}, code=nil)
# Check if the header indicates an error
if header["error"]
- f.write("an error")
# Raise this as a Ruby exception of the MentosError class.
# Pythonland will return a traceback, or at least some information
# about the error, in the error key.
@@ -249,6 +233,8 @@ def mentos(method, args=[], kwargs={}, code=nil)
res = res.strip if code || method == :lexer_name_for || method == :css
res
end
+ else
+ raise MentosError.new("No header received back.")
end
rescue Errno::EPIPE, EOFError
View
@@ -17,12 +17,12 @@ def test_highlight_empty
def test_highlight_defaults_to_html
code = P.highlight(RUBY_CODE)
- assert_match '<span class="c">#!/usr/bin/ruby</span>', code
+ assert_match '<span class="c1">#!/usr/bin/ruby</span>', code
end
def test_highlight_works_with_larger_files
code = P.highlight(REDIS_CODE)
- assert_match '>used_memory_peak_human<', code
+ assert_match 'used_memory_peak_human', code
end
def test_highlight_markdown_compatible_html
@@ -82,17 +82,17 @@ def test_highlight_works_with_single_character_input
def test_highlight_works_with_trailing_newline
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE)
- assert_match '<span class="c">#!/usr/bin/ruby</span>', code
+ assert_match '<span class="c1">#!/usr/bin/ruby</span>', code
end
def test_highlight_works_with_multiple_newlines
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE + "\n\n")
- assert_match '<span class="c">#!/usr/bin/ruby</span>', code
+ assert_match '<span class="c1">#!/usr/bin/ruby</span>', code
end
def test_highlight_works_with_trailing_cr
code = P.highlight(RUBY_CODE_TRAILING_NEWLINE + "\r")
- assert_match '<span class="c">#!/usr/bin/ruby</span>', code
+ assert_match '<span class="c1">#!/usr/bin/ruby</span>', code
end
def test_highlight_still_works_with_invalid_code
@@ -104,10 +104,6 @@ def test_highlight_still_works_with_invalid_code
class PygmentsLexerTest < Test::Unit::TestCase
RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'"
- def test_lexer_by_content
- assert_equal 'rb', P.lexer_name_for(RUBY_CODE)
- end
-
def test_lexer_by_mimetype
assert_equal 'rb', P.lexer_name_for(:mimetype => 'text/x-ruby')
assert_equal 'json', P.lexer_name_for(:mimetype => 'application/json')
@@ -129,7 +125,9 @@ def test_lexer_by_filename_and_content
end
def test_lexer_by_nothing
- assert_equal nil, P.lexer_name_for(:invalid => true)
+ assert_raise MentosError do
+ P.lexer_name_for(:invalid => true)
+ end
end
end

0 comments on commit 9d32e7d

Please sign in to comment.