Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support content-only lexer_name_for.

  • Loading branch information...
commit fe8bc184de7bce6475d6731ab5a818bce3461775 1 parent b0a6182
Victor Costan pwnall authored
9 lib/pygments/mentos.py
View
@@ -66,7 +66,8 @@ def return_lexer(self, lexer, args, inputs, code=None):
- 'lexer' ("python")
- 'mimetype' ("text/x-ruby")
- 'filename' ("yeaaah.py")
- - 'code' ("import derp" etc)
+
+ The code argument should be a string, such as "import derp".
The code guessing method is not especially great. It is advised that
clients pass in a literal lexer name whenever possible, which provides
@@ -101,7 +102,7 @@ def return_lexer(self, lexer, args, inputs, code=None):
return lexers.guess_lexer(code, **inputs)
else:
- _write_error("No lexer")
+ return None
def highlight_text(self, code, lexer, formatter_name, args, kwargs):
@@ -186,7 +187,7 @@ def get_data(self, method, lexer, args, kwargs, text=None):
_write_error("No lexer")
else:
- _write_error("No lexer")
+ _write_error("Invalid method " + method)
return res
@@ -294,7 +295,7 @@ def start(self):
text = sys.stdin.read(_bytes)
# Sanity check the return.
- if method == 'highlight':
+ if _bytes:
start_id, end_id = self._get_ids(text)
text = self._check_and_return_text(text, start_id, end_id)
8 lib/pygments/popen.rb
View
@@ -158,7 +158,13 @@ def lexer_name_for(*args)
opts = {}
end
- mentos(:lexer_name_for, args, opts)
+ if args.last.is_a?(String)
+ code = args.pop
+ else
+ code = nil
+ end
+
+ mentos(:lexer_name_for, args, opts, code)
end
# Public: Highlight code.
4 test/test_pygments.rb
View
@@ -171,6 +171,10 @@ def test_lexer_by_filename_and_content
assert_equal 'rb', P.lexer_name_for(RUBY_CODE, :filename => 'test.rb')
end
+ def test_lexer_by_content
+ assert_equal 'rb', P.lexer_name_for(RUBY_CODE)
+ end
+
def test_lexer_by_nothing
assert_raise MentosError do
P.lexer_name_for(:invalid => true)
Please sign in to comment.
Something went wrong with that request. Please try again.