From fe8bc184de7bce6475d6731ab5a818bce3461775 Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Wed, 26 Sep 2012 00:52:22 -0400 Subject: [PATCH] Support content-only lexer_name_for. --- lib/pygments/mentos.py | 9 +++++---- lib/pygments/popen.rb | 8 +++++++- test/test_pygments.rb | 4 ++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/pygments/mentos.py b/lib/pygments/mentos.py index 8c7b01ad..7aa05521 100755 --- a/lib/pygments/mentos.py +++ b/lib/pygments/mentos.py @@ -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) diff --git a/lib/pygments/popen.rb b/lib/pygments/popen.rb index 7fb8eb1b..16499895 100644 --- a/lib/pygments/popen.rb +++ b/lib/pygments/popen.rb @@ -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. diff --git a/test/test_pygments.rb b/test/test_pygments.rb index 6c3f86ba..38b8aba0 100644 --- a/test/test_pygments.rb +++ b/test/test_pygments.rb @@ -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)