Skip to content

Commit

Permalink
Support content-only lexer_name_for.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Sep 26, 2012
1 parent b0a6182 commit fe8bc18
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/pygments/mentos.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion lib/pygments/popen.rb
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions test/test_pygments.rb
Expand Up @@ -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)
Expand Down

0 comments on commit fe8bc18

Please sign in to comment.